5.3 - Alternative Actions

As the output from Example 5.1 illustrates, there may be occasions when you want to use an if-then-else statement instead of just an if-then statement. In that example, we told SAS only what to do if the condition following the IF keyword was true. By including an else statement, we can tell SAS what to do if the condition following the IF keyword is false.

Example 5.3 Section

The following SAS program creates a character variable status, whose value is "Failed" IF the student's first exam grade is less than 65, otherwise (i.e., ELSE) the value is "Passed":

DATA grades;
	input name $ 1-15 e1 e2 e3 e4 p1 f1;
	* if the first exam is less than 65 indicate failed;
	if (e1 < 65) then status = 'Failed';
	* otherwise indicate passed;
	else status = 'Passed';
Alexander Smith  78 82 86 69  97 80
John Simon       88 72 86  . 100 85
Patricia Jones   98 92 92 99  99 93
Jack Benedict    54 63 71 49  82 69
Rene Porter     100 62 88 74  98 92

PROC PRINT data = grades;
	var name e1 status;

Launch and run  the SAS program. Review the output from the PRINT procedure to convince yourself that the values of the character variable status have been assigned correctly.

Note that, in general, using ELSE statements with IF-THEN statements can save resources:

  • Using IF-THEN statements without the ELSE statement causes SAS to evaluate all IF-THEN statements.
  • Using IF-THEN statements with the ELSE statement causes SAS to execute IF-THEN statements until it encounters the first true statement. Subsequent IF-THEN statements are not evaluated.

For greater efficiency, you should construct your IF-THEN-ELSE statements with conditions of decreasing probabilities.