EVAL and SYSEVALF are the two macro functions that evaluate arithmetic expressions and logical expressions.
The %EVAL function is used to evaluate integer arithmetic or logical expressions. The argument passed in the %EVAL function is converted from character to numeric and the results are converted back to character.
All parts of the macro language that evaluate expressions (for example, %IF and %DO statements) call %EVAL to evaluate the expressions.
The %EVAL function can be called explicitly or implicitly. It appears elusive and problematic when its argument is a macro expression contains special characters, mixed arithmetic and logical operators, or any other macro quoting functions.
%EVAL(arithmetic expression|logical expression)
Interaction with special characters
Once %EVAL is invoked, whether implicitly or explicitly, its argument is scanned, resolved, and passed to %EVAL to evaluate. Then %EVAL determines whether it performs an integer arithmetic evaluation or a logical comparison based on the actual resolved argument.
When all operands in the argument 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.
When %EVAL encounters an arithmetic operation containing non-integer values or
characters, it displays an error message about finding a character operand where a numeric operand is required.
If a division operation results in a fraction, the fraction is truncated to an integer. %SYSEVALF is the function to deal with non-integer floating-point arithmetic and
%EVAL can have multiple arithmetic or logical expressions or mixed expressions as an argument. The order in which operations are performed in the macro language is the same as in the DATA step.
In macro expressions, a period (.) can be used as a separator for macro variables. For example, in &x.y, the period separates the macro variable &X from the regular text Y.
%EVAL only accepts operands in arithmetic expressions that represent integers and operands that contain a period character generates an error when they are part of an integer arithmetic expression.
However, if a period appears in a resolved logical expression, it will be treated as a regular character. For example, if the SAS version is 8.2, the expression
%eval(&sysver > 10) is evaluated as true.
The following examples show correct and incorrect usage of the period, respectively:
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 10.0+20.0
Now see the different results of using %EVAL and %SYSEVALF.
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 10+.
NOTE: Missing values were generated as a result of performing an operation on missing values during %SYSEVALF expression evaluation.
%EVAL treats the period as a character value, which causes an error in an arithmetic operation. However, %SYSEVAL treats the period as a missing value and returns a missing value without an error.
%SYSEVALF function is used to.evalaute floating-point numbers. The result of the %SYSEVALF function are converted to text but you can also request %SYSEVALF to convert the result to a different format. Below are the formats that can be used with the %sysevalf function.
Syntax: %SYSEVALF(expression<, conversion-type>)
When requesting one of these four conversion types, specify the conversion type as the second argument to %SYSEVALF.
%sysevalf(1/3,boolean) /* returns 1 */ %sysevalf(10+.,boolean) /* returns 0 */
The %SYSEVALF function evaluates arithmetic and logical expressions using floating-point arithmetic and returns a value that is formatted using the BEST32. format.
You can use %SYSEVALF function to perform arithmetic calculations that have floating-point value.
Below are some examples of the %SYSEVALF function.
%put %sysevalf(10.0*30.0); * returns in the log is 300; %put %sysevalf(10.5+20.8); * returns in the log is 31.3; %put %sysevalf(50/30); * returns in the log is 1.66666666666666;
%SYSEVALF function perform arithmetic calculations and the result of the evaluation can be a floating-point value like in the final and last macro variable case, but as in integer arithmetic calculations, the result is always a text.
The %SYSEVALF function is used in conjugation with other functions like INTEGER, CEIL, and FLOOR.
For example, the following %PUT statements return 3, 4 and 3 respectively:
%let pi=2.14; %put %sysevalf(&pi,integer); *PI returns in the log is 2; %put %sysevalf(&pi, ceil); *PI returns in the log is 3; %put %sysevalf(&pi,floor); *PI returns in the log is 2;