Chapter Contents


SAS Component Language: Reference


The SELECT statement executes one of several statements or groups of statements based on the value of the expression that you specify.

WHEN-1 (when-expression-1) statement(s);
<WHEN-n (when-expression-n) statement(s);>
<OTHERWISE statement;>
SAS evaluates select-expression, if present, as well as when-expression-1. If the values of both expressions are equal, then SAS executes the statements associated with when-expression-1. If the values are not equal, then SAS evaluates when-expression-n, and if the values of select-expression-1 and when-expression-1 are equal, SAS executes the statements associated with when-expression-n. SAS evaluates each when expression until it finds a match or until it has evaluated all of the when expressions without finding a match. If you do not specify a select expression, then SAS evaluates each when expression and executes only the statements associated with the first when expression that evaluates to true.

If the value of none of the when expressions matches the value of the select expression, or if you do not specify a select expression and all of the when expressions are false, then SAS executes the statements associated with the OTHERWISE statement. If you do not specify an OTHERWISE statement, the program halts.

In SCL applications, you cannot specify a series of when expressions separated by commas in the same WHEN statement. However, separating multiple WHEN statements with a comma is equivalent to separating them with the logical operator OR, which is acceptable in SCL applications.

The statements associated with a when expression can be any executable SAS statement, including SELECT and null statements. A null statement in a WHEN statement causes SAS to recognize a condition as true and to take no additional action. A null statement in an OTHERWISE statement prevents SAS from issuing an error message when all of the when expressions are false.

Each WHEN statement implies a DO group of all statements until the next WHEN or OTHERWISE statement. Therefore the following program is valid:

select (paycat);
   when ('monthly') 
   when ('hourly') 
     if hrs>40 then put 'Check timecard.';
   otherwise put 'problem observation';
However, if you need to include a LEAVE statement as part of your WHEN statement, then you must explicitly specify the DO statement in your WHEN statement.

You can specify expressions and their possible values in either of the following ways:

  1. SELECT;
    WHEN (variable operator value) statement(s);

  2. SELECT (variable);
    WHEN (value) statement(s);

For example, both of the following SELECT statements are correct:
  when (x<=5) put '1 to 5';
  when (x>=6) put '6 to 10';

select (x);
  when (1) put 'one';
  when (2) put 'two';
The following code is incorrect because it compares the value of the expression X with the value of the expression X=1. As described in Boolean Numeric Expressions, in Boolean expressions, a value of 0 is false and a value of 1 is true. Therefore, the expression X is false and the expression X=1 is false, so the program prints x is 1.
select (x);
  when (x=0) put 'x is 0';
  when (x=1) put 'x is 1';
  otherwise put x=;

For more information about the SELECT statement, refer to SELECT and to SAS Language Reference: Dictionary.

Chapter Contents



Top of Page

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