BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ANIRBAN2
Fluorite | Level 6
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:
&op.=+
 

options mlogic symbolgen mprint
%macro calc(a=,b=,op=);
%global c;
%if %str(&op.)=%nrstr(+) %then
%let C=%eval(&a.+ &b.);
%if %str(&op.)=%nrstr(-) %then
%let C=%eval(&a. - &b.);
%if %str(&op.)= %nrstr(*) %then
%let C=%eval(&a.* &b.);
%if %str(&op.)=%nrstr(/) %then %do;
%if &b.=0 %then
%put "SOORRY";
%else
%let C=%eval(&a./ &b.);
%end;
%mend;

1 ACCEPTED SOLUTION

Accepted Solutions
Amir
PROC Star

Hi @ANIRBAN2,

 

Thanks for sharing the code and error. There are a few things to note with the macro definition, but for starters, how are you calling the macro? Have you tried something like the following?

 

%calc(a=1,b=2,op=%str(+));

 

 

Kind regards,

Amir.

View solution in original post

5 REPLIES 5
Amir
PROC Star

Hi @ANIRBAN2,

 

Thanks for sharing the code and error. There are a few things to note with the macro definition, but for starters, how are you calling the macro? Have you tried something like the following?

 

%calc(a=1,b=2,op=%str(+));

 

 

Kind regards,

Amir.

SASKiwi
PROC Star

Is this just an experiment to see if you can do arithmetic in SAS macro language or is it for some other purpose?

 

A SAS DATA step is a much better place to do calculations like this and it can still be inside a macro:

 

options mlogic symbolgen mprint
%macro calc(a=,b=,op=);
%global c;

data _null_;
  &c = &a &op &b;
run;

%mend;

 

Astounding
PROC Star

Numeric operators within a %IF condition cause trouble.  The macro processor will look to perform math.  Instead of using quoting functions, simply use double quotes so macro language knows you have characters and won't try to perform math:

%if "&op" = "+" %then
%let C=%eval(&a.+ &b.);

Be sure to use double quotes (not single quotes), and make certain that the value of &OP does not contain any leading or trailing blanks.

ANIRBAN2
Fluorite | Level 6
This is also Working
PeterClemmensen
Tourmaline | Level 20

The reason that the %if statement causes trouble when dealing with character values is that an implicit call to the %Eval Macro Function is performed behind the scenes. As the %Eval Doc says, it "Evaluates arithmetic and logical expressions using integer arithmetic." 

 

This can cause various problems. For example, if a values has a dot in it as below. Here, the macro m will conclude that 10.0 is less than 2.0. 

 

%macro m;
   %if 10.0 > 2.0 %then %do;
      %put 10.0 is greater than 2.0!;
   %end;
   %else %do;
      %put 10.0 is less than 2.0!;
   %end;
%mend;
 
%m;

I doubt that you actually really need this macro. However, for a workaround, I would go with @Astoundings solution.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 559 views
  • 3 likes
  • 5 in conversation