Previous Page | Next Page

SAS Variables

Dropping, Keeping, and Renaming Variables


Using Statements or Data Set Options

The DROP, KEEP, and RENAME statements or the DROP=, KEEP=, and RENAME= data set options control which variables are processed or output during the DATA step. You can use one or a combination of these statements and data set options to achieve the results you want. The action taken by SAS depends largely on whether you

The following table summarizes the general differences between the DROP, KEEP, and RENAME statements and the DROP=, KEEP=, and RENAME= data set options.

Statements versus Data Set Options for Dropping, Keeping, and Renaming Variables
Statements Data Set Options
apply to output data sets only. apply to output or input data sets.
affect all output data sets. affect individual data sets.
can be used in DATA steps only. can be used in DATA steps and PROC steps.
can appear anywhere in DATA steps. must immediately follow the name of each data set to which they apply.


Using the Input or Output Data Set

You must also consider whether you want to drop, keep, or rename the variable before it is read into the program data vector or as it is written to the new SAS data set. If you use the DROP, KEEP, or RENAME statement, the action always occurs as the variables are written to the output data set. With SAS data set options, where you use the option determines when the action occurs. If the option is used on an input data set, the variable is dropped, kept, or renamed before it is read into the program data vector. If used on an output data set, the data set option is applied as the variable is written to the new SAS data set. (In the DATA step, an input data set is one that is specified in a SET, MERGE, or UPDATE statement. An output data set is one that is specified in the DATA statement.) Consider the following facts when you make your decision:

The following table summarizes the action of data set options and statements when they are specified for input and output data sets. The last column of the table tells whether the variable is available for processing in the DATA step. If you want to rename the variable, use the information in the last column.

Status of Variables and Variable Names When Dropping, Keeping, and Renaming Variables
Where Specified Data Set Option or Statement Purpose Status of Variable or Variable Name
Input data set DROP=

KEEP=

includes or excludes variables from processing if excluded, variables are not available for use in DATA step

RENAME= changes name of variable before processing use new name in program statements and output data set options; use old name in other input data set options
Output data set DROP, KEEP specifies which variables are written to all output data sets all variables available for processing

RENAME changes name of variables in all output data sets use old name in program statements; use new name in output data set options

DROP=

KEEP=

specifies which variables are written to individual output data sets all variables are available for processing

RENAME= changes name of variables in individual output data sets use old name in program statements and other output data set options


Order of Application

If your program requires that you use more than one data set option or a combination of data set options and statements, it is helpful to know that SAS drops, keeps, and renames variables in the following order:


Examples of Dropping, Keeping, and Renaming Variables

The following examples show specific ways to handle dropping, keeping, and renaming variables:

Previous Page | Next Page | Top of Page