Here is the issue;
At work, due to some changes in IT rules, colleagues have differently mapped online folders. This is an issue with shared projects that use those folders as libraries, so I am trying to define this dynamically. The idea is that when you run the project, you get a prompt for which user is running the query and then assign correct path to the program.
But the issue is since there are multiple folders that I need to use with different paths. I was thinking of setting a dummy variable and then assign the actual path strings based on an if/then statement. What I cannot find is how to properly assign value to a macro variable with an if-then.
Are you asking how to set the value of a macro variable? You can use the %LET macro statement.
%let mvar=new value;
Or you could use the CALL SYMPUTX() statement in a data step.
data _null_;
call symputx('mvar','new value');
run;
To do it conditionally you could use conditional macro code:
%if &username=FRED %then %do;
%let mvar=new value;
%end;
Or conditional data step code:
data _null_;
if "&username"="FRED" then do;
call symputx('mvar','new value');
end;
run;
But for something so complex you might want to make a dataset that contains the mapping
data mapping;
infile datalines dsd truncover ;
input username :$32. value :$200.;
datalines;
FRED,new value
JOE,old value
;
and then use that. Either with a data step:
data _null_;
set mapping;
where username="&username";
call symputx('mvar',value);
run;
Or perhaps PROC SQL
proc sql noprint;
select value into :mvar trimmed
from mapping
where username="&username"
;
quit;
Or you might want to make a FORMAT that maps the username into the right value.
proc format;
value $mapping
'FRED'='new value'
'JOE'='old value'
;
run;
Which you could then use with the PUT() or PUTC() function.
%let mvar=%sysfunc(putc(&username,$mapping.));
Are you asking how to set the value of a macro variable? You can use the %LET macro statement.
%let mvar=new value;
Or you could use the CALL SYMPUTX() statement in a data step.
data _null_;
call symputx('mvar','new value');
run;
To do it conditionally you could use conditional macro code:
%if &username=FRED %then %do;
%let mvar=new value;
%end;
Or conditional data step code:
data _null_;
if "&username"="FRED" then do;
call symputx('mvar','new value');
end;
run;
But for something so complex you might want to make a dataset that contains the mapping
data mapping;
infile datalines dsd truncover ;
input username :$32. value :$200.;
datalines;
FRED,new value
JOE,old value
;
and then use that. Either with a data step:
data _null_;
set mapping;
where username="&username";
call symputx('mvar',value);
run;
Or perhaps PROC SQL
proc sql noprint;
select value into :mvar trimmed
from mapping
where username="&username"
;
quit;
Or you might want to make a FORMAT that maps the username into the right value.
proc format;
value $mapping
'FRED'='new value'
'JOE'='old value'
;
run;
Which you could then use with the PUT() or PUTC() function.
%let mvar=%sysfunc(putc(&username,$mapping.));
Amazing answer, thank you.
Generally, for the time constraints and current needs, I think the
%if &username=FRED %then %do;
%let mvar=new value;
%end;will work best, as we are only looking at 2 different paths for 3 different users.
But hopefully, number of people with similar but not same issue will find this answer helpful as well.
Nearly 200 sessions are now available on demand in the Innovate Hub.
Watch Now →Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.