Hello all,
I have a Sas 9.4 program that calls a Python script on Sas datasets. The Sas script is being used on multiple computers and for each of these computers the Python path is different so I would like to assign that dynamically.
Entering "where python" in the Windows command line gives me this result on one computer:
C:\ProgramData\Anaconda3\python.exe
With CALL SYSTEM(where python) in Sas I can produce the same result but is there a way to assign that result to a variable so I can use it in my Python call later on? So that e.g. pythonpath = "C:\ProgramData\Anaconda3\python.exe"
Many thanks!
Try this:
filename oscmd pipe 'where python';
data _null_;
infile oscmd truncover;
input result $100.;
call symput('pythonpath',trim(result));
run;
Edit: added the trim() call
Try this:
filename oscmd pipe 'where python';
data _null_;
infile oscmd truncover;
input result $100.;
call symput('pythonpath',trim(result));
run;
Edit: added the trim() call
Thank you so much, that was very fast!
It works, although I changed the code a bit because I didn't understand fully the last part of your code:
filename oscmd pipe 'where python'; data _null_; infile oscmd truncover; input result $100.; CALL SYSTEM(trim(result)); run;
This starts python in the command line window.
Thanks again, I had spent quite some time trying to find a solution through Googling but to no avail..
call symput() creates a macro variable, to be used later like
filename oscmd pipe "&pythonpath. c:\some_path\some_code.py 2>&1";
data _null_;
infile oscmd;
input;
put _infile_;
run;
All output (including stderr because of the 2>&1 redirection) will end up in the SAS log.
Thank you for the explanation, that makes sense. Having experience primarily with Python I find Sas very often still quite confusing the moment I have to do something other than PROC SQL. I've managed to get some very handy things to work but very often it's a bit trial and error..
In all honesty at this point I also wouldn't be able to explain what goes on in the first part of your code but I will try to dig a little deeper later. For now I'm very happy with your help, the code is running again with the renewed macro!
filename pipe means that what is in the physical name is run as an external command. If used as a FILE in a data step, the output from the put statement(s) is fed to the stdin of that external command. If used as an INFILE, the command is run, and the stdout is used for the input statement(s) in the data step.
_infile_ is an automatic variable that holds the complete buffer from the input statement.
Thank you for the further clarification, I think I get it now but it's not easy for me to understand. Guess I have to spend some more time with Sas to really get used to the language!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.