Using Arithmetic and Logical Expressions

The %EVAL Function

The %EVAL function evaluates integer arithmetic or logical expressions. Logical expressions and arithmetic expressions are sequences of operators and operands forming sets of instructions that are evaluated to produce a result.
  • An arithmetic expression contains an arithmetic operator.
  • A logical expression contains a logical operator.
General form, %EVAL function:
%EVAL(arithmetic or logical expression)
The %EVAL function
  • translates integer strings and hexadecimal strings to integers
  • translates tokens representing arithmetic, comparison, and logical operators to macro-level operators
  • performs arithmetic and logical operations.
For arithmetic expressions, if an operation results in a non-integer value, %EVAL truncates the value to an integer. Also, %EVAL returns a null value and issues an error message when non-integer values are used in arithmetic expressions.
%EVAL evaluates logical expressions and returns a value to indicate whether the expression is true or false. A value of 0 indicates that the expression is false, and a value of 1 or any other numeric value indicates that the expression is true.
The %EVAL function does not convert the following to numeric values:
  • numeric strings that contain a period or E notation
  • SAS date and time constants.
Here are some examples.

Examples

The following table shows several examples of arithmetic and logical expressions, as well as the results that %EVAL produces when it evaluates these expressions.
If you submit these statements...
These messages are written to the log...
%put value=%eval(10 lt 2);
value=0
%put value=10+2; 
%put value=%eval(10+2);
value=10+2 
value=12
%let counter=2; 
%let counter=%eval(&counter+1);
%put counter=&counter;
counter=3
%let numer=2; 
%let denom=8; 
%put value=%eval(&numer/&denom);
value=0
%let numer=2; 
%let demon=8; 
%put value=%eval(&numer/&denom*&denom);
%put value=%eval(&denom*&numer/&denom);
value=0 
value=2
%let real=2.4; 
%let int=8; 
%put value=%eval(&real+&int);
value=
In the last example above, the decimal value of the real variable causes an error message to be written to the SAS log, as shown here.
Table 11.23 SAS Log
1    %let real=2.4;
2    %let int=8;
3    %put value=%eval(&real+&int);
ERROR: A character operand was found in the %EVAL function
or %IF condition where a numeric operand is required.
The condition was: 2.4+8
value=
Because %EVAL does not convert a value that contains a period to a number, the operands are evaluated as character operands.
You have seen that the %EVAL function generates ERROR messages in the log when it encounters an expression that contains non-integer values. In order to avoid these ERROR messages, you can use the %SYSEVALF function. The %SYSEVALF function evaluates arithmetic and logical expressions using floating-point arithmetic.
General form, %SYSEVALF function:
%SYSEVALF(expression<, conversion-type>)
Here is an explanation of the syntax:
expression
is an arithmetic or logical expression to evaluate.
conversion-type
converts the value returned by %SYSEVALF to the type of value specified. Conversion-type can be BOOLEAN, CEIL, FLOOR, or INTEGER.
The %SYSEVALF function performs floating-point arithmetic and returns a value that is formatted using the BEST16. format. The result of the evaluation is always text.

Example

The macro in the following example performs all types of conversions for values in the %SYSEVALF function:
%macro figureit(a,b);
   %let y=%sysevalf(&a+&b);
   %put The result with SYSEVALF is: &y;
   %put BOOLEAN conversion: %sysevalf(&a +&b, boolean);
   %put CEIL conversion: %sysevalf(&a +&b, ceil);
   %put FLOOR conversion: %sysevalf(&a +&b, floor);
   %put INTEGER conversion: %sysevalf(&a +&b, integer);
   %mend figureit;

%figureit(100,1.59)
Executing this program writes the following lines to the SAS log.
Table 11.24 SAS Log
The result with SYSEVALF is: 101.59
BOOLEAN conversion: 1
CEIL conversion: 102
FLOOR conversion: 101
INTEGER conversion: 101

Automatic Evaluation

%SYSEVALF is the only macro function that can evaluate logical expressions that contain floating point, date, time, datetime, or missing values. Specifying a conversion type can prevent problems when %SYSEVALF returns missing or floating-point values to macro expressions or macro variables that are used in other macro expressions that require an integer value.
Keep in mind that any macro language function or statement that requires a numeric or logical expression automatically invokes the %EVAL function. This includes the %SCAN function, the %SUBSTR function, the %IF-%THEN statement, and more.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.83.62