I'm looking for assistance making a call to Python scripts (from SAS) while passing arguments.
Here's some SAS code.
%LET optionA = C:\temp\some_filename.csv;
%LET optionB = some text with spaces;
%LET optionC = text_with_underscores;
%LET optionD = ; /* an empty value */
%LET optionE = 15;
OPTIONS XWAIT;
X "C:\temp\test.py &optionA &optionB &optionC &optionD &optionE";
And here's test.py
import sys optionA = sys.argv[1] optionB = sys.argv[2] optionC = sys.argv[3] optionD = sys.argv[4] optionE = sys.argv[5] print 'optionA: ' + optionA print 'optionB: ' + optionB print 'optionC: ' + optionC print 'optionD: ' + optionD print 'optionE: ' + optionE print 'Press Enter to return to SAS' raw_input()
Running the SAS code results in the Python script executing:
As you can see, optionB ends up being communicated as four separate one-word arguments.
Could someone please point out if there's a universal way of handling this, so each argument can contain any permissible combination of letters, numbers, and symbols, and is still treated as a single argument? I suppose some kind of escaping could be used, or maybe there's a completely different approach?
Thanks!
Try this:
X "C:\temp\test.py '&optionA' '&optionB' '&optionC' '&optionD' '&optionE'";
Because you have double quotes on the "outside", the single quotes contained "inside" do not prevent the resolution of macro variables.
Thanks Kurt.
Here's what I get now:
But I think you've pointed me in the right direction. I now just need to find a way in Python to parse arguments at single-quotes rather than spaces, which shouldn't be a problem.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.