Dear SAS experts,
I use SAS for many years, but recently I meet following issue related to %str and cannot explain. Please see my SAS codes below:
The macro quoting is confusing the SAS parser into seeing multiple tokens instead of one token.
You can add an call to the %unquote() macro function.
set %unquote(abc_&id.);
Or perhaps easier you can build the name first and then use it.
%let dsname=abc_&id.;
set &dsname. ;
The macro quoting is confusing the SAS parser into seeing multiple tokens instead of one token.
You can add an call to the %unquote() macro function.
set %unquote(abc_&id.);
Or perhaps easier you can build the name first and then use it.
%let dsname=abc_&id.;
set &dsname. ;
The string 101 does not need to be enclosed by %STR()
%STR() is only needed for certain special characters, as stated in the documentation.
Tom already gave the correct answer: you need to %unquote() it.
Just to add, I believe this is a bug (though I'm not sure SAS views it as such). The value should be %unquoted automatically before it causes problems, but it doesn't always happen. The rule I learned is that if you turn on MPRINT, and the code in the log looks valid while generating an error, try %UNQUOTE. The SAS docs mention this:
What to Do When Automatic Unquoting Does Not Work
... In rare cases, masking text with a macro quoting function changes how the word scanner tokenizes the text. (The word scanner and tokenization are discussed in SAS Programs and Macro Processing and and Macro Processing.) For example, a single or double quotation mark produced by resolution within the %BQUOTE function becomes a separate token. The word scanner does not use it as the boundary of a literal token in the input stack. If generated text that was once masked by the %BQUOTE function looks correct but SAS does not accept it, you might need to use the %UNQUOTE function to restore normal tokenization. (emphasis added)
When you use %TRIM the code works because %TRIM unquotes it. As would %UPCASE, or other macro functions that don't explicitly quote the value they return.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.