【发布时间】:2011-11-22 22:47:16
【问题描述】:
我想从 Windows 上的另一个程序调用一个 SAS 程序。我有一些从命令行以批处理模式调用 SAS 的经验,但没有真正的经验从它接收消息并处理这些消息。我搜索了一下,发现了很多关于从 SAS 程序中读取 stdin 的信息,但我似乎无法弄清楚如何让我的 SAS 程序写出到 stdout 或 stderr。我什至可以在 Windows 中执行此操作吗?
通过 SAS 程序,我想做以下事情:
- 将警告消息和错误消息重定向到标准错误,而不是将它们打印到日志文件中
- 在 SAS 程序中,手动检测错误和/或其他问题并将其输出到 stderr 或 stdout。
这是我尝试过的:
SAS
data test;
attrib i length=8;
do i = 1 to 10;
put 'putting'; *how can i make this go to stdout?;
putlog 'putting to log'; *this can go to the log - that is okay;
if i = 5 then do;
*pretend this is an error I am manually detecting - how can i make this go to stderr?;
put 'we found 5';
end;
output;
end;
run;
data _null_;
1 = y; *this is an error detected by SAS. How can I make this go to stderr?;
run;
调用 SAS 的 Python:
import subprocess
import os
if __name__ == '__main__':
filename = os.path.normpath(r'C:\Users\oob\Desktop\sas_python_test.sas')
sas_executable = os.path.normpath(r'C:\Program Files\SAS\SASFoundation\9.2\sas.exe')
cmd = r'"' + sas_executable + r'"' + " " + r'"' + filename + r'"'
p = subprocess.Popen(cmd,shell=False,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
print p.communicate()
我在控制台上的结果是:
('', '')
【问题讨论】:
-
我从未使用过 SAS,但它是控制台应用程序吗?它可能没有标准输出/标准错误的句柄。在这种情况下,试试 PyWin32 的
win32com模块到 automate SAS using OLE。 -
这里是关于使用未命名管道的 SAS 文档链接,可能会有所帮助:support.sas.com/documentation/cdl/en/hostwin/63285/HTML/default/…