BookmarkSubscribeRSS Feed
SPat2019
Calcite | Level 5

How to invoke python script in SAS

14 REPLIES 14
SASKiwi
PROC Star

What version of SAS are you using as this will dictate how you use Python.

SPat2019
Calcite | Level 5

I am SAS EG 7.15

Tom
Super User Tom
Super User

@SPat2019 wrote:

I am SAS EG 7.15


The question was what version of SAS are you running. Not the version number of the tool you are using to submit the SAS code.

SPat2019
Calcite | Level 5

I am using version - 7.15 (7.100.5.5850) (64-bit)

Tom
Super User Tom
Super User

@SPat2019 wrote:

I am using version - 7.15 (7.100.5.5850) (64-bit)


There is no version 7 of SAS.

Run this code to see the version of SAS you are using.

%put &sysvlong;

See how old a version you have by checking the release date on this site.

https://blogs.sas.com/content/iml/2013/08/02/how-old-is-your-version-of-sas-release-dates-for-sas-so...

 

PS I believe that Version 7 of Enterprise Guide is also very old.

SPat2019
Calcite | Level 5

9.04.01M7P080620

Tom
Super User Tom
Super User

You are not going to be able to run PROC PYTHON using that version of SAS.  That would require using VIYA instead of SAS.

 

Check the setting of the XCMD option to see whether you can run Python commands by calling the operating system command to run python from within your SAS session by using the X or SYSTEM commands or by using the PIPE filename engine.

 

471  %put %sysfunc(getoption(xcmd));
XCMD

If your session is running with NOXCMD set instead then you will not be able to run an operating system command.

 

If  XCMD is enabled try a simple program to call whatever command line program you want. (Remember it will run on the machine where SAS itself is running, not the machine you are using to run Enterprise Guide.

data _null_:
  infile "python3 myfile.py" pipe;
  input;
  put _infile_;
run;

Note there are other ways to reference SharePoint than using Python.

https://blogs.sas.com/content/sasdummy/2020/07/09/sas-programming-office-365-onedrive/

SPat2019
Calcite | Level 5

I am getting when i run --  %put %sysfunc(getoption(xcmd));

 

 

GOPTIONS NOACCESSIBLE;
71 %LET _CLIENTTASKLABEL=;
72 %LET _CLIENTPROCESSFLOWNAME=;
73 %LET _CLIENTPROJECTPATH=;
74 %LET _CLIENTPROJECTPATHHOST=;
75 %LET _CLIENTPROJECTNAME=;
76 %LET _SASPROGRAMFILE=;
77 %LET _SASPROGRAMFILEHOST=;
78
79 ;*';*";*/;quit;run;
80 ODS _ALL_ CLOSE;
81
82
83 QUIT; RUN;

Tom
Super User Tom
Super User

@SPat2019 wrote:

I am getting when i run --  %put %sysfunc(getoption(xcmd));

 

 

GOPTIONS NOACCESSIBLE;
71 %LET _CLIENTTASKLABEL=;
72 %LET _CLIENTPROCESSFLOWNAME=;
73 %LET _CLIENTPROJECTPATH=;
74 %LET _CLIENTPROJECTPATHHOST=;
75 %LET _CLIENTPROJECTNAME=;
76 %LET _SASPROGRAMFILE=;
77 %LET _SASPROGRAMFILEHOST=;
78
79 ;*';*";*/;quit;run;
80 ODS _ALL_ CLOSE;
81
82
83 QUIT; RUN;


That log does not show any attempt to run the %PUT statement.

 

If you want to run commands from your SAS session you will need to ask the people that support your SAS server whether or not those commands can run on that server.  You can ask them at the same time whether or not they have enabled the XCMD option for the SAS Session you are using from Enterprise Guide.   If they haven't then it doesn't matter whether or not you can run python on that machine, since you won't be able to run it from SAS.  But they might have another SAS server you can connect to that will allow you do what you want.

SPat2019
Calcite | Level 5

reran - now i am getting 

 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73 %put %sysfunc(getoption(xcmd));
XCMD
74
75 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
87
 
Tom
Super User Tom
Super User

Now you are all set.

You just need to figure out the hard part, getting python code to access sharepoint.  Then the final hurdle will be calling that python code from your SAS session.  

SPat2019
Calcite | Level 5

I already have python script, to call python script i can use below code?

 

data _null_:
  infile "python3 myfile.py" pipe;
  input;
  put _infile_;
run;

 

Tom
Super User Tom
Super User

Whatever command gets the code to run on the computer were SAS is running is what goes inside the quotes.  Note that the code needs to run without user input.  That example data step will echo to the SAS log any lines of text that running the python program generates to the console/terminal window.

 

If your Enterprise Guide session is connecting to SAS running on your local Windows machine then it will be easier to test the command from the command prompt on your machine before trying to call it from SAS.

 

If you are using SAS running on some remote server then you need to know what command on that server will let you run python code.  That command is normally python3, but whether or not it exists on that machine and what exactly is the command to call python depends on how that machine was setup.  Note that you should really discuss this with your local SAS support team as they will know what your SAS environment actually is and whether or not python is available for you to use.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 14 replies
  • 2542 views
  • 2 likes
  • 4 in conversation