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 (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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