SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
SASdevAnneMarie
Barite | Level 11

Hello Experts,

 

The value of my macrovariable %put TEST &resultat. is 4.

I would like to do the test : %if %eval(&resultat.<4) %then
%do; .....

 My question is : do I need to write the %eval or I can just write 

 %if  &resultat.<4 %then
%do; ..... because it seems to work.

 

Thank you for your help !

4 REPLIES 4
japelin
Rhodochrosite | Level 12

In the meantime, is it possible to upload the code before and after?
Normally, %eval is not necessary if such a value is stored in a macro variable.


You could run the following code and check the logs to see how SAS is processing the macro.

 

options mprint mlogic symbolgen;
PeterClemmensen
Tourmaline | Level 20

No, you do not need to write the %Eval Function explicitly.

 

The reason you do not have to is that SAS calls the %Eval Function implicitly behind the scenes. 

 

So, this

 

%macro a;
   %if 1 + 1 = 2 %then %put One plus one equals two!;
%mend;
 
%a

 

Is the same as this

 

%macro b;
   %if %eval(1 + 1 = 2) %then %put one plus one equals two!;
%mend;
 
%b

 

Quentin
Super User

And it's important to know that %EVAL is implicitly called by %IF, because it explains surprises like:

1    %macro a();
2       %if 10.1 < 2 %then %put 10.1 is less than two!?! ;
3    %mend;
4    %a()
10.1 is less than two!?!

If you give an %IF statement a string that it thinks is a numeric expression, sometimes you'll get an error which shows that %EVAL is being called, e.g.:

6    %macro b();
7       %if Q+1=Q+1 %then %put Equal ;
8    %mend;
9    %b()
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand
       is required. The condition was: Q+1=Q+1
ERROR: The macro B will stop executing.
The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
Patrick
Opal | Level 21

One could argue that using %eval() explicitly is not a bad thing as then it's directly visible in the code what's happening.

%macro a();
   %if 10.1 < 2             %then %put Test1a: 10.1 is less than two!?! ;
   %if %eval(10.1 < 2)      %then %put Test2a: 10.1 is less than two!?! ;
   %if %sysevalf(10.1 < 2)  %then %put Test3a: 10.1 is less than two!?! ;
   %if 10 < 2               %then %put Test4a: 10   is less than two!?! ;

   %if 2 < 10.1             %then %put Test1b: Two is less than 10.1!?! ;
   %if %eval(2 < 10.1)      %then %put Test2b: Two is less than 10.1!?! ;
   %if %sysevalf(2 < 10.1)  %then %put Test3b: Two is less than 10.1!?! ;
   %if 2 < 10               %then %put Test4b: Two   is less than 10!?! ;

%mend;
%a()

Patrick_0-1662180173159.png

 

 

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 847 views
  • 5 likes
  • 5 in conversation