BookmarkSubscribeRSS Feed
cbob
Calcite | Level 5
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
15 REPLIES 15
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello Cbob,

Your test is not displayed correctly. Please, check.

Sincerely,
SPR
cbob
Calcite | Level 5
> 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)
SASPhile
Quartz | Level 8
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'
cbob
Calcite | Level 5
> 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,
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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
chang_y_chung_hotmail_com
Obsidian | Level 7
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 --*;
cbob
Calcite | Level 5
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
chang_y_chung_hotmail_com
Obsidian | Level 7
@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 --*;
Ksharp
Super User
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
CPAZ
Calcite | Level 5

Thanks Ksharp.

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

Chris

ArtC
Rhodochrosite | Level 12

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 ....

Haikuo
Onyx | Level 15

ArtC,

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

Best Regards,

Haikuo

ArtC
Rhodochrosite | Level 12

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.

art297
Opal | Level 21

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 15 replies
  • 27767 views
  • 4 likes
  • 9 in conversation