2

I have a SAS script from which I need to launch a Python script. I need the SAS script to find the first path to a "python.exe" file from the PATH environment variable, rather than hardcoding a path to a particular python installation.

This is the code I am attempting to use:

%let progdir = C:\Temp\scripts;
%let pypath = C:\Temp\pypath.txt;

x "if exist &pypath (del &pypath /Q)";
x "for %i in (python.exe) do @echo.%~$PATH:i >> &pypath"; run;

data null; infile "&pypath" length=reclen obs=1;
    input location $varying254. reclen;
    call symput('runpython', trim(location));
    run;

x "%bquote(&runpython) &progdir./gtfs_collapse_routes.py";
x "if exist &pypath (del &pypath /Q)"; run;

The second x call seems to be executed correctly, as the &pypath file is created with the expected contents. However, this code is resulting in a SAS warning that I would like to eliminate:

WARNING: Apparent invocation of macro I not resolved.

This is obviously a reference to the %i in the second x call, but I am at a loss as to how to avoid this warning. (I originally wanted to get the python path using x "where python >> &pypath", but for unknown reasons that was failing to create a file at all even though that command works when run from cmd.exe.)


After a helpful prod in the right direction from @Joe, this is my updated, working code for obtaining the python path without a warning:

%let command = %nrstr(for %i in (python.exe) do @echo.%~$PATH:i);
x "&command >> &pypath"; run;
2
  • How many Python installations do you have? Commented May 12, 2017 at 21:46
  • @Parfait around 5 or 6 currently, from various software packages and Anaconda environments. Commented May 12, 2017 at 22:17

1 Answer 1

2

Double quotes in SAS are used when you want to be able to resolve macro variables. Single quotes on the other hand prevent the resolution of macro variables - and additionally prevent SAS from trying to resolve things that look like macro variables, as in this case.

x '... stuff ... ';

You could also use macro quoting (%NRSTR() and such) if needed, though I think here regular single quotes will suffice for what you need, unless you're not telling us something.

Sign up to request clarification or add additional context in comments.

2 Comments

This pointed me in the right direction. Developing mostly in python, I always forget that single/double quotes aren't interchangeable everywhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.