|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
|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.
|Where Specified||Data Set Option or Statement||Purpose||Status of Variable or Variable Name|
|Input data set||DROP=||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=||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|
This example uses the DROP= and RENAME= data set options and the INPUT function to convert the variable POPRANK from character to numeric. The name POPRANK is changed to TEMPVAR before processing so that a new variable POPRANK can be written to the output data set. Note that the variable TEMPVAR is dropped from the output data set and that the new name TEMPVAR is used in the program statements.
This example uses the DROP statement and the DROP= data set option to control the output of variables to two new SAS data sets. The DROP statement applies to both data sets, CORN and BEAN. You must use the RENAME= data set option to rename the output variables BEANWT and CORNWT in each data set.