- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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:\
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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:\