BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bouz22
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
6 REPLIES 6
Kurt_Bremser
Super User

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

bouz22
Calcite | Level 5

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

Kurt_Bremser
Super User

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.

bouz22
Calcite | Level 5

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!

Kurt_Bremser
Super User

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.

bouz22
Calcite | Level 5

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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 898 views
  • 2 likes
  • 2 in conversation