DATA Step, Macro, Functions and more

ERROR: A Character operand was found in the %Eval function

Reply
Occasional Contributor
Posts: 8

ERROR: A Character operand was found in the %Eval function

Hello,
Can anyone help me to solve this error?
My Code:
%let a = 0.967;
%let b = 0.942;
%let diff = %sysevalf(&a. - &b.);
%Put diff = &diff.;

%Macro test;

%if (0.01 <= &diff. <= 0.01) %then %put &diff.;
%else %put 'Out of Range';
%Mend;
%test;

All I want to do is print the condition result in the log. In this case I
want to print as Out of Range as the value of diff is beyond the boundary
limits.

I am getting this error "A character operand was found in the %EVAL function
or %IF condition where a numeric operand is required. The condition was: (0.
01 <= &diff. <= 0.01)

Thanks,
cbob
Super Contributor
Super Contributor
Posts: 365

Re: ERROR: A Character operand was found in the %Eval function

Hello Cbob,

Your test is not displayed correctly. Please, check.

Sincerely,
SPR
Occasional Contributor
Posts: 8

Re: ERROR: A Character operand was found in the %Eval function

> Hello Cbob,
>
> Your test is not displayed correctly. Please, check.
>
> Sincerely,
> SPR

Hello SPR,
For some reason my msg didn't get fully displayed. Below is my actual code

> %let a = 0.967;
> %let b = 0.942;
> %let diff = %sysevalf(&a. - &b.);
> %Put diff = &diff.;
> %Macro test;
> %if (0.01 LE &diff. LE 0.01) %then %put &diff.;
> %else %put 'Out of Range';
> %Mend;
> %test;
>
> All I want to do is print the condition result in the
> log. In this case I
> want to print as Out of Range as the value of diff is
> beyond the boundary
> limits.
>
> I am getting this error "A character operand was
> found in the %EVAL function
> or %IF condition where a numeric operand is required.
> The condition was: (0. 01 LE &diff. LE 0.01)
Super Contributor
Posts: 647

Re: ERROR: A Character operand was found in the %Eval function

Works fine:


84
85
86 %let a = 0.967;
87 %let b = 0.942;
88 %let diff = %sysevalf(&a. - &b.);
89 %Put diff = &diff.;
diff = 0.025
90 %Macro test;
91 %if (0.01 LE &diff. LE 0.01) %then %put &diff.;
92 %else %put 'Out of Range';
93 %Mend;
94 %test;
'Out of Range'
Occasional Contributor
Posts: 8

Re: ERROR: A Character operand was found in the %Eval function

> Works fine:
>
>
> 84
> 85
> 86 %let a = 0.967;
> 87 %let b = 0.942;
> 88 %let diff = %sysevalf(&a. - &b.);
> 89 %Put diff = &diff.;
> diff = 0.025
> 90 %Macro test;
> 91 %if (0.01 LE &diff. LE 0.01) %then %put
> &diff.;
> 92 %else %put 'Out of Range';
> 93 %Mend;
> 94 %test;
> 'Out of Range'

Thanks for your reply!. It blows up when the diff is negative.Also it is -0.01 in the below lower limit. It works absolutely fine with diff value being positive but if the diff is NEGATIVE it throws error.

%if (-0.01 LE &diff. LE 0.01) %then %put

Thanks,
Super Contributor
Super Contributor
Posts: 365

Re: ERROR: A Character operand was found in the %Eval function

Hello Cbob,

I personally do not like to use comparison operators for macro variables containing numbers with decimals. Macro variables contain character values so SAS compares character strings but not numbers. So result could depend on how many digits contain decimals you compare.

Sincerely,
SPR
Regular Contributor
Posts: 241

Re: ERROR: A Character operand was found in the %Eval function

Your macro %test has a number of problems. I would rather do something like:



   %*-- check if value is between min and max, inclusive --*;

   %macro inRange(value, min=0.0, max=0.1);

     %if %sysevalf((&value) < (&min)) %then 0;

     %else %if %sysevalf((&max) < (&value)) %then 0;

     %else 1;

   %mend  inRange;

 

   %put %inRange(0.00);  %*-- 1 --*;

   %put %inRange(0.01);  %*-- 1 --*;

   %put %inRange(0.10);  %*-- 1 --*;

   %put %inRange(0.15);  %*-- 0 --*;
