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?
Use QUOTE function something like this untested
entrytitle %sysfunc(quote(%superq(titlemacr&i)));
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.
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 ;
...
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.
If
%bquote(&title34)
works then
%superq(title34)
should also work and so should
%superq(title&i)
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 16. 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.