Hello, i have list of name of stores. i take every time one of the names(put the name to macro with call symputx) and cut other data with the name. the problem that some names has double quotes inside name.
example :
namestore ind
aaaa 1
b("bb b") 2
cc 3
d_"dd_d" 4
thank you
Assuming you want to keep it exactly as it is, then you can use %nrstr() to create the macro variables.
To resolve it, you can use %unquote and use single quotes.
*Create sample data;
data have;
length namestore $12.;
namestore="aaaa";ind=1;output;
namestore='b("bb b")';ind=2;output;
namestore="cc";ind=3;output;
namestore='d_"dd_d"';ind=4;output;
run;
*Create macro variables;
data _null_;
set have;
call symputx(catt('var', put(_n_, 2. -l)), %nrstr(namestore));
run;
*display macro variables in log for testing;
%put &var1.;
%put &var2.;
%put &var3.;
%put &var4.;
*Check that it resolves properly and filters properly;
data want;
set have;
where namestore=%unquote(%str(%')&var2%str(%'));
run;
Using %nrstr() brings no benefits here.
This works just as well:
*Create sample data;
data HAVE;
length NAMESTORE $12.;
NAMESTORE="aaaa" ;IND=1;output;
NAMESTORE='b("bb b")';IND=2;output;
NAMESTORE="cc" ;IND=3;output;
NAMESTORE='d_"dd_d"' ;IND=4;output;
run;
*Create macro variables;
data _null_;
set HAVE;
call symputx(cats('var', _N_ ), NAMESTORE);
run;
*display macro variables in log for testing;
%put &var1.;
%put &var2.;
%put &var3.;
%put &var4.;
data WANT;
set HAVE;
where NAMESTORE=%unquote(%str(%')&var2%str(%'));
run;
For weird macro values, I prefer not to resolve them in the code. This is safer:
data WANT;
set HAVE;
where NAMESTORE=%unquote(%str(%')%superq(var2)%str(%'));
run;
Quote function, then you make no assumptions about the kind of type(single/double) quote you are quoting.
where namestore=%sysfunc(quote(%superq(var2)));
@ChrisNZ wrote:
Using %nrstr() brings no benefits here.
This works just as well:
*Create sample data; data HAVE; length NAMESTORE $12.; NAMESTORE="aaaa" ;IND=1;output; NAMESTORE='b("bb b")';IND=2;output; NAMESTORE="cc" ;IND=3;output; NAMESTORE='d_"dd_d"' ;IND=4;output; run; *Create macro variables; data _null_; set HAVE; call symputx(cats('var', _N_ ), NAMESTORE); run; *display macro variables in log for testing; %put &var1.; %put &var2.; %put &var3.; %put &var4.; data WANT; set HAVE; where NAMESTORE=%unquote(%str(%')&var2%str(%')); run;
For weird macro values, I prefer not to resolve them in the code. This is safer:
data WANT; set HAVE; where NAMESTORE=%unquote(%str(%')%superq(var2)%str(%')); run;
SYMGET() ?
data want;
set have;
where namestore=symget('var2');
run;
My question is going to be why would you want to put "data" items in macro variables? Macro programming is for the generation of Base SAS code, Base SAS is for the manpiulation and processing of data. Forcing Macro to try to do data processing will result in hard to maintain/read and obfuscated code for no benefit. You already have a dataset with your parameters in, just use that dataset as a lookup - you will have heard of things like merging/joining, exists() function for SQL etc.
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.