DATA Step, Macro, Functions and more

Macro execution when macro variables contain special characters

Reply
Occasional Contributor
Posts: 18

Macro execution when macro variables contain special characters

Hi all,

I'm having a problem with a macro I'm trying to write to display titles in a sas template

the macro goes as follows:

Step one: compile macro variables

proc sql;

select count(distinct titles)

     into :n

     from bigset;

select distinct titles

     into :%nrstr(titlemacr1) - :%nrstr(titlemacr%left(&n))

     from bigset;

quit;

Step II:  Execute a macro to generate graphs with a unique title as follows:

%macro titleloop;

%do i=1 %to &n;

proc template;

...

...

     entrytitle %sysfunc(%nrquote("&&titlemacr&i");

...

...

end;

run;

%end;

%mend;

%titleloop;

The problem i'm having is that the entrytitle statement will not resolve if the string which &&titlemacr&i resolves to contains any quotes.  For example, if titlemacr resolves to ABCD, the macro is fine.  if titlemacr resolves to ABC"D the macro generates an error and does not work.

Am I going about the sysfunc/nrquote functions incorrectly?

Respected Advisor
Posts: 3,799

Re: Macro execution when macro variables contain special characters

Use QUOTE function something like this untested

entrytitle %sysfunc(quote(%superq(titlemacr&i)));

Occasional Contributor
Posts: 18

Re: Macro execution when macro variables contain special characters

Posted in reply to data_null__

That didn't quote work -- I still receive an error along the lines of 'the function quote referenced by %sysfunc has too many arguments

The macro variable in question has a double quotes attached to two numbers followed by blank space.  It seems like the function tries to resolve when it sees that quote, and that's where the issue beings.

Super User
Super User
Posts: 7,070

Re: Macro execution when macro variables contain special characters

You should check that PROC TEMPLATE handles these characters properly by creating an example without macro logic or macro variables.  For example if TITLEMACR1 contains ABC"D then you would want to test if

   entrytitle "ABC""D" ;

works.

Note: You can simplify the SQL that is generating the macro variables.  You can also put the call to QUOTE() function there and avoid issues with macro variable expansions.

proc sql noprint ;

  select distinct quote(trim(titles))

     into :titlemacr1 - :titlemacr99999

     from bigset

  ;

%let n=&sqlobs ;

quit;


...

   entrytitle &&title1macr&i ;

...

Occasional Contributor
Posts: 18

Re: Macro execution when macro variables contain special characters

Still not quite working, however the template does resolve if there are quotes within quotes, e.g. entrytitle "ABC""D" does resolve.

Further, if i try out the following:

entrytitle %sysfunc(quote(%bquote(&title34)));

the macro resolves, too.  it apepars that the double pass of the macro creates an issue.

Super User
Super User
Posts: 7,070

Re: Macro execution when macro variables contain special characters

If

%bquote(&title34)

works then

%superq(title34)

should also work and so should

%superq(title&i)

Ask a Question
Discussion stats
  • 5 replies
  • 219 views
  • 0 likes
  • 3 in conversation