%macro nmtest(a1=);
data class_&a1.;
set sashelp.class;
run;
%mend nmtest;
%nmtest(a1=%str(bb));
Your use of %str() is not needed here.
%macro nmtest(a1=);
data class_&a1.;
set sashelp.class;
run;
%mend nmtest;
%nmtest(a1=bb);
This produces only one data set.
If you turn on macro debugging ... via this command
options mlogic;
and then re-run your code, you will see in the log that &a1 has additional characters introduced by the unnecessary %str( )
118 options mlogic; 119 %macro nmtest(a1=); 120 data class_&a1.; 121 set sashelp.class; 122 run; 123 124 %mend nmtest; 125 %nmtest(a1=%str(bb)) MLOGIC(NMTEST): Beginning execution. MLOGIC(NMTEST): Parameter A1 has value bb SYMBOLGEN: Macro variable A1 resolves to bb SYMBOLGEN: Some characters in the above value which were subject to macro quoting have been unquoted for printing. MPRINT(NMTEST): data class_bb; MPRINT(NMTEST): set sashelp.class; MPRINT(NMTEST): run;
Perhaps @Astounding might be able to explain in more detail
Thanks. I was only expecting class_bb to be generated and not class_ .
The %str function inserts a special macro token wich causes the dataset name to appear as two names for the data step compiler.
@Quentin wrote:
Your expectation is correct. It's a small bug that has been around for a long time that sometimes invisible quoting characters can cause problems for the compiler when SAS fails to automagically remove them. As Tom showed, in those cases explicitly %unquoting() the value is usually a solution.
But not using %str() in the first place also eliminates the problem. Since &a1 is going to be part of a data set name, there's no need for %str() at all, there cannot be special characters in the data set name that %str() has to mask.
The macro quoting is confusing the parser. The macro quoting on the macro variable causes the macro processor to see that as two tokens which causes the SAS processing to also see it as two.
You can remove the quoting using %UNQUOTE() macro function.
248 %macro nmtest(a1=); 249 250 data %unquote(class_&a1.); 251 set sashelp.class; 252 run; 253 254 %mend nmtest; 255 options mprint; 256 %nmtest(a1=%str(bb)); MPRINT(NMTEST): data class_bb; MPRINT(NMTEST): set sashelp.class; MPRINT(NMTEST): run; NOTE: There were 19 observations read from the data set SASHELP.CLASS. NOTE: The data set WORK.CLASS_BB has 19 observations and 5 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.