DATA Step, Macro, Functions and more

SAS Quoting Function %STR

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

SAS Quoting Function %STR

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

 

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?
 
 
 

 

 


Accepted Solutions
Solution
‎12-14-2016 12:14 PM
Regular Contributor
Posts: 234

Re: SAS Quoting Function %STR


All Replies
Super User
Posts: 9,671

Re: SAS Quoting Function %STR

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)

Super User
Posts: 9,671

Re: SAS Quoting Function %STR

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

compile stage is BEFORE execute stage.

Therefore BOTH worked.

Contributor
Posts: 21

Re: SAS Quoting Function %STR

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

 

Solution
‎12-14-2016 12:14 PM
Regular Contributor
Posts: 234

Re: SAS Quoting Function %STR

Contributor
Posts: 21

Re: SAS Quoting Function %STR

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.

Super User
Posts: 10,474

Re: SAS Quoting Function %STR

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. 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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