Chapter Contents |
Previous |
Next |
SAS Macro Language: Reference |
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.
Comparing Numeric Operands in Logical Expressions |
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;
Invoking the COMPNUM macro with these values
%compnum(1,2) %compnum(-1,0)
displays these results 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.
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 %sysevalf(&first is less than &second); %mend compflt;
Invoking the COMPFLT macro with these values
%compflt (1.2,.9) %compflt (-.1,.) %compflt (0,.)
places these values 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 COMPC macro. Invoking the COMPC 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;
Invoking the macro COMPCHAR with these values
%compchar(a,b) %compchar(.,1) %compchar(Z,E)
prints these results in the log:
a comes before b . comes before 1 Z comes after EWhen 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.
If you invoke the COMPNUM macro, shown earlier, with these values
%compnum(10,2.0)
then these 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 may have smaller values than uppercase, and uppercase letters may have smaller values than digits.
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.