SAS 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 | 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.
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.
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:
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.
Finally, options on output data sets are evaluated left to right within the DATA statement. DROP= and KEEP= options are applied before the RENAME= option.
Examples of Dropping, Keeping, and Renaming Variables |
The following examples show specific ways to handle 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.
data newstate(drop=tempvar); length poprank 8; set state(rename=(poprank=tempvar)); poprank=input(tempvar,8.); run;
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.
data corn(rename=(cornwt=yield) drop=beanwt) bean(rename=(beanwt=yield) drop=cornwt); set harvest; if crop='corn' then output corn; else if crop='bean' then output bean; drop crop; run;
This example shows how to use data set options in the DATA statement and the RENAME statement together. Note that the new name QTRTOT is used in the DROP= data set option.
data qtr1 qtr2 ytd(drop=qtrtot); set ytdsales; if qtr=1 then output qtr1; else if qtr=2 then output qtr2; else output ytd; rename total=qtrtot; run;
Copyright © 2010 by SAS Institute Inc., Cary, NC, USA. All rights reserved.