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

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)

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

3 REPLIES 3
Astounding
PROC Star

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.

Patrick
Opal | Level 21

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' 

Ksharp
Super User
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;

sas-innovate-2024.png

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.

 

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
  • 26313 views
  • 4 likes
  • 4 in conversation