IF-THEN/ELSE Statement

IF expression THEN statement1 ;
ELSE statement2 ;

The IF-THEN/ELSE statement conditionally executes statements. The ELSE statement is optional.

The arguments to the IF-THEN/ELSE statement are as follows:

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:

a = {0, 5, 1, 10};
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 the matrix a is less than 20, the scalar value 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, consider the following statements:

a = { 1 2, 3 4};
b = {-1 0, 0 1};
if a>b then do;
   /* statements */
end;

This code produces the same result as the following statements:

if all(a>b) then do;
   /* statements */
end;

IF statements can be nested within the clauses of other IF or ELSE statements. There is no limit on the number of nesting levels. Consider the following example:

if a>b then 
   if a>abs(b) then do;
      /* statements */
   end;

Consider the following statements:

if a^=b then do;
   /* statements */
end;
if ^(a=b) then do;
   /* statements */
end;

The two IF statements are equivalent. In each case, the THEN clause is executed only when all corresponding elements of a and b are unequal.

Evaluation of the following statement requires only one element of a and b to be unequal in order for the expression to be true:

if any(a^=b) then do;
   /* statements */
end;