This tutorial explains how to rename variables in SAS using the RENAME option, along with examples.
The syntax of RENAME option is as follows :
RENAME=(variable1=new_variable1 variable2=new_variable2 ...)
Rename One Variable in SAS
The following code renames the variable "petallength" to "petal_length" in the dataset "sashelp.iris" and create a new dataset called "mydata" with the renamed variable.
data mydata (rename=(petallength = petal_length)); set sashelp.iris; run;
To check if a variable has been renamed correctly, we can use PROC CONTENTS to see the variable names of a dataset.
proc contents data=sashelp.iris SHORT; proc contents data=mydata SHORT;
The RENAME option can be used with either the DATA statement or the SET statement, depending on your specific needs.
- RENAME option in the DATA statement: When you use the RENAME option in the DATA statement, you are renaming variables in the output dataset.
- RENAME option in the SET statement: When you use the RENAME option in the SET statement, you are renaming variables while reading data from an input dataset.
The following code returns missing values for a new variable "newvar" because "petal_length" was not renamed before creating the new variable "newvar". Hence rename=
option should be used in the SET statement instead of DATA statement here.
data mydata (rename=(petallength = petal_length)); set sashelp.iris; newvar = petal_length * 10; run;Correct Code
data mydata; set sashelp.iris (rename=(petallength = petal_length)); newvar = petal_length * 10; run;
Rename Multiple Variables in SAS
The following code renames the variables "petallength" to "petal_length" and "sepallength" to "sepal_length" while reading the data from the "sashelp.iris" dataset.
data mydata; set sashelp.iris (rename=(petallength = petal_length sepallength=sepal_length)); run; proc contents data=sashelp.iris SHORT; proc contents data=mydata SHORT;
Rename All Variables in SAS
The following code renames all variables in the "sashelp.iris" dataset by adding the suffix "_1" to their original names.
proc sql noprint; select cats(name,"=",name,"_1") into :rename_vars separated by " " from dictionary.columns where libname="SASHELP" and memname="IRIS"; quit; %PUT &rename_vars.; data mydata; set sashelp.iris (rename=(&rename_vars.)); run;
- The above code uses the table
dictionary.columns
which contains information about the column names of all datasets in the current SAS session. - By using the
WHERE
statement, we filtered this table and selected only column names in the "sashelp.iris" dataset and stored them in the macro variable "rename_vars" - The
%PUT
statement is used to display the contents of the macro variable "rename_vars" in the log. It helps you to check what the macro variable contains. - In the subsequent DATA step, the SET statement reads data from the "sashelp.iris" dataset and applies the variable rename assignments specified in the macro variable "rename_vars"
- The resulting dataset "mydata" will contain the renamed variables with suffix "_1".
Rename Variables based on a Pattern
The following code renames only those variables which contain "Length" in their names using CONTAINS operator.
proc sql noprint; select cats(name,"=",name,"_1") into :rename_vars separated by " " from dictionary.columns where libname="SASHELP" and memname="IRIS" and name contains 'Length'; quit; %PUT &rename_vars.; data mydata; set sashelp.iris (rename=(&rename_vars.)); run;
Share Share Tweet