Mask special character in macro value

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Mask special character in macro value

Hi all,

 

I'm looping through unique values of a variable that I use to create charts using call symput. But some of the values contain special characters like apostrophe and that's where I run into issues. How do I tell SAS to mask any special characters stored in my macro value? Below is my sample code (and the red text is where i get into trouble when the custname value is, for example, "CUST'A":

 

 

data _null_;
set topcust1;
call symput('custname'||left(_n_),strip(custname));
call symput('stop',left(_n_));
run;


/*CREATE CHARTS*/

%macro createplots;
%do i=1 %to 10;title1 J=C font="&font/bold" H=13pt "PRICE CHART BY CUSTOMER: %upcase(&&custname&i.))";


proc gplot data=data1 (where=(custname="&&custname&i."));
plot price*date = group/ 
skipmiss
haxis=axis1 
vaxis=axis2 
run;quit;
%end;
%mend;



ods _all_ close; 
ods listing; 
ods pdf file="&outpath\&file.";
%createplots;
ods pdf close;
quit;

 


Accepted Solutions
Solution
2 weeks ago
PROC Star
Posts: 1,076

Re: Mask special character in macro value

Use %bquote function to mask the resolved macro var value

View solution in original post


All Replies
Solution
2 weeks ago
PROC Star
Posts: 1,076

Re: Mask special character in macro value

Use %bquote function to mask the resolved macro var value

Super User
Super User
Posts: 7,752

Re: Mask special character in macro value

[ Edited ]

Here is one way that the data step which generates the macro variables can immediately re-create them with macro quoting.

data _null_;
  if eof then call symputx('stop',_n_-1);
  set topcust1 end=eof;
  mvar=cats('custname',_n_);
  call symputx(mvar,custname);
  call execute(catx(' ','%let',mvar,'=%superq(',mvar,');'));
run;

Although in your situation why not just add real quotes?

data _null_;
  if eof then call symputx('stop',_n_-1);
  set topcust1 end=eof;
  call symputx(cats('custname',_n_),quote(trim(custname),"'"));
run;
...
(where=(custname=&&custname&i))
...
☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 115 views
  • 0 likes
  • 3 in conversation