Previous PageNext Page

Example 2: The SELECT-WHEN structure
Another - more efficient - way to code statements to execute conditionally is with the SELECT-WHEN structure. The following code achieves results identical to that above using the IF-THEN/ELSE structure.

Data Grades;
     Input Score;
     ID = _N_;
     SELECT;
       WHEN ( 0 LE Score LT 65) Grade='F';
       WHEN (65 LE SCORE LT 70) Grade='D';
       WHEN (70 LE SCORE LT 80) Grade='C';
       WHEN (80 LE SCORE LT 90) Grade='B';
       WHEN (      SCORE GE 90) Grade='A';
	 OTHERWISE Grade=.;
     END;
Datalines;

Notice that the SELECT block is ended with the END statement.

SELECT-WHEN structures (data step only)
The simplest form of Select is:
SELECT;
      WHEN (when-expression) statement;
      ...
     [ WHEN (when-expression) statement; ]
OTHERWISE statement;
END;
SAS evaluates each when-expression in turn. The first expression that is true, causes the execution of the associated statement. If none are true, the statement in the OTHERWISE is executed. If none are true and no OTHERWISE statement was included in the structure, the data step will be in error: "ERROR: Unsatisfied WHEN clause and no OTHERWISE clause".

For more information see SL:R, Chapter 9 SAS Language Statements

Previous PageTable Of ContentsNext Page