Language Reference

IF-THEN/ELSE Statement

conditionally executes statements

IF expression THEN statement1;
ELSE statement2;

The inputs to the IF-THEN/ELSE statements 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:

  
    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, consider the following code:

  
    if x<y then 
       do;
 
This code produces the same result as the following code:
  
    if all(x<y) then 
       do;
 
IF statements can be nested within the clauses of other IF or ELSE statements. IML imposes no limit on the number of nesting levels. Consider the following 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.

Consider the following statements:

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

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

  
    if any(a^=b) then do;
 

Previous Page | Next Page | Top of Page