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 that you want. The action taken by SAS depends largely on whether you perform
one of the following actions:
Use a statement or data set option or both.
Specify the data set options on an input or an output data set.
The following table summarizes the general differences between the DROP, KEEP, and
RENAME statements and the DROP=, KEEP=, and RENAME= data set options.
Table 4.6 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:
If variables are not written to the output data set and they do not require any
processing, using an input data set option to exclude them from the DATA step is
more efficient.
If you want to rename a variable before processing it in a DATA step, you must use
the RENAME= data set option in the input data set.
Dropping, Keeping, and Renaming Variables 53
If the action applies to output data sets, you can use either a statement or a data set
option in the output data set.
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.
Table 4.7 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:
First, options on input data sets are evaluated left to right within SET, MERGE, and
UPDATE statements. DROP= and KEEP= options are applied before the
RENAME= option.
Next, DROP and KEEP statements are applied, followed by the RENAME
statement.
54 Chapter 4 SAS Variables

Get SAS 9.4 Language Reference, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.