data_weed
data_weed

Reputation: 19

How to utilize subprocess when invoking sas code in python on Linux server

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
   """"""""""""

    

Upvotes: 0

Views: 482

Answers (2)

data_weed
data_weed

Reputation: 19

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.

Upvotes: 0

Joe
Joe

Reputation: 63424

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.

Upvotes: 1

Related Questions