Chapter Contents

Previous

Next
SELECT

SELECT



Executes one of several statements or groups of statements

Valid: in a DATA step
Category: Control
Type: Executable


Syntax
Arguments
Details
Comparisons
Examples
Example 1: Using Statements
Example 2: Using DO Groups
Example 3: Using a Compound Expression
Example 4: Making Comparisons for Equality
See Also

Syntax

SELECT <(select-expression)>;
WHEN-1 (when-expression-1 <..., when-expression-n>) statement;
<... WHEN-n (when-expression-1 <..., when-expression-n>) statement;>
<OTHERWISE statement;>
END;

Arguments

(select-expression)
specifies any SAS expression that evaluates to a single value.

(when-expression)
specifies any SAS expression, including a compound expression. SELECT requires you to specify at least one when-expression.
Tip: Separating multiple when-expressions with a comma is equivalent to separating them with the logical operator OR.
Tip: The way a when-expression is used depends on whether a select-expression is present.
Explanation: If the select-expression is present, SAS evaluates the select-expression and when-expression. SAS compares the two for equality and returns a value of true or false. If the comparison is true, statement is executed. If the comparison is false, execution proceeds either to the next when-expression in the current WHEN statement, or to the next WHEN statement if no more expressions are present. If no WHEN statements remain, execution proceeds to the OTHERWISE statement, if one is present. If the result of all SELECT-WHEN comparisons is false and no OTHERWISE statement is present, SAS issues an error message and stops executing the DATA step.

If no select-expression is present, the when-expression is evaluated to produce a result of true or false. If the result is true, statement is executed. If the result is false, SAS proceeds to the next when-expression in the current WHEN statement, or to the next WHEN statement if no more expressions are present, or to the OTHERWISE statement if one is present. (That is, SAS performs the action that is indicated in the first true WHEN statement.) If the result of all when-expressions is false and no OTHERWISE statement is present, SAS issues an error message. If more than one WHEN statement has a true when-expression, only the first WHEN statement is used; once a when-expression is true, no other when-expressions are evaluated.

statement
can be any executable SAS statement, including DO, SELECT, and null statements. You must specify the statement argument.


Details

The SELECT statement begins a SELECT group; SELECT groups contain WHEN statements that identify SAS statements that are executed when a particular condition is true. Use at least one WHEN statement in a SELECT group. An optional OTHERWISE statement specifies a statement to be executed if no WHEN condition is met. An END statement ends a SELECT group.

Null statements that are used in WHEN statements cause SAS to recognize a condition as true without taking further action. Null statements that are used in OTHERWISE statements prevent SAS from issuing an error message when all WHEN conditions are false.


Comparisons


Examples

Example 1: Using Statements

select (a);
   when (1) x=x*10;
   when (2);
   when (3,4,5) x=x*100;
   otherwise;
end;

Example 2: Using DO Groups

select (payclass);
   when ('monthly') amt=salary;
   when ('hourly')
      do;
         amt=hrlywage*min(hrs,40);
         if hrs>40 then put 'CHECK TIMECARD';
      end;         /* end of do     */
   otherwise put 'PROBLEM OBSERVATION';
end;               /* end of select */

Example 3: Using a Compound Expression

select;
   when (mon in ('JUN', 'JUL', 'AUG') 
   and temp>70) put 'SUMMER ' mon=;
   when (mon in ('MAR', 'APR', 'MAY')) 
   put 'SPRING ' mon=;
   otherwise put 'FALL OR WINTER ' mon=;
end;

Example 4: Making Comparisons for Equality

   /* INCORRECT usage to select value of 2 */
select (x);
   /* evaluates T/F and compares for       */
   /* equality with x                      */
   when (x=2) put 'two';    
end;                        


   /* correct usage */
select(x);
   /* compares 2 to x for equality */
   when (2) put 'two';     
end;


   /* correct usage */
select;
   /* compares 2 to x for equality         */
   when (x=2) put 'two';    
end;

See Also

Statements:

DO
IF, Subsetting
IF-THEN/ELSE


Chapter Contents

Previous

Next

Top of Page

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