Hi,
I'm quoting a list of variables to process in a data step placed inside a macro.
I can't figure out why I need to remove the second %str to make this step work inside a macro.
Since I get the ERROR: Expected close parenthesis after macro function invocation not found.
What does the macro processor return when processing?
How SAS Processes Statements with Macro Activity
%let paramcd=Alfred Louise;
%macro works();
data want;
set sashelp.class(where=(Name in ("%sysfunc(tranwrd(%sysfunc(compbl(¶mcd.)),%str( )," "))")
));
run;
%mend works;
%works;
%macro doesNotWork();
data want;
set sashelp.class(where=(Name in ("%sysfunc(tranwrd(%sysfunc(compbl(¶mcd.)),%str( ),%str(" ")))")
));
run;
%mend doesNotWork;
%doesNotWork;
- Cheers -
@Oligolas wrote:
you're both right
@PaigeMiller I'd expect SAS to recognize that the quotes inside %STR are matched?
And yet, it doesn't, because the quotes are not matched. Matched quotes indicate that what is between them is quoted, i.e. is a text string. You have a need for a double-quote character at the end of a text string, followed by a space, followed by a double-quote character to start another text string. You are not trying to quote the text between them.
As Tom says, you are passing the the %STR function text that doesn't have characters that need %STR. And so the interpretation is different.
Using quotes or double-quotes as text can get tricky inside the macro processor. You need to indicate that these are considered unmatched double-quotes, by using %" rather than just ". Now both macros work.
%let paramcd=Alfred Louise;
%macro works;
data want;
set sashelp.class(where=(Name in ("%sysfunc(tranwrd(%sysfunc(compbl(¶mcd.)),%str( )," "))") ));
run;
%mend works;
%works
%macro doesNotWork;
data want;
set sashelp.class(where=(Name in ("%sysfunc(tranwrd(%sysfunc(compbl(¶mcd.)),%str( ),%str(%" %")))")
));
run;
%mend doesNotWork;
%doesNotWork
you're both right
@Tom I sometimes pass special characters through %STR in other context and didn't expect SAS to crash here.
@PaigeMiller I'd expect SAS to recognize that the quotes inside %STR are matched?
- Cheers -
@Oligolas wrote:
you're both right
@PaigeMiller I'd expect SAS to recognize that the quotes inside %STR are matched?
And yet, it doesn't, because the quotes are not matched. Matched quotes indicate that what is between them is quoted, i.e. is a text string. You have a need for a double-quote character at the end of a text string, followed by a space, followed by a double-quote character to start another text string. You are not trying to quote the text between them.
As Tom says, you are passing the the %STR function text that doesn't have characters that need %STR. And so the interpretation is different.
Why did you add the %STR() to quote something that does not need quoting? To include quotes in %STR() you need to escape them with %.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.