Let's say I have macro variable &mvar that has a list of values in it (aaaaa bbbbb ccccc ... zzzzz). I need to surround each of these values with quotation marks so that I can use them later in my program as a list of possible character variable values (i.e., "if var1 in (&mvar) then X"). Is there a way to do this programatically, or am I stuck doing it by hand? (Using SAS Enterprise Guide 7.11)
You're in luck. Someone else asked the same question earlier today.
https://communities.sas.com/t5/SAS-Procedures/Adding-Single-Quote-to-list-of-strings/m-p/302599
Also note, you don't have to necessarily break out the strings individually. You could also use:
if index(" &mvar ", ' ' || strip(var1) || ' ') then do;
You will need the leading and trailing blanks to make the comparison produce the right result.
You're in luck. Someone else asked the same question earlier today.
https://communities.sas.com/t5/SAS-Procedures/Adding-Single-Quote-to-list-of-strings/m-p/302599
Also note, you don't have to necessarily break out the strings individually. You could also use:
if index(" &mvar ", ' ' || strip(var1) || ' ') then do;
You will need the leading and trailing blanks to make the comparison produce the right result.
To create the macro variable with quotes in the same place than the macro variable with blanks is likely the "best" approach but shouldn't this be possible for some reason then below code will do the job as well.
%let mvar=aaaaa bbbbb ccccc zzzzz;
%let mvar_quotes=%nrbquote(')%qsysfunc(prxchange(s/\s+/%nrbquote(',')/oi,-1,&mvar))%nrbquote(');
%put mvar_quotes: %bquote(&mvar_quotes);
SAS Log:
mvar_quotes: 'aaaaa','bbbbb','ccccc','zzzzz'
Or maybe you don't need add quote around them, use SYMGET() instead. %let list=F M; data x; set sashelp.class; if findw(symget('list'),strip(sex)); run;
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.