Defining Arithmetic and Logical Expressions

Evaluating Arithmetic and Logical Expressions

You can use arithmetic and logical expressions in specific macro functions and statements. (See the following table.) The arithmetic and logical expressions in these functions and statements enable you to control the text generated by a macro when it is executed.
Macro Language Elements that Evaluate Arithmetic and Logical Expressions
%DOmacro-variable=expression %TO expression<%BY expression>;
%DO %UNTIL(expression);
%DO %WHILE(expression);
%EVAL (expression);
%IF expression %THEN statement;
%QSCAN(argument,expression<,delimiters>)
%QSUBSTR(argument,expression<,expression>)
%SCAN(argument,expression,<delimiters>)
%SUBSTR(argument,expression<,expression>)
%SYSEVALF(expression,conversion-type)
You can use text expressions to generate partial or complete arithmetic or logical expressions. The macro processor resolves text expressions before it evaluates the arithmetic or logical expressions. For example, when you submit the following statements, the macro processor resolves the macro variables &A, &B, and &OPERATOR in the %EVAL function, before it evaluates the expression 2 + 5:
%let A=2;
%let B=5;
%let operator=+;
%put The result of &A &operator &B is %eval(&A &operator
&B).;
When you submit these statements, the %PUT statement writes the following to the log:
The result of 2 + 5 is 7.

Operands and Operators

Operands in arithmetic or logical expressions are always text. However, an operand that represents a number can be temporarily converted to a numeric value when an expression is evaluated. By default, the macro processor uses integer arithmetic, and only integers and hexadecimal values that represent integers can be converted to a numeric value. Operands that contain a period character (for example 1.0) are not converted. The exception is the %SYSEVALF function. It interprets a period character in its argument as a decimal point and converts the operand to a floating-point value on your operating system.
Note: The values of numeric expressions are restricted to the range of –2**64 to 2**64–1.
Operators in macro expressions are a subset of the operators in the DATA step (Macro Language Operators). However, in the macro language, there is no MAX or MIN operator, and it does not recognize ':', as does the DATA step. The order in which operations are performed when an expression is evaluated is the same in the macro language as in the DATA step. Operations within parentheses are performed first.
Note: Expressions in which comparison operators surround a macro expression, as in 10<&X<20, might be the equivalent of a DATA step compound expression (depending on the expression resolution). To be safe, specify the connecting operator, as in the expression 10<&X AND &X<20.
Macro Language Operators
Operator
Mnemonic
Precedence
Definition
Example
**
1
exponentiation
2**4
+
2
positive prefix
+(A+B)
-
2
negative prefix
-(A+B)
¬^~
NOT
3
logical not*
NOT A
*
4
multiplication
A*B
/
4
division
A/B
+
5
addition
A+B
-
5
subtraction
A-B
<
LT
6
less than
A<B
<=
LE
6
less than or equal
A<=B
=
EQ
6
equal
A=B
#
IN
6
equal to one of a list**
A#B C D E
¬= ^= ~=
NE
6
not equal*
A NE B
>
GT
6
greater than
A>B
>=
GE
6
greater than or equal
A>=B
&
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. See MINDELIMITER= System Option for more information.
** Before using the IN (#) operator, see MINOPERATOR System Option.
** When you use the IN operator, both operands must contain a value. If the operand contains a null value, an error is generated.
CAUTION:
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.