Chapter Contents


SAS Macro Language: Reference

Arithmetic and Logical Expressions

You can use arithmetic and logical expressions in specific macro functions and statements (Macro Language Elements that Evaluate Arithmetic and Logical Expressions ). 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;






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 this line 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, which interprets a period character in its argument as a decimal point and converts the operand to a floating point value on your operating system.

Operators in macro expressions are a subset of those in the DATA step (Macro Language Operators). However, in macro, there is no MAX or MIN operator, and macro does not recognize IN or ':', 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, may or may not be the equivalent of a DATA step compound expression (depending on what the expression resolves into). To be safe, write the connecting operator explicitly, as in the expression 10<&X AND &X<20.  [cautionend]

Macro Language Operators
Operator Mnemonic Name Procedence
exponential 1
positive prefix 2
negative prefix 2
¬^~ NOT logical not* 3
multiplication 4
division 4
addition 5
subtraction 5
< LT less than 6
<= LE less than or equal 6
= EQ eqaul 6
¬=^=~= NE not equal* 6
> GT greater than 6
>= GE greater than or equal 6
& AND logical and 7
| OR logical or 8
*The symbol to use depends on your keyboard.

Chapter Contents



Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.