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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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