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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1463 views
  • 2 likes
  • 4 in conversation