DATA Step, Macro, Functions and more

double quotes

Reply
Contributor
Posts: 61

double quotes

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

 

Super User
Posts: 19,815

Re: double quotes

[ Edited ]

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;
PROC Star
Posts: 1,760

Re: double quotes

[ Edited ]

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;
     

 

Respected Advisor
Posts: 3,799

Re: double quotes

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;
     

 


 

Super User
Posts: 10,035

Re: double quotes

SYMGET() ?

 

data want;
set have;
where namestore=symget('var2');
run;
Super User
Super User
Posts: 7,970

Re: double quotes

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. 

Ask a Question
Discussion stats
  • 5 replies
  • 398 views
  • 10 likes
  • 6 in conversation