Operator Mnemonic Precedence Definition Example
= EQ 6 equal A=B
# IN 6 equal to one
of a list**
¬= ^= ~= NE 6 not equal* A NE B
> GT 6 greater than A>B
>= GE 6 greater than or
& AND 7 logical and A=B & C=D
| OR 8 logical or A=B | C=D
*The symbol to use depends on your keyboard.
** The default delimiter for list elements is a blank. For more information, see
“MINDELIMITER= System Option” on page 368.
** Before using the IN (#) operator, see “MINOPERATOR System Option” on page 370.
** When you use the IN operator, both operands must contain a value. If the operand contains a
null value, an error is generated.
Integer expressions that contain exponential, multiplication, or division
operators and that use or compute values outside the range
-9,007,199,254,740,992 to 9,007,199,254,740,992 might get inexact results.
How the Macro Processor Evaluates Arithmetic
Evaluating Numeric Operands
The macro facility is a string handling facility. However, in specific situations, the macro
processor can evaluate operands that represent numbers as numeric values. The macro
processor evaluates an expression that contains an arithmetic operator and operands that
represent numbers. Then, it temporarily converts the operands to numeric values and
performs the integer arithmetic operation. The result of the evaluation is text.
By default, arithmetic evaluation in most macro statements and functions is performed
with integer arithmetic. The exception is the %SYSEVALF function. For more
information, see “Evaluating Floating-Point Operands” on page 77. The following
macro statements illustrate integer arithmetic evaluation:
%let a=%eval(1+2);
%let b=%eval(10*3);
%let c=%eval(4/2);
%let i=%eval(5/3);
%put The value of a is &a;
%put The value of b is &b;
76 Chapter 6 Macro Expressions
%put The value of c is &c;
%put The value of I is &i;
When you submit these statements, the following messages appear in the log:
The value of a is 3
The value of b is 30
The value of c is 2
The value of I is 1
Notice the result of the last statement. If you perform division on integers that would
ordinarily result in a fraction, integer arithmetic discards the fractional part.
When the macro processor evaluates an integer arithmetic expression that contains a
character operand, it generates an error. Only operands that contain characters that
represent integers or hexadecimal values are converted to numeric values. The following
statement shows an incorrect usage:
%let d=%eval(10.0+20.0); /*INCORRECT*/
The %EVAL function supports only integer arithmetic. The macro processor does not
convert a value containing a period character to a number, and the operands are
evaluated as character operands. This statement produces the following error message:
ERROR: A character operand was found in the %EVAL function or %IF
condition where a numeric operand is required. The condition was:
Evaluating Floating-Point Operands
The %SYSEVALF function evaluates arithmetic expressions with operands that
represent floating-point values. For example, the following expressions in the
%SYSEVALF function are evaluated using floating-point arithmetic:
%let a=%sysevalf(10.0*3.0);
%let b=%sysevalf(10.5+20.8);
%let c=%sysevalf(5/3);
%put 10.0*3.0 = &a;
%put 10.5+20.8 = &b;
%put 5/3 = &c;
The %PUT statements display the following messages in the log:
10.0*3.0 = 30
10.5+20.8 = 31.3
5/3 = 1.6666666667
When the %SYSEVALF function evaluates arithmetic expressions, it temporarily
converts the operands that represent numbers to floating-point values. The result of the
evaluation can represent a floating-point value, but as in integer arithmetic expressions,
the result is always text.
The %SYSEVALF function provides conversion type specifications: BOOLEAN,
INTEGER, CEIL, and FLOOR. For example, the following %PUT statements return 1,
2, 3, and 2 respectively:
%let a=2.5;
%put %sysevalf(&a,boolean);
%put %sysevalf(&a,integer);
%put %sysevalf(&a,ceil);
%put %sysevalf(&a,floor);
How the Macro Processor Evaluates Arithmetic Expressions 77

Get SAS 9.4 Macro Language, 5th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.