Flow Control

Cleaner, Faster, Better: Refactoring Your SAS Logic with SELECT Groups

This code is also available in: Deutsch Español Français
Difficulty Level
Beginner
Published on :
Michael

Expert Advice

Michael
Responsable de l'infrastructure Viya.

Unlike IF-THEN logic, which simply moves to the next observation if no condition is true, the SELECT statement in SAS will trigger a fatal error and stop the Data Step if it encounters a value that matches none of the WHEN clauses; therefore, you should always include an OTHERWISE statement (even if it is just a null statement like 'otherwise;') to handle unexpected values safely.

Stop writing endless IF-THEN-ELSE chains for categorical variables.
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;
Pro Tip
If you want to do "nothing" when no condition is met (to prevent SAS from issuing an error), you can use a null statement in the OTHERWISE clause: otherwise;.
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


Related Documentation

Aucune documentation spécifique pour cette catégorie.