Macro Functions |
Type: | Macro evaluation function |
See also: | %SYSEVALF Function |
Syntax | |
Details | |
Comparisons | |
Examples | |
Example 1: Illustrating Integer Arithmetic Evaluation | |
Example 2: Incrementing a Counter | |
Example 3: Evaluating Logical Expressions |
Syntax |
%EVAL (arithmetic or logical expression) |
Details |
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.
Logical, or Boolean, expressions return a value that is evaluated as true or false. In the macro language, any numeric value other than 0 is true and a value of 0 is false.
%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:
%let d=%eval(10+20); /* Correct usage */ %let d=%eval(10.0+20.0); /* Incorrect usage */
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.
Comparisons |
%EVAL performs integer evaluations, but %SYSEVALF performs floating point evaluations.
Examples |
These statements illustrate different types of evaluations:
%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;
When these statements are submitted, the following is written to the SAS log:
1+2 is 3 10*3 is 30 5/3 is 1
The third %PUT statement shows that %EVAL discards the fractional part when it performs division on integers that would result in a fraction:
The macro TEST uses %EVAL to increment the value of the macro variable I by 1. Also, the %DO %WHILE statement calls %EVAL to evaluate whether I is greater than the value of the macro variable FINISH.
%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:
The value of i is 1 The value of i is 2 The value of i is 3 The value of i is 4
Macro COMPARE compares two numbers.
%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)
When this program executes, these lines are written to the SAS log:
1 < 2 -1 < 0
Copyright © 2009 by SAS Institute Inc., Cary, NC, USA. All rights reserved.