Sentry Page Protection
SAS Functions [13-14]
RENAME, KEEP & DROP STATEMENTS
You can change the name of a variable using the RENAME statement.
The variable names in this data set (i.e. FIRST_NAME, LAST_NAME and PARTICIPANTS_AGE) are particularly long.
We can rename the variable names using the RENAME statement.
Example
Data Profile2;
Set Profile;
Rename First_Name = First
Last_Name = Last
Participants_age = age;
Run;
Note: it is highly recommended to name the variable in no more than 8 characters.
Long variable name not only takes longer to type but also leads to more programming errors.
KEEP & DROP STATEMENTS
The KEEP and DROP statements allow you to, well, keep and drop variables!
The two examples below create the exact same data set:
Example
Data Profile2;
Set Profile;
Keep First_Name Last_Name;
Run;
Data Profile2;
Set Profile;
Drop Participants_age;
Run;
Exercise
Locate the MARGARIN data set from the SASHELP library.
The MARGARIN data set contains 7 variables.
Write a SAS program to create a data set that contains only the HOUSEID, CHOICE and BRAND variables.
Rename the HOUSEID variable to just ID.
Locate the MARGARIN data set from the SASHELP library.
The MARGARIN data set contains 7 variables.
Write a SAS program to create a data set that contains only the HOUSEID, CHOICE and BRAND variables.
Rename the HOUSEID variable to just ID.
Need some help?
HINT:
The KEEP statement is the better function to use when there is a small number of variables to keep in the data set.
SOLUTION:
Data Margarin;
Set SASHelp.Margarin;
Keep HouseID Choice Brand;
Rename HouseID = ID;
Run;
Fill out my online form.