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 !
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;
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
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.
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()
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.