Chapter Contents
Chapter Contents
Previous
Previous
Next
Next
Language Reference

IF-THEN/ELSE Statement

conditionally executes statements

IF expression THEN statement1;
ELSE statement2;

The inputs to the IF-THEN/ELSE statements are
expression
is an expression that is evaluated for being true or false.
statement1
is a statement executed when expression is true.
statement2
is a statement executed when expression is false.
The IF statement contains an expression to be evaluated, the keyword THEN, and an action to be taken when the result of the evaluation is true.

The ELSE statement optionally follows the IF statement and gives an action to be taken when the IF expression is false. The expression to be evaluated is often a comparison, for example,

   if max(a)<20 then p=0;
   else p=1;
The IF statement results in the evaluation of the condition MAX(A)<20. If the largest value found in matrix A is less than 20, P is set to 0. Otherwise, P is set to 1. See the description of the MAX function for details.

When the condition to be evaluated is a matrix expression, the result of the evaluation is another matrix. If all values of the result matrix are nonzero and nonmissing, the condition is true; if any element in the result matrix is 0 or missing, the condition is false. This evaluation is equivalent to using the ALL function.

For example, writing

   if x<y then
      do;
produces the same result as writing
   if all(x<y) then
      do;
IF statements can be nested within the clauses of other IF or ELSE statements. Any number of nesting levels is allowed. Below is an example.
  if x=y then if abs(y)=z then
     do;
CAUTION: Execution of THEN clauses occurs as if you were using the ALL function.

The statements

   if a^=b then do;
and
   if ^(a=b) then do;
are both valid, but the THEN clause in each case is only executed when all corresponding elements of A and B are unequal; that is, when none of the corresponding elements are equal.

Evaluation of the statement

   if any(a^=b) then do;
requires only one element of A and B to be unequal for the expression to be true.

Chapter Contents
Chapter Contents
Previous
Previous
Next
Next
Top
Top

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