How to Rename SAS Datasets (In #3 Simple Ways)

Changing the dataset names or creating backup of existing data sets are kind of basic tasks you do as a developer. It’s often used when you do investigation on finding data errors or making changes in the existing solutions.

SAS has different ways to rename the variables and its labels but here in this article you will learn how to rename sas dataset using 3 simple methods.

You can rename the SAS dataset using the CHANGE statement. The statement starts with the “CHANGE” keyword in the SAS proc datasets procedure followed by old or existing dataset name and new dataset name which you want to keep. 

Here are exactly three methods you can use to rename any SAS dataset.

1. Rename SAS Dataset using PROC DATASETS and CHANGE statement

This is the most preferred method when it comes to renaming the existing dataset in SAS. It’s an easy and fastest way to get rid of old dataset names. 

You don’t need to create a new dataset in order to rename it. That’s the biggest advantage of using this method.

Syntax:

				/* Method 1: Rename a sas dataset Using proc datasets...change*/

proc datasets lib=libref;
	change old_name=new_name;
	run;

			

Example:

For the demonstration purpose create a new dataset as “sas_dataset” in the WORK library as follows:

				/*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;
			

Now we know that you have a dataset “sas_dataset” in the WORK library and you want to change the name of this dataset to “new_sas_dataset”.

Let’s use the PROC DATASETS procedure with the CHANGE statement. You can refer to the above syntax mentioned earlier.

				/* rename a dataset from sas_dataset to new_sas_dataset */

proc datasets lib=work;
	change sas_dataset=new_sas_dataset;
	run;
			
rename dataset name in sas

BULK Rename Multiple Datasets

The above mentioned method is great when it comes to renaming one dataset but what if you want to rename multiple data sets?

For the demonstration let’s create few more datasets in WORK library.

				/* create multiple datasets for the demo */

data sas_dataset2 sas_dataset3 sas_dataset4 sas_dataset5 sas_dataset6;
	set sas_dataset;
run;

/* view list of data sets from WORK library */
proc datasets lib=WORK memtype=DATA;
run;

			
list of datasets from SAS library

To bulk rename SAS datasets you need to adjust the PROC DATASETS procedure a little bit to adapt the required list of data sets. The CHANGE statement works for the list of the data sets as well.

				/* BULK rename multiple sas data sets in one go */

proc datasets lib=work;
	change sas_dataset  = new_sas_dataset
		   sas_dataset2 = new_sas_dataset2
		   sas_dataset3 = new_sas_dataset3
		   sas_dataset4 = new_sas_dataset4
		   ;
	run;
			
BULK rename sas dataset names

Did you know how to add prefix or suffix to all the data sets from a sas library?

2. Rename SAS dataset using SAS DATA Step 

This is another famous method which is being used to rename the dataset. In this method you kind of go through different routes by creating entirely separate dataset with a new name. 

This is how it works:

  1. You create a copy of original dataset but with a new name
  2. Verify everything which you want in the new dataset
  3. Delete original dataset

Syntax:

				/* create copy of a sas dataset but with different name */

data new_dataset;
    set original_dataset:
run;

			

Example:

				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;

/* create a new dataset using existing dataset */
data work.new_sas_dataset;
	set work.sas_dataset;
run;
			
Rename a sas dataset Using sas data step

At this point you have your original dataset as well as a new dataset with a new desired dataset name. Once you verify everything then you’re good to go to delete the original dataset and start using the new dataset.

				/* drop original dataset */
proc sql;
	drop table work.sas_dataset;
quit;

			

3. Rename a SAS dataset using PROC SQL 

This is the replica of the above SAS data step method. It does exactly the same but with the PROC SQL procedure. Instead of using data steps, you can use proc sql, follow the same steps to rename SAS dataset.

Syntax:

				proc sql;
create table new_table as 
    select * from original_dataset;
quit;

			

Example:

				/* create a new dataset using original dataset*/

proc sql;
	create table work.new_sas_dataset as select * from work.sas_dataset;

	drop table work.sas_dataset;
quit;

			
Rename a sas dataset Using proc sql

You can add a drop statement immediately after create table statement within the proc sql procedure.

FAQ

How to rename a sas dataset?

You can use CHANGE statement in proc datasets procedure to modify dataset to rename the dataset. It’s also possible to achieve the same without touching original dataset but creating a new copy with different name using PROC SQL. 

 

You can delete original dataset later ones you verify everything you’ve got in the new dataset and initial original dataset is no longer needed.

How to rename sas data sets in bulk?

You can rename multiple data sets in one go using PROC DATASETS and RENAME statement. You just need to pass all the dataset names which you want to rename it and everything else will be taken care by proc datasets procedure.

 

It’s also possible to rename all the datasets by adding prefix or suffix to the dataset names.