I often assign a library DESK as follows.
libname DESK '%SystemDrive%\Users\%USERNAME%\Desktop\';
And I avoid the following as SAS tries to resolve environment variables inside double quotes.
libname DESK "%SystemDrive%\Users\%USERNAME%\Desktop\";
Since there are many directories, I used macro variables and fed them to LIBNAME.
%let DESK %SystemDrive%\Users\%USERNAME%\Desktop\;
libname DESK '&DESK.';
However, SAS does not resolve macro variables inside single quotes, so I need to use double quotes instead.
libname DESK "&DESK.";
This is not safe as SAS tries to resolve both macro (ex. DESK) and environment variables (ex. SystemDrive, USERNAME). It also spits out many warning messages as byproducts. Is there any way not to resolve environment variables in macro variables while resolving? I also tried the following but failed.
libname DESK %tslit(&DESK.);
Thank you so much.
That style of %VARNAME% is a WINDOWS only construct. If you use that you are limiting your program to only be able to run on WIndows operating system.
Or you can use the %SYSGET() macro function.
Also SAS uses the exclamation point to reference environment variables in operating system commands.
filename home '%HOMEDRIVE%\%HOMEPATH%';
%put %sysfunc(pathname(home));
filename home "%sysget(HOMEDRIVE)\%sysget(HOMEPATH)";
%put %sysfunc(pathname(home));
filename home "!HOMEDRIVE\!HOMEPATH";
%put %sysfunc(pathname(home));
2452 filename home '%HOMEDRIVE%\%HOMEPATH%'; 2453 %put %sysfunc(pathname(home)); H:\ 2454 filename home "%sysget(HOMEDRIVE)\%sysget(HOMEPATH)"; 2455 %put %sysfunc(pathname(home)); H:\ 2456 filename home "!HOMEDRIVE\!HOMEPATH"; 2457 %put %sysfunc(pathname(home)); H:\
Hi @Junyong
You could use the SYSGET function to put the environment variable into a true macro variable:
%let drive=%sysfunc(sysget(SystemDrive));
Same can be done for username or use the builtin SAS macro variable &sysuserid. That should get you error free code to use inside a macro or libname double quotes:
%let drive=%sysfunc(sysget(SystemDrive));
libname desk "&drive\Users\&sysuserid\desk";
In Windows, there should also be a %USERPROFILE% environment variable you can grab which has the full path in one string, ie: C:\users\username
Hope this helps.
That style of %VARNAME% is a WINDOWS only construct. If you use that you are limiting your program to only be able to run on WIndows operating system.
Or you can use the %SYSGET() macro function.
Also SAS uses the exclamation point to reference environment variables in operating system commands.
filename home '%HOMEDRIVE%\%HOMEPATH%';
%put %sysfunc(pathname(home));
filename home "%sysget(HOMEDRIVE)\%sysget(HOMEPATH)";
%put %sysfunc(pathname(home));
filename home "!HOMEDRIVE\!HOMEPATH";
%put %sysfunc(pathname(home));
2452 filename home '%HOMEDRIVE%\%HOMEPATH%'; 2453 %put %sysfunc(pathname(home)); H:\ 2454 filename home "%sysget(HOMEDRIVE)\%sysget(HOMEPATH)"; 2455 %put %sysfunc(pathname(home)); H:\ 2456 filename home "!HOMEDRIVE\!HOMEPATH"; 2457 %put %sysfunc(pathname(home)); H:\
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.