Published on :
Control Flow CREATION_INTERNE

Example: SELECT WHEN Statement

This code is also available in: Deutsch Español
The SELECT WHEN statement allows you to conditionally process one or more statements within a group of statements based on the value of a single categorical variable. A group of statements between SELECT and END statements is called a SELECT group. SELECT groups contain WHEN statements that identify which SAS© statements are executed when a particular condition is true. The END statement and at least one WHEN statement are required when specifying the SELECT statement. Using a SELECT group is more efficient than using a series of IF-THEN statements when you have a long series of mutually exclusive conditions. A large number of conditions makes a SELECT group more efficient than IF-THEN/ELSE statements because CPU time is reduced. SELECT groups also make the program easier to read and debug. An optional OTHERWISE statement specifies a statement to execute if no WHEN condition is met. For example, if your data contains missing or invalid data. In null statements used within an OTHERWISE statement, the SELECT WHEN statement prevents SAS© from issuing an error message.
Data Analysis

Type : CREATION_INTERNE


Examples use generated data (datalines) or SASHELP.

1 Code Block
DATA STEP
Explanation :
This SAS code uses a DATA step to create a new 'shoes' dataset from 'sashelp.shoes'. It filters observations where the region is 'Canada', 'Pacific', or 'Asia'. Then, it uses a SELECT WHEN statement to apply different sales reductions based on the region: 10% for 'Canada', 9% for 'Pacific', and 7% for 'Asia'. Finally, a PROC PRINT is used to display the content of the modified dataset.
Copied!
1DATA shoes;
2 SET sashelp.shoes(where=(Region='Canada' or Region='Pacific' or Region='Asia'));
3 keep Region Sales Product;
4 select(Region);
5 when('Canada') sales = sales * .10;
6 when('Pacific') sales = sales * .09;
7 when('Asia') sales = sales * .07;
8 END;
9RUN;
10PROC PRINT DATA=shoes; RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.