BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Junyong
Pyrite | Level 9

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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:\

View solution in original post

3 REPLIES 3
Astounding
PROC Star
Can you retrieve your environmental variables using some other character such as a dollar sign? Why are you locked in to using macro language triggers?
DaveHorne
SAS Employee

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.

Tom
Super User Tom
Super User

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:\

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 1123 views
  • 2 likes
  • 4 in conversation