SELECT Statement

Executes one of several statements or groups of statements.

Valid in: DATA step
Categories: CAS
Control
Type: Executable

Syntax

END;

Arguments

(select-expression)

specifies any SAS expression that evaluates to a single value.

Restriction If a variable is specified in the select-expression, an operator cannot be used in a when-expression. For more information, see Using Variables in the Select Statement.
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.

Restriction An operator can be used in a when-expression as long as a variable does not appear in the select-expression. For more information, see Using Variables in the Select Statement.
Tips Separating multiple when-expressions with a comma is equivalent to separating them with the logical operator OR.
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. (That is, SAS performs the action that is indicated in the first true WHEN statement.) If no when-expression evaluates to true, SAS proceeds to the OTHERWISE statement if it is present. 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. After 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.

The SELECT statement works much like the CASE statement in the SQL procedure.

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 this 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;

Example 6: Using Variables in the Select Statement

A variable can be used in an operation in the when-expression if the variable is not specified in the SELECT statement. For example:

data _null_;
   h1=hour(datetime());
   select;
      when  (h1<12) greetp1 = "Morning  ";
   end;
   put greetp1;
run;

This example produces an error because the less than (<) operator that is specified in the when-expression is attempting to operate on the h1 variable that is specified in the select-expression.

data _null_;
h1=hour(datetime());
select (h1);
  when (<12) greetp1 = "Morning  ";
end;
put greetp1;
run;

Here is the SAS log that is produced at the point of the less than (<) operator:

Syntax error, expecting one of the following: a name, a quoted string,
a numeric constant, a datetime constant, a missing value, INPUT, PUT

See Also

Last updated: June 17, 2025