3 Ways to Rename Variables in SAS

SAS has different ways to rename or change the dataset names but in this article you’ll learn 3 ways to rename variables in SAS. 

You can use the RENAME function to rename one or more variables in the SAS dataset using either Data step or PROC DATASETS procedure. It’s also possible to rename and add prefixes or suffixes to all the variables in a SAS dataset.

1. Rename Variables in SAS using Data Step and RENAME statement

This method is being used specially when you’re creating a new dataset by renaming the variables from the SAS dataset. The original dataset remains untouched. 

Here is the simple syntax to rename variables in sas:

				data new_sas_dataset;
	set original_sas_dataset (rename=(old_name=new_name));
run;

			

Example 1. Rename one Variable:

The following example shows how to use the rename function to rename one variable in SAS.

				/*create a new sas dataset*/
data work.sas_dataset;
	input var1 var2 var3;
	datalines;
1 4 76
2 3 49
2 3 85
4 5 88
2 2 90
;
run;

/*print dataset*/
proc print data=sas_dataset;
			
rename one variable in sas using data step
				/* ex.1 Rename one variable var1 to new_var1 */
data new_sas_dataset;
	set sas_dataset (rename=(var1=new_var1));
run;


/*print new dataset after renaming variable var1*/
proc print data=new_sas_dataset;

			
rename one variable in sas

Example 2. Rename Multiple Variables

The following example shows how to use the rename function to rename multiple variables in SAS.

				/* ex.2 Rename multiple variables */
data new_sas_dataset;
	set sas_dataset (rename=(var1=new_var1 var2=new_var2));
run;

/*print new dataset after renaming multiple variables*/
proc print data=new_sas_dataset;
			
rename multiple variables in sas using data step

2. Rename Variables in SAS using PROC DATASETS

In this method you don’t create a new dataset but you modify existing dataset. So be cautious before modifying existing dataset. The PROC DATASETS procedure with MODIFY & RENAME statements allows you to rename variables from the SAS dataset.

Here is the simple syntax to rename variables using proc datasets:

				proc datasets lib=libref;
	modify dataset_name;
	rename old_name=new_name;
quit;
			

Example 1. Rename One Variable

The following example shows how to use the rename function to rename one variable using PROC DATASETS and MODIFY, RENAME statements.

				/* ex.1 Rename one variable var1 to new_var1 */
proc datasets lib=work;
	modify sas_dataset;
	rename var1=new_var1;
quit;


/*print the same dataset after renaming variable var1*/
proc print data=sas_dataset;
			
rename one variable in sas

Example 2: Rename Multiple Variables

The following example shows how to use the rename function to rename multiple variables using PROC DATASETS and MODIFY, RENAME statements.

				/* ex.2 rename multiple variables (var1 & var2)*/
proc datasets lib=work;
	modify sas_dataset;
	rename var1=new_var1 var2=new_var2;
quit;


/*print the same dataset after renaming multiple variables */
proc print data=sas_dataset;

			
rename multiple variables in sas using data step

3. Rename All the Variables from a SAS Dataset

You’ll find yourself in a situation where you want to rename all the variables from a given SAS dataset. This method is very useful when you know the pattern of changing the variable names.

Sometimes you want to add something in the beginning (as prefix) or sometimes you may want to add something at the end (as suffix) to all the variable names. It can be easily achieved through macro variable and proc datasets procedure.

It’s a three step process. 

  1. You create a macro variable that holds everything which comes after the RENAME statement in proc datasets procedure.
  2. Use that macro variable into the PROC DATASETS procedure to pass it after the RENAME statement. 
  3. Verify changes using proc print procedure.

Example 1: Add prefix to all variables in SAS dataset

				/*define prefix to append to each variable in SAS*/
proc sql noprint;
   select cats(name, '=', 'NEW_', name)
          into :list
          separated by ' '
          from dictionary.columns
          where libname = 'WORK' and memname = 'SAS_DATASET';
quit;


/*add prefix to each variable in dataset*/
proc datasets library = work;
   modify sas_dataset;
   rename &list;
quit;


/*view updated dataset with new variable names*/
proc print data=sas_dataset;

			
rename all variables by adding prefix in SAS dataset

Example 2: Add suffix to all variables in SAS dataset

				/*define suffix to append to each variable in SAS*/
proc sql noprint;
   select cats(name, '=', name, '_NEW')
          into :list
          separated by ' '
          from dictionary.columns
          where libname = 'WORK' and memname = 'SAS_DATASET';
quit;


/*add suffix to each variable in dataset*/
proc datasets library = work;
   modify sas_dataset;
   rename &list;
quit;


/*view updated dataset with new variable names*/
proc print data=sas_dataset;
			
rename all variables by adding suffix in SAS dataset

FAQ

How to rename variables in SAS?

You can use the RENAME function to rename one or more variables in the SAS dataset using either Data step or PROC DATASETS procedure.

 

proc datasets lib=libref;
modify dataset_name;
rename old_name=new_name;
quit;
How to rename all the variables in SAS?

You can definitely rename all the variables ones for all using proc datasets procedure along with MODIFY, RENAME statements.

 

proc datasets library = work;
modify sas_dataset;
rename &list;
quit;