DATA Step, Macro, Functions and more

Assign the result of a Windows command line entry to a Sas variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Assign the result of a Windows command line entry to a Sas variable

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!


Accepted Solutions
Solution
Wednesday
Super User
Posts: 10,278

Re: Assign the result of a Windows command line entry to a Sas variable

[ Edited ]

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

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
Wednesday
Super User
Posts: 10,278

Re: Assign the result of a Windows command line entry to a Sas variable

[ Edited ]

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

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 6

Re: Assign the result of a Windows command line entry to a Sas variable

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

Super User
Posts: 10,278

Re: Assign the result of a Windows command line entry to a Sas variable

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 6

Re: Assign the result of a Windows command line entry to a Sas variable

Posted in reply to KurtBremser

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!

Super User
Posts: 10,278

Re: Assign the result of a Windows command line entry to a Sas variable

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 6

Re: Assign the result of a Windows command line entry to a Sas variable

Posted in reply to KurtBremser

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!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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