Previous Page | Next Page

Statements

SELECT Statement



Executes one of several statements or groups of statements.
Valid: in a DATA step
Category: Control
Type: Executable

Syntax
Arguments
Details
Using WHEN Statements in a SELECT Group
Evaluating the when-expression When a select-expression Is Included
Evaluating the when-expression When a select-expression Is Not Included
Processing Large Amounts of Data with %INCLUDE Files
Comparisons
Examples
Example 1: Using Statements
Example 2: Using DO Groups
Example 3: Using a Compound Expression
Example 4: Making Comparisons for Equality
Example 5: Processing Large Amounts of Data
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.

See: Evaluating the when-expression When a select-expression Is Included
(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.
See: Evaluating the when-expression When a select-expression Is Not Included
statement

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


Details


Using WHEN Statements in a SELECT Group

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.


Evaluating the when-expression When a select-expression Is Included

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.


Evaluating the when-expression When a select-expression Is Not Included

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.


Processing Large Amounts of Data with %INCLUDE Files

One way to process large amounts of data is to use %INCLUDE statements in your DATA step. Using %INCLUDE statements enables you to perform complex processing while keeping your main program manageable. The %INCLUDE files that you use in your main program can contain WHEN statements and other SAS statements to process your data. See Processing Large Amounts of Data for an example.


Comparisons

Use IF-THEN/ELSE statements for programs with few statements. Use subsetting IF statements without a THEN clause to continue processing only those observations or records that meet the condition that is specified in the IF clause.


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;


Example 5: Processing Large Amounts of Data

In the following example, the %INCLUDE statements contain code that includes WHEN statements to process new and old items in the inventory. The main program shows the overall logic of the DATA step.

data test (keep=ItemNumber);
   set ItemList;
   select;
      %include NewItems;
      %include OldItems;
      otherwise put 'Item ' ItemNumber ' is not in the inventory.';
   end;
run;


See Also

Statements:

DO Statement

IF Statement, Subsetting

IF-THEN/ELSE Statement

Previous Page | Next Page | Top of Page