Published on :
DATA Step CREATION_INTERNE

KEEP Statement

This code is also available in: Deutsch Español Français
Awaiting validation
The KEEP statement enables a DATA step to write only the specified variables to one or more SAS© data sets. It applies to all SAS© data sets created in the same DATA step and can appear anywhere in the step. If no KEEP or DROP statement is present, all data sets created in the DATA step contain all variables. If the same variable is listed in both DROP and KEEP statements, DROP takes precedence over KEEP, regardless of the order of the statements, and the variable is dropped. It is advisable not to use both KEEP and DROP statements in the same DATA step.
Comparisons:
  • The KEEP statement cannot be used in SAS© PROC steps, unlike the KEEP= data set option.
  • The KEEP statement applies to all output data sets named in the DATA statement. To write different variables to different data sets, you must use the KEEP= data set option.
  • The DROP statement is a parallel statement that specifies which variables to omit from output data sets.
  • KEEP and DROP statements select variables to include or exclude from output data sets. The subsetting IF statement selects observations.
  • Do not confuse the KEEP statement with the RETAIN statement. The RETAIN statement causes SAS© to retain a variable's value from one iteration of the DATA step to the next. The KEEP statement does not affect the value of variables, but only specifies which variables to include in output data sets.
Data Analysis

Type : CREATION_INTERNE


Examples use generated data (datalines).

1 Code Block
DATA STEP
Explanation :
These examples show the correct syntax for listing variables in the KEEP statement.
Copied!
1keep name address city state zip phone;
2keep rep1-rep5;
3 
2 Code Block
DATA STEP Data
Explanation :
This example uses the KEEP statement to include only the NAME and AVG variables in the output data set. Variables SCORE1 through SCORE20, from which AVG is calculated, are not written to the AVERAGE data set.
Copied!
1DATA scores;
2 INPUT name $ score1-score20;
3 DATALINES;
4John 10 12 15 11 14 13 16 10 18 12 14 11 13 15 10 12 11 13 14 10 16
5Jane 15 14 13 16 12 11 10 17 13 15 12 14 11 16 13 10 12 11 14 15
6;
7RUN;
8 
9DATA average;
10 SET scores;
11 keep name avg;
12 avg=mean(of score1-score20);
13RUN;
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


Banner
Expert Advice
Expert
Simon
Expert SAS et fondateur.
« Remember that KEEP only controls what is written to the output. All variables remain available for calculations throughout the duration of the DATA step, regardless of where the KEEP statement is placed. »