2
votes

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;
1
How many Python installations do you have?Parfait
@Parfait around 5 or 6 currently, from various software packages and Anaconda environments.nmpeterson

1 Answers

2
votes

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.