0

I am trying to run a SAS program on linux using python. This server has SAS installed (version 9.4). And python version I have is 3.7.3.

I learned there is a library saspy which I could use however my project does not have access to this library. So, I found another alternative, my intension is to invoke the sas program in python and get the return code when it is successfully completed. This Sas program usually takes 1hr to complete.

So, I would like to use the return code when it is successful and later would like to notify myself through an email. I wrote the below code (sample) and I unable to make the subprocess work ? Any help is appreciated. Thanks

#!usr/bin/python 
import os
import subprocess
import time 
**My function begins here** 
def Trigger_func(T):
    if T == 1:
       start = time.time() /** want to start a timer to know how long it takes to complete*/
       #cmd = os.system('BGsas -p path/Sample.sas') /** SAS code is being invoked properly if used in this way */
       sas_script = 'path/Sample.sas' /* Just a variable for the sas code path */
       cmd2 = f'BGsas -p {sas_script}'
       result = subprocess.call(cmd2, shell=True) /* if I use this subprocess to call the sas code it is not working, I am getting the return code <> 0 **/
       if result == 0: 
             stop = time.time() - start
             return [1, stop]
       else:
            stop = time.time() - start 
            return [0, stop]


   """""""" 
   When the above process is completed, I will use this success return code to notify myself through 
   an email
   """"""""""""

    
4
  • So you are trying to run your SAS program by calling SAS from the command line? I cannot tell what actual command you are running. Make sure the SAS command works when you run it from your terminal. Make sure you can run any other operating system commands from your python program. Commented Jan 26, 2022 at 13:56
  • Hey Tom, I am running on command line using nohup python3 <python-script> and let it run. But I noticed when I use the subprocess it is not working as expected in my case. I want something that lets me know when that sas program got executed completed and provides me some return code so I can use it for further operation. However, in my case the subprocess is returning code as zero even though the sas program is still running in the background. Commented Jan 26, 2022 at 15:36
  • I tried using both subprocess.call and subprocess.run Commented Jan 26, 2022 at 15:45
  • But does the command to run SAS that you are trying to get Python to call actually work when you run it at the command line? If the command doesn't work then python cannot get it to work. And if that command returns immediately to the command line without waiting for the SAS job to finish then you cannot get the python program to wait either. Commented Jan 26, 2022 at 16:36

2 Answers 2

1

subprocess.call is an older method of doing this, but it should work; per the documentation, you need to use the returncode attribute to access the return code.

You may be better off using subprocess.run(), which returns a CompletedProcess instance.

Either way, you probably should ensure that your shell command (which looks like a .bat file) actually returns the value from the SAS execution. If it uses call, for example (in Windows batch script), it may actually be running SAS in the background - which is consistent with what you're describing here, and also consistent with the filename (BGsas). You may want to use a different script to launch SAS in the foreground.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for suggestion Joe. I will test with subprocess.run() again. When you said different script to launch SAS, Could you elaborate ? Right now I have SAS script as sample.sas, then using Main.py as the python script to invoke this sample.sas. To run this for testing I am running on command line. Thanks
sample.sas is the name of your sas program. To get SAS to run your SAS code you have to execute SAS itself. Joe is talking about the command you are running that is actually going tell SAS to run that program. He is describing it as a script because it does not look like you are calling the SAS executable directly.
cmd2 = f'BGsas -p {sas_script}' That - BGsas is a script (most likely, or something else that launches SAS). Not the SAS program itself.
0

I embedded the sas program in shell script and then invoked in python using p = subprocess.run(['sh', './path/shellscript.sh']) and used the p.returncode for further operations.

I see BGsas is not working as intended in my case. Thanks Joe and Tom for your time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.