BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Henry
Calcite | Level 5

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:

 

data abc_101;
a=3;
run;
%macro test1(id= );
%put abc_&id.;
data a;
set abc_&id.;
run;
%mend test1;
%test1(id=%str(101));
 
MLOGIC(TEST1): Beginning execution.
MLOGIC(TEST1): Parameter ID has value 101
MLOGIC(TEST1): %PUT abc_&id.
abc_101
MPRINT(TEST1): data a;
MPRINT(TEST1): set abc_101;
ERROR: File WORK.ABC_.DATA does not exist.
MPRINT(TEST1): run;
 
SAS log shows error that no WORK.ABC_.DATA found. It seems like there is a blank in the front of &id which cause SAS cannot find ABC_ data. Why %put statement shows correct string, but set statement cannot. I also tried different solutions below:
 
%macro test2(id= );
%let id=%trim(&id);
%put abc_&id.;
data a;
set abc_&id.;
run;
%mend test2;
%test2(id=%str(101));
 
%macro test3(id= );
%put abc_&id.;
data a;
set abc_&id.;
run;
%mend test3;
%test3(id=101);
 
Both works. I use %str function all the time to pass macro parameters, but now I am kind of confused. Does any experts can help me explain why we need to use %trim in the macro program or not use %str function. Thank you so much. 
 
 
 
 
 
 
 
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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. ;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

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. ;
PaigeMiller
Diamond | Level 26

The string 101 does not need to be enclosed by %STR()

 

%STR() is only needed for certain special characters, as stated in the documentation.

--
Paige Miller
Quentin
Super User

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:

https://documentation.sas.com/?docsetId=mcrolref&docsetTarget=p1f5qisx8mv9ygn1dikmgba1lmmu.htm&docse...

 

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.

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1193 views
  • 1 like
  • 4 in conversation