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

Hi all,

 

I have the following code, but am not getting the desired result. It works only with one condition but not other. Please let me know how to fix this

 

%macro test(dsn=,var=);

data &dsn;

set  core(where=(cat="&var"));

run;

 

%if  &var eq  CL %then %do;

proc print data=&dsn;

run;

%end;

 

%else %do;

proc contents data=&dsn;

run;

%end;

 

%mend;

 

Macro call :

%test(dsn=dsn1,var=CL); This one is working

%test(dsn=dsn1,var=CL-VT-MO);

 

Log shows the following error;

A character operand was found in the %EVAL function or %IF condition where a numeric operand is required.

The condition was: &cond eq  CL

 

Appreciate your input

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

The SAS macro processor interprets the dash as subtraction, which doesn't make sense in this situation. So, you need to tell the SAS macro processor not to do that.

 

%test(dsn=dsn1,var=%str(CL-VT-MO))
--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

The SAS macro processor interprets the dash as subtraction, which doesn't make sense in this situation. So, you need to tell the SAS macro processor not to do that.

 

%test(dsn=dsn1,var=%str(CL-VT-MO))
--
Paige Miller
Kurt_Bremser
Super User

In a %IF condition, simple arithmetic can be used (in this, the macro processor deviates from being a "pure text" processor). That's why you need to "mask" arithmetic operators.

novinosrin
Tourmaline | Level 20

Hi @sri1  This is a very interesting question that stumped me a few years ago. Basically, what we need to understand is that how a macro %IF %THEN %ELSE treats INTEGER arithmetic and FLOAT arithmetic. 

 

The default behavior of a %IF is to compute INTEGER arithmetic/comparison operators. Therefore when the macro processor "tokenises" the code syntax bits that had operators that are of the arithmetic/comparison operator, it evaluates the component by sending signals to the ALU(arithmetic logical unit), places back the result and then continues to process the next subsequent tokens. 

 

However, the same %IF is not designed to compute/evaluate FLOAT arithmetic by default and would often result in a similar error that you encountered. Moreover, in your case, the macro processor sees CL, VT and MO as character constant operands and so there is no way that it can compute or evaluate an INTEGER-arithmetic operation. 

 

This leads to the situation in us finding a way to treat the operators as text, so that the default behavior of macro processor when dealing with tokens within a %IF is changed to processing regular name tokens aka text

 

The macro quoting functions, like %STR,%BQUOTE does just that. Of course there is a difference between how stuff works %STR vs%BQUOTE besides the fact former works at compile time and the latter works at execution time with some nuances, but that's beyond the scope of this question in my opinion.

 

So the following should fix-

%test(dsn=dsn1,var=%str(CL-VT-MO))

/*OR*/

%test(dsn=dsn1,var=%bquote(CL-VT-MO))

Hope that helps.

 

PS Thank you @mkeintz  and @Tom  for helping me understand this. I guess you both were young back then. 🙂 lol

 

 

 

 

Tom
Super User Tom
Super User

The problem is this statement:

%if  &var eq  CL %then %do;

And the solution is shown in the step just above.

data &dsn;
  set  core(where=(cat="&var"));
run;

Just change your test to:

%if  "&var" eq  "CL" %then %do;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 813 views
  • 2 likes
  • 5 in conversation