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

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);

但是%test(-3.1);会产生错误

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 

我不知道为什么这种简单的值比较会导致错误。一开始我猜是因为.。但是输入3.1好像一切正​​常

您应该使用 sysevalf 来评估使用浮点运算的算术和逻辑表达式。正如您所指出的,%sysevalf(&k le 0) 将解决问题。

宏 %IF 语句隐式调用 %EVAL() 函数。 %EVAL() 可以理解整数(无论是正数还是负数),但不能理解小数值。当 %EVAL() 比较两个值时,如果其中一个是小数,它将进行字符比较。所以 %IF (3.1>10) returns 正确。如果你给 %EVAL 一个小数,前面有一个负号(-3.1),它会出错,因为它认为 3.1 是字符而不是数字,所以 - 符号必须是减法运算符,然后你试图减去字符值。下面是一些使用 %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*/