BookmarkSubscribeRSS Feed
AlexeyS
Pyrite | Level 9

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

 

5 REPLIES 5
Reeza
Super User

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;
ChrisNZ
Tourmaline | Level 20

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;
     

 

data_null__
Jade | Level 19

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;
     

 


 

Ksharp
Super User

SYMGET() ?

 

data want;
set have;
where namestore=symget('var2');
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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. 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 1126 views
  • 10 likes
  • 6 in conversation