我想从Windows上的另一个程序调用SAS程序。我有一些在批处理模式下从命令行调用SAS的经验,但没有真正的经验从它接收消息并处理这些消息。我搜索了很多关于从SAS程序中读取stdin的信息,但似乎无法弄清楚如何让我的SAS程序写出stdout或stderr。我可以在Windows中做到这一点吗?在Windows中从SAS捕获stdout和stderr?
从SAS程序,我想做到以下几点:
- 重定向警告信息和错误信息,而不是到stderr它们打印到日志文件
- 内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;
的Python调用SAS:
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,但它是一个控制台应用程序?它可能没有stdout/stderr的句柄。在这种情况下,尝试使用PyWin32的'win32com'模块[使用OLE自动化SAS](http://support.sas.com/documentation/cdl/en/hostwin/63285/HTML/default/viewer.htm#oleauto.htm)。 – eryksun
以下是有关使用可能有所帮助的未命名管道的SAS文档的链接: http://support.sas.com/documentation/cdl/en/hostwin/63285/HTML/default/viewer.htm#unnamed.htm – RWill