Chapter Contents

Previous

Next
SAS Macro Language: Reference

How the Macro Processor Evaluates Arithmetic Expressions

The macro facility is a string handling facility. However, in specific situations, the macro processor can evaluate operands that represent numbers as numeric values. When the macro processor evaluates an expression that contains an arithmetic operator and operands that represent numbers, it temporarily converts the operands to numeric values and performs the integer arithmetic operation. The result of the evaluation is text.


Evaluating Numeric Operands

By default, arithmetic evaluation in most macro statements and functions is performed with integer arithmetic. The exception is the %SYSEVALF function. See Evaluating Floating Point Operands for more information. 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;
%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*/

Because 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:
10.0+20.0


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, these %PUT statements

%let a=2.5;
%put %sysevalf(&a,boolean);
%put %sysevalf(&a,integer);
%put %sysevalf(&a,ceil);
%put %sysevalf(&a,floor);

return 1, 2, 3, and 2, respectively. These conversion types tailor the value returned by %SYSEVALF so that it can be used in other macro expressions that require integer or Boolean values.

CAUTION:
Specify a conversion type for the %SYSEVALF function. If you use the %SYSEVALF function in macro expressions or assign its results to macro variables that are used in other macro expressions, then errors or unexpected results may occur if the %SYSEVALF function returns missing or floating point values. To prevent errors, specify a conversion type that returns a value compatible with other macro expressions. See "%SYSEVALF" in Chapter 13, "Macro Language Dictionary" for more information on using conversion types.  [cautionend]


Chapter Contents

Previous

Next

Top of Page

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