I want to call a SAS program from another program on Windows. I have some experience invoking SAS from the command line in batch mode, but no real experience receiving messages from it and handling those messages. I have googled around and found quite a bit of information about reading from stdin from within a SAS program, but I cannot seem to figure out how to make my SAS program write out to stdout or stderr. Can I even do this in Windows?
From the SAS program, I would like to do the following:
- Redirect Warning Messages and Error Messages to stderr instead of printing them to the log file
- Within the SAS program, manually detect errors and/or other issues and output them to either stderr or stdout.
Here is what I have tried:
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 that calls the 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()
My results on the console from this are:
('', '')
win32com
module to automate SAS using OLE. – Eryk Sun