Occasional Contributor
Posts: 8

Re: ERROR: A Character operand was found in the %Eval function

Chang,

Thanks for your response. I guess what I'm trying to do is to validate whether the difference between the two macro variables a& b is with in the boundary limits(-0.01 to 0.01) or not.

%let a = 0.9377;
%let b = 0.9570;
%let diff = %sysevalf(&a - &b);
%Put diff = &diff;

%Macro test;
%if ((-0.01) LE %sysevalf(&diff.) LE (0.01)) %then %put &diff.;
%else %put 'Out of Range';
%Mend;
%test;

When i run the above code I am getting the below error
ERROR: A Character operand was found in the %Eval function or %IF condition where a numeric operand is required.

Thanks,
Sridhar
Regular Contributor
Posts: 241

Re: ERROR: A Character operand was found in the %Eval function

@cbob: Someone has once said:

    "Programming is hard enough even when you do know the language. If you don't, I don't see how you have a chance."



After reading Beilei Xu and Lei Zhang's excellent NESUG paper, I think I know what is going on -- that is, the very first part of the comparison:
   -0.01 le 0.01
is an error within the implicit %eval between %if and %then. See below for more.



   %put %eval(-1);   %*-- this is fine. integer --*;

   %put %eval(0.01); %*-- error. char(.) found when an integer expected --*;

   %put %eval(0.01 le 0.01);  %*-- fine. just comparing two text strings --*;

   %put %eval(-0.01 le 0.01); %*-- error. minus operator expects an integer --*;
Super User
Posts: 9,662

Re: ERROR: A Character operand was found in the %Eval function

Hi.
Try to this:

[pre]
%if %sysevalf((-0.01) LE &diff. LE (0.01)) %then
[/pre]


For the %if ,macro facility will automatically add %eval around the digit ,
So you need to change it into %sysevalf.



Ksharp
Contributor
Posts: 30

Re: ERROR: A Character operand was found in the %Eval function

Thanks Ksharp.

This actually is helping me with the same kind of issue. Saved me tons of research time! Smiley Happy

Chris

Valued Guide
Posts: 632

Re: ERROR: A Character operand was found in the %Eval function

Aside from the problem of evaluating non integers with the implied %EVAL.  The macro language does not evaluate compound expressions in the same way as in the DATA step.

In the DATA step

if 5 < x < 10 is evaluaged as if 5 < x and x < 10

In the macro language

%if 5 < &x < 10 is evaluated as %if (5 < &x) < 10

Regardless of the value of &X this expression is true as both 0 and 1 are less than 10.

In the macro language always break up compound expressions.

%if 5 < &x and &x < 10 %then ....

Respected Advisor
Posts: 3,124

Re: ERROR: A Character operand was found in the %Eval function

ArtC,

   Love your book on SAS Macro. Wondering how I can get your signature.:smileysilly:

Best Regards,

Haikuo

Valued Guide
Posts: 632

Re: ERROR: A Character operand was found in the %Eval function

It is always fun to meet those that read and use the SAS Press books.  If you are at SAS Global Forum or PharmaSUG this spring look me up.  I would be very happy to meet and talk with you and to of course sign your book.

PROC Star
Posts: 7,356

Re: ERROR: A Character operand was found in the %Eval function

Haikuo:  While I haven't written any books that I can sign, look me up, too, if you are going to SGF.  I'm doing one presentation with FriedEgg, another with both FriedEgg and KSharp (and a couple of other presentations too) and, of course, ArtC and I will both be presenting at the SAS-L/ SAS Discussion Forums/ sasCommunity.org/ SASProfessionals.net/ SASCanadaCommunity meetup on Tuesday night (April 24th) between 6pm and 8pm.

Ask a Question
Discussion stats
  • 15 replies
  • 19635 views
  • 4 likes
  • 9 in conversation