DATA Step, Macro, Functions and more

passing arguments to Python using X?

Occasional Contributor
Posts: 18

passing arguments to Python using X?

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;

X "C:\temp\ &optionA &optionB &optionC &optionD &optionE";


And here's


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'


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?



Super User
Posts: 10,279

Re: passing arguments to Python using X?

Try this:

X "C:\temp\ '&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.


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

Re: passing arguments to Python using X?

Posted in reply to KurtBremser

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.

Ask a Question
Discussion stats
  • 2 replies
  • 2 in conversation