BookmarkSubscribeRSS Feed
desertsp
Obsidian | Level 7

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:

 

python_screen.png

 

 

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!

2 REPLIES 2
Kurt_Bremser
Super User

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.

 

desertsp
Obsidian | Level 7

Thanks Kurt.

 

Here's what I get now:

python_screen2.png

 

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-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1772 views
  • 0 likes
  • 2 in conversation