How the Macro Processor Evaluates Logical Expressions

Comparing Numeric Operands in Logical Expressions

A logical, or Boolean, expression returns a value that is evaluated as true or false. In the macro language, any numeric value other than 0 is true and a value of 0 is false.
When the macro processor evaluates logical expressions that contain operands that represent numbers, it converts the characters temporarily to numeric values. To illustrate how the macro processor evaluates logical expressions with numeric operands, consider the following macro definition:
%macro compnum(first,second);
   %if &first>&second %then %put &first is greater than &second;
   %else %if &first=&second %then %put &first equals &second;
   %else %put &first is less than &second;
%mend compnum;
Invoke the COMPNUM macro with these values:
%compnum(1,2)
%compnum(-1,0)
The following results are displayed in the log:
1 is less than 2
-1 is less than 0
The results show that the operands in the logical expressions were evaluated as numeric values.

Comparing Floating-Point or Missing Values

You must use the %SYSEVALF function to evaluate logical expressions containing floating-point or missing values. To illustrate comparisons with floating-point and missing values, consider the following macro that compares parameters passed to it with the %SYSEVALF function and places the result in the log:
%macro compflt(first,second);
   %if %sysevalf(&first>&second) %then %put &first is greater than
&second;
   %else %if  %sysevalf(&first=&second) %then %put &first equals
&second;
   %else %put &first is less than &second;
%mend compflt;
Invoke the COMPFLT macro with these values:
%compflt (1.2,.9)
%compflt (-.1,.)
%compflt (0,.)
The following values are written in the log:
1.2 is greater than .9
-.1 is greater than .
0 is greater than .
The results show that the %SYSEVALF function evaluated the floating-point and missing values.

Comparing Character Operands in Logical Expressions

To illustrate how the macro processor evaluates logical expressions, consider the COMPCHAR macro. Invoking the COMPCHAR macro compares the values passed as parameters and places the result in the log.
%macro compchar(first,second);
   %if &first>&second %then %put &first comes after &second;
   %else %put &first comes before &second;
%mend compchar;
Invoke the macro COMPCHAR with these values:
%compchar(a,b)
%compchar(.,1)
%compchar(Z,E)
The following results are printed in the log:
a comes before b
. comes before 1
Z comes after E
When the macro processor evaluates expressions with character operands, it uses the sort sequence of the host operating system for the comparison. The comparisons in these examples work with both EBCDIC and ASCII sort sequences.
A special case of a character operand is an operand that looks numeric but contains a period character. If you use an operand with a period character in an expression, both operands are compared as character values. This can lead to unexpected results. So that you can understand and better anticipate results, look at the following examples.
Invoke the COMPNUM macro with these values:
%compnum(10,2.0)
The following values are written to the log:
10 is less than 2.0
Because the %IF-THEN statement in the COMPNUM macro uses integer evaluation, it does not convert the operands with decimal points to numeric values. The operands are compared as character strings using the host sort sequence, which is the comparison of characters with smallest-to-largest values. For example, lowercase letters might have smaller values than uppercase, and uppercase letters might have smaller values than digits.
CAUTION:
The host sort sequence determines comparison results.
If you use a macro definition on more than one operating system, comparison results might differ because the sort sequence of one host operating system might differ from the other system. See the SORT Procedure in Base SAS Procedures Guide for more information about host sort sequences.