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

I am wondering does there exist a scenario that the "+" operator must be masked by quoting functions such as %STR or %BQUOTE? In SAS documentation, it states that "+" may need to be masked "To prevent it from being treated as an operator in the argument of an %EVAL function".  However, I tested a couple of toy examples and found that directly typing "+" and using "%str(+)" makes no difference. 

 

For example, both "%let var1 = A+B;"  and "%let var2 = %str(A+B)"  resolve to "A+B" when &var1/&var2 is referenced -- there are no error/warning messages in the log. Therefore, it seems that SAS never misinterprets "+". 

 

So my question is, could anyone construct a specific example that enclosing the "+" (or similar operators such as "=, -, *") with quoting function is a must? That is, without quoting it, the SAS will issue an error? Thank you very much!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

An %IF %THEN statement invokes %EVAL.  So try this combination:

%let breakfast = ham + eggs;
%if &breakfast = ham + eggs %then %let fed=yes;

The first statement works just fine.  But the second statement generates an error message.  The implied %EVAL sees the plus sign, and tries to add together ham + eggs.  There are a few ways to get around this, and a quoting function is one of them.

 

 

View solution in original post

5 REPLIES 5
ChrisNZ
Tourmaline | Level 20

The help page mentions function %eval, yet you haven't used it.

 

%put %eval( 2 %str(+) 3 );    will show you the effect of masking the +.

 

Unsure why you'd ever want to do this.

 

 

Shmuel
Garnet | Level 18

Usually you use the + (plus) sign in mathematical operation,

but sometimes you want to display a formula (to a child) and prompt for a result to check.

Look at next code :

data test;
    no_1 = '2';
    no_2 = '3';
    formula = no_1 || "+" || no_2 || "=?";
    put formula " Enter your answer";
run;

The usage of macro programing is just a tool to generate a code which is finally sas code.

Zhanxiong
Obsidian | Level 7

%put %eval(2 %str(+) 3); will issue an error message, and I know that...

 

I am asking to construct an example that only after %str(+) is added, the error/warning caused by directly using unmasked "+" can be AVOIDED, not the opposite direction. 

Astounding
PROC Star

An %IF %THEN statement invokes %EVAL.  So try this combination:

%let breakfast = ham + eggs;
%if &breakfast = ham + eggs %then %let fed=yes;

The first statement works just fine.  But the second statement generates an error message.  The implied %EVAL sees the plus sign, and tries to add together ham + eggs.  There are a few ways to get around this, and a quoting function is one of them.

 

 

Zhanxiong
Obsidian | Level 7
This is exactly what I want. Thank you!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 855 views
  • 4 likes
  • 4 in conversation