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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.