DATA Step, Macro, Functions and more

Add quotation marks to string

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Add quotation marks to string

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)


Accepted Solutions
Solution
‎10-06-2016 08:32 AM
Super User
Posts: 5,082

Re: Add quotation marks to string

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


All Replies
Solution
‎10-06-2016 08:32 AM
Super User
Posts: 5,082

Re: Add quotation marks to string

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.

Respected Advisor
Posts: 3,892

Re: Add quotation marks to string

[ Edited ]

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' 

Super User
Posts: 9,681

Re: Add quotation marks to string

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;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 1587 views
  • 1 like
  • 4 in conversation