Published on :
Flow Control CREATION_INTERNE

Example: SELECT WHEN Statement

This code is also available in: Deutsch Español Français
The SELECT WHEN statement allows you to conditionally process one or more statements in a statement group based on the value of a single categorical variable. A group of statements between the SELECT and END statements is called a SELECT group. SELECT groups contain WHEN statements that identify the SAS© statements 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 in an OTHERWISE statement, the SELECT WHEN statement prevents SAS© from issuing an error message.
Data Analysis

Type : CREATION_INTERNE


The examples use data from the SASHELP library (sashelp.shoes).

1 Code Block
DATA STEP
Explanation :
This example creates a new dataset 'shoes' from `sashelp.shoes`. It filters the data to include only the 'Canada', 'Pacific', and 'Asia' regions. Then, it uses the SELECT WHEN statement to apply different discounts to sales based on the region: 10% for Canada, 9% for Pacific, and 7% for Asia. Finally, `PROC PRINT` displays the contents 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.
Copyright Info : Copyright © SAS Institute Inc. All Rights Reserved