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!
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.
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.
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.
%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.
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.