SAS How To's

3 Simple Ways to Rename a Dataset in SAS

SAS provides different ways to rename variables and labels, but do you know how to change the name of a dataset in SAS?

You can rename a dataset in SAS with the CHANGE statement. The statement starts with the CHANGE keyword, followed by the current table name, an equal sign, and the new table name. The CHANGE statement is part of the PROC DATASETS procedure.

In this article, we discuss the CHANGE statement and two other methods to rename a dataset.

Rename a SAS Dataset with the CHANGE Statement

The easiest way to change the name of a dataset in SAS is with the CHANGE statement. In fact, it’s the only way to rename a dataset without the need to create a new dataset.

This is how to rename a dataset in SAS with the CHANGE statement:

1. Start the DATASETS procedure with the PROC DATASETS keywords.

2. Specify the location (i.e., library) of your dataset with the LIBRARY=-option. If you don’t use this option, SAS assumes that your dataset is in the WORK library.

3. Start the CHANGE statement with the CHANGE keyword.

4. Specify the current name of the dataset, followed by an equal sign and the new name. Both the name of the current dataset and the new dataset mustn’t be enclosed between quotes.

5. Finish the DATASETS procedure with the RUN statement.

In the example below, we show how to use the CHANGE statement to rename the CARS dataset as VEHICLES.

proc datasets library=work;
    change cars=vehicles;
run;

You can check the log to make sure that SAS has changed the name of the dataset.

Rename dataset in SAS

Instead of inspecting the SAS log, you can also use the PROC DATASET procedure to list all the datasets in a given library (in this case, the WORK library).

proc datasets library=work memtype=data;
run;

Do you know? How to use PROC DATASETS to list all the variables of a dataset

Rename a SAS Dataset with a DATA Step

The second method to rename a SAS dataset is with a DATA Step.

In fact, this method first creates a copy of the original dataset with a new name. After that, it removes the original dataset.

These are the steps to change the name of a table in SAS with a DATA Step:

1. Define the name of the new dataset with the DATA statement.

2. Specify the name of the original dataset with the SET statement.

3. Use the RUN statement to create a copy of the original dataset, but with a new name.

4. Optionally, use the PROC DATASETS procedure and the DELETE statement to remove the original dataset.

The example below provides an example of how to rename a dataset with a DATA Step.

data work.vehicles;
    set work.cars;
run;
 
proc datasets lib=work;
    delete cars;
run;

Do you know? 3 Ways to Delete a Dataset in SAS

Rename a SAS Dataset with PROC SQL

The third method to change the name of a table in SAS is with PROC SQL. Like the previous method, PROC SQL creates a new table with the new name. So, in other to save disk space we recommend removing the table with the old name.

This is how to change the name of a table in SAS with PROC SQL:

1. Start the SQL procedure with the PROC SQL keywords.

2. Specify the new table name with the CREATE TABLE clause.

3. Use the SELECT * clause to select all columns.

4. Use the FROM clause to specify the original table.

5. Optionally, use the DROP TABLE statement to remove the original table.

6. Finish the SQL procedure with the QUIT statement.

In the example below, we provide SAS code to change the name of a table with PROC SQL.

proc sql;
    create table work.vehicles as
    select * from work.cars;
 
    drop table work.cars;
quit;

One thought on “3 Simple Ways to Rename a Dataset in SAS

Comments are closed.