Lovnlust
Lovnlust

Reputation: 1537

macro error: A character operand was found in the %EVAL function or %IF condition

%macro test(k);

%if &k le 0 %then %put < 0 ;
%else %put > 0;
%mend test;
%test(-5); 
%test(3.1);

But %test(-3.1); will generate error

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 
       &k le 0 

I have no idea why this simple value comparison will lead to error. At first I guess it's because .. But inputing 3.1 seems everything goes well.

Upvotes: 1

Views: 18698

Answers (2)

Quentin
Quentin

Reputation: 6378

The macro %IF statement implicitly calls the %EVAL() function. %EVAL() understands integers (whether positive or negative), but not decimal values. When %EVAL() compares two values, if one of them is a decimal it will do a CHARACTER comparison. So %IF (3.1>10) returns true. If you give %EVAL a decimal with a negative sign in front of it (-3.1), it errors, because it thinks 3.1 is character not a number, so the - sign must be the subtraction operator, and then you are trying to subtract character values. Below are some examples of playing with %eval().

%put %eval(10   > 2) ;   /*true: numeric comparison*/
%put %eval(10.1 > 2) ;   /*false: character comparison*/

%put %eval(-2   > -5 ) ; /*true: numeric comparison*/
%put %eval(2.0  > -5 ) ; /*true: character comparison*/

%put %eval(+10 > +2 ) ; /*true: numeric comparison*/
%put %eval(-10 > +2 ) ; /*false: numeric comparison*/
%put %eval(10.1 > +20 ) ; /*false: character comparison (+20 is evaluated to 20) */
%put %eval(+10.1 >+20 ) ;  /*error: %eval() cant handle +10.1*/
%put %eval(-10.1 >+20 ) ;  /*error: %eval() cant handle -10.1*/

%put %eval(-2);    /* -2 */
%put %eval(+2);    /* 2 */
%put %eval(-2.1);  /*error*/
%put %eval(+2.1);  /*error*/

Upvotes: 4

Stig Eide
Stig Eide

Reputation: 1062

You should use sysevalf to evaluate arithmetic and logical expressions using floating-point arithmetic. As you pointed out, %sysevalf(&k le 0) will solve the problem.

Upvotes: 1

Related Questions