/***************************************************************************************************************** SAS file name: eval.sas File location: __________________________________________________________________________________________________________________ Purpose: To explore the %Eval Macro Function in SAS Author: Peter Clemmensen Creation Date: 17/05/2020 This program supports the blog post "Explore the %Eval Macro Function in SAS" on SASnrd.com *****************************************************************************************************************/ /* SAS needs evaluation functions to evaluate arithmetic and logical expresions */ %let a = 1+2; %let b = %eval(1+2); %put &=a &=b; /* %Eval deals with integer expresions and treats any non-numeric as character */ %let a = %eval(1+1.5); /* Error */ %let b = %eval(1+1.0); /* Error */ %let c = %eval(1+1.); /* Error */ %let d = %eval(10.0 > 2.0); /* No error. But d evaluates to 0. */ %let e = %eval(3/2); /* SAS truncates e to 1 */ %put &=a &=b &=c &=d; /* Implicit Eval Function */ /* When logical or arithmetic expresions are used in conditional logic or various %Do blocks, and implicit %Eval Function is used */ /* Macro a and b are identival. */ %macro a; %if 1+1=2 %then %put one plus one equals two!; %mend; %a; /* Behind the scenes, this happens */ %macro b; %if %eval(1+1=2) %then %put one plus one equals two!; %mend; %b; /* Be careful with non integers! */ %macro c; %if 10.0 > 2.0 %then %do; %put 10.0 is greater than 2.0!; %end; %else %do; %put 10.0 is less than 2.0!; %end; %mend; %c; /* Implicit %Eval Calls are present in iterative macro logic as well */ /* This */ %macro iter; %do i = 1 %to 10 %by 2; %put &i; %end; %mend; %iter; /* Is equivalent to */ %macro iter2; %do i = %eval(1) %to %eval(10) %by %eval(2); %put &i; /* %let i = %eval(&i + 2); Implicit */ %end; %mend; %iter2;