|Type:||Macro evaluation function|
|See also:||%SYSEVALF Function|
|Example 1: Illustrating Integer Arithmetic Evaluation|
|Example 2: Incrementing a Counter|
|Example 3: Evaluating Logical Expressions|
|%EVAL (arithmetic or logical expression)|
The %EVAL function evaluates integer arithmetic or logical expressions. %EVAL operates by converting its argument from a character value to a numeric or logical expression. Then, it performs the evaluation. Finally, %EVAL converts the result back to a character value and returns that value.
If all operands can be interpreted as integers, the expression is treated as arithmetic. If at least one operand cannot be interpreted as numeric, the expression is treated as logical. If a division operation results in a fraction, the fraction is truncated to an integer.
%EVAL accepts only operands in arithmetic expressions that represent integers (in standard or hexadecimal form). Operands that contain a period character cause an error when they are part of an integer arithmetic expression. The following examples show correct and incorrect usage, respectively:
Because %EVAL does not convert a value containing a period to a number, the operands are evaluated as character operands. When %EVAL encounters a value containing a period, it displays an error message about finding a character operand where a numeric operand is required.
An expression that compares character values in the %EVAL function uses the sort sequence of the operating environment for the comparison. Refer to "The SORT PROCEDURE" in the Base SAS Procedures Guide for more information about operating environment sort sequences.
All parts of the macro language that evaluate expressions (for example, %IF and %DO statements) call %EVAL to evaluate the condition. For a complete discussion of how macro expressions are evaluated, see Macro Expressions.
%let a=1+2; %let b=10*3; %let c=5/3; %let eval_a=%eval(&a); %let eval_b=%eval(&b); %let eval_c=%eval(&c); %put &a is &eval_a; %put &b is &eval_b; %put &c is &eval_c;
%macro test(finish); %let i=1; %do %while (&i<&finish); %put the value of i is &i; %let i=%eval(&i+1); %end; %mend test; %test(5)
When this program executes, these lines are written to the SAS log:
%macro compare(first,second); %if &first>&second %then %put &first > &second; %else %if &first=&second %then %put &first = &second; %else %put &first<&second; %mend compare; %compare(1,2) %compare(-1,0)