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

Hello, I am learning macro quoting functions and I got some questions. Hope some of you expert can help 🙂

 

Below is the code I ran:

 

options mlogic mprint;
%macro translate (var);
%if &var = %str(EQ) %then %put The result is correct;
%else %put The result is incorrect;
%mend;
%translate(EQ)

The mlogic shows the condition is false :

 

MLOGIC(TRANSLATE): Beginning execution.
MLOGIC(TRANSLATE): Parameter VAR has value EQ
MLOGIC(TRANSLATE): %IF condition &var = EQ is FALSE
MLOGIC(TRANSLATE): %PUT The result is incorrect
The result is incorrect
MLOGIC(TRANSLATE): Ending execution.
 
I'm assuming this is because the &var needs to masked as well in the %IF condition. Because I want to mask the resolved value, I used the %bquote quoting function:
 
%macro translate (var);
%if %bquote(&var) = %str(EQ) %then %put The result is correct;
%else %put The result is incorrect;
%mend;
%translate(EQ)
It works which is great:
 
MLOGIC(TRANSLATE): Beginning execution.
MLOGIC(TRANSLATE): Parameter VAR has value EQ
MLOGIC(TRANSLATE): %IF condition %bquote(&var) = EQ is TRUE
MLOGIC(TRANSLATE): %PUT The result is correct
The result is correct
MLOGIC(TRANSLATE): Ending execution.
 
And now, I'm just doing a quick experiment and I used the %str instead of the %bquote function:
 
%macro translate (var);
%if %str(&var) = %str(EQ) %then %put The result is correct;
%else %put The result is incorrect;
%mend;
%translate(EQ)
It also works. 
 
MLOGIC(TRANSLATE): Beginning execution.
MLOGIC(TRANSLATE): Parameter VAR has value EQ
MLOGIC(TRANSLATE): %IF condition &var = EQ is TRUE
MLOGIC(TRANSLATE): %PUT The result is correct
The result is correct
MLOGIC(TRANSLATE): Ending execution.
 
So my question is, why does the %str works when I use it on the &var. From what I understand, the %str masks the value in compile time where the &var value is not resolved. So when it comes the execution time, the resolved value (which is EQ) should not be masked and this should cause %IF condition to fail.
 
Could anyone explain why the %str function works in this example?
 
 
 

 

 

1 ACCEPTED SOLUTION
6 REPLIES 6
Ksharp
Super User

options mlogic mprint;
%macro translate (var);
%if %bquote(&var) = %str(EQ) %then %put The result is correct;
%else %put The result is incorrect;
%mend;
%translate(EQ)

Ksharp
Super User

%str mask value in complie stage, %bquote mask value in execute stage.

compile stage is BEFORE execute stage.

Therefore BOTH worked.

kisumsam
Quartz | Level 8

Thanks for your response KSharp. I'm not sure if I followed tho. I thought the %str masks the value before it is resolved and it won't mask the resolved value. That's why we needed the bquote.

 

Below is an example that I read from another Sesug article:

 

 

%Let A = X,X ;
%Let B = Y%Str(&A)Y ;
%Put %Substr( %str(&B) , 4 , 1 );

 

In the third line, I'm masking &B with the %str function and it gives me error message:

 

56 %Let A = X,X ;
57 %Let B = Y%Str(&A)Y ;
58 %Put %Substr( %str(&B) , 4 , 1 );
ERROR: Macro function %SUBSTR has too many arguments.
 
I'm assuming this is because %str doesn't mask the resolved value which is YX,XY. The extra comma makes it an error in the %substr function.
 
Now, if I switch it to bquote which I believe masks the resolved value, it works:
 
%Let A = X,X ;
%Let B = Y%Str(&A)Y ;
%Put %Substr( %bquote(&B) , 4 , 1 );
Can I assume the %str doesn't mask the resolved value? If yes, the previous example:
 
%str(&var) shouldn't work because it doesn't mask the resolved value EQ. 
 
Can someone clarify on this? I'm confused...

 

kisumsam
Quartz | Level 8

Thanks so much! I know this topic is complex and this paper explains the concept very well. Will probably have to re-read it a few times to fully understand macro quoting.

ballardw
Super User

From the documentation, %str masks

+ - * / < > = ¬ ^ ~ ; ,  # blank
AND OR NOT EQ NE LE LT GE GT IN

Note that you are using it with & so %str may not do what you want.

%NSTR does the same as %str but adds: & %

So perhaps you want %NSTR in this case. 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 6 replies
  • 1866 views
  • 2 likes
  • 4 in conversation