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-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
  • 2 replies
  • 1397 views
  • 0 likes
  • 2 in conversation