1
votes

I’m having trouble checking for errors in python’s subprocess. The subprocess is a sas program. Any wizards have any magic to share? I’d like to avoid searching the sas log for strings and instead rely on the return code.

I’m stuck w/ python 2.7 on a AIX server.

I’m able to invoke a sas program in a variety of ways using python’s subprocess module. I’ve successfully invoked a program using the check_output module, the Popen module or even just the call object. However, for the life of me I cannot get the return code!

Import subprocess
Subprocess.call([‘SAS’,’/path/program.sas’])

Or

Import subprocess
P = subprocess.check_output(“sas /path/program.sas”, stderr=subprocess.STDOUT, shell=True)
Print(p)

Or Import subprocess P = subprocess.Popen(“sas /path/program.sas”, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) proc.wait() Standard_output, standard_error = proc.communicate*( Print(proc.returncode) Or

Import subprocess
Try:
  output = subprocess.check_output(‘sas /path/program.sas’, stderr=subprocess.STDOUT, shell=true)
Except subprocess.CalledProcessError as exc:
  Print(“Status : Fail”, exc.returncode)

Python 2.7 Subprocess

SAS Completion Status

1
Does it work with simpler programs like true and false? - Lorinczy Zsigmond
@LorinczyZsigmond the 3rd example will return errors with simple Unix commands such as ls - l [non existent file], but I’m not getting the errors returned from sas programs. - hisnameismyname2
Does it work from shell? SAS ...; echo ReturnStatus=$? it should print 0 for success, non-zero for failure. - Lorinczy Zsigmond
@LorinczyZsigmond haven’t tried this, but I like your idea. Thanks! I’ll give it a try tomorrow. - hisnameismyname2
@LorinczyZsigmond thanks again for the tip! It helped me get to the source of my issue (user error). I posted the answer below. - hisnameismyname2

1 Answers

0
votes

In my case, I kept receiving a return code of 0 or ‘’. This was happening, because of my sas program. See, my program was nothing more than an invalid libname reference.

Libname x ‘blah’;

This was returning 0 when I expected 2. I added abortabend to my program a viola python returned 2.

proc = subprocess.Popen(“sas /path/program.sas;” shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
proc.wait()
standard_output, standard_error = proc.communicate()

print(proc.returncode)

Note that you may need to setup your autoexec and config vars or define them in your command.