PROC DATASETS in SAS: Learn with Examples

Deepanshu Bhalla Add Comment

This tutorial shows how to use PROC DATASETS in SAS, along with examples.

The PROC DATASETS statement in SAS is used to manipulate SAS datasets. It can be used to perform a variety of operations on datasets, such as copying, renaming, deleting, and modifying datasets.

Syntax of PROC DATASETS

The syntax of PROC DATASETS is as follows:

proc datasets library=mylibrary;
run;
quit;

If you don't use the library= option, SAS will consider the WORK library as the default library for managing datasets in the PROC DATASETS.

Sample Datasets

Let's create two datasets in the WORK library from the SASHELP library. These datasets will be used in further examples.

data cars;
set sashelp.cars;
run;

data class;
set sashelp.class;
run;

Display a list of all the datasets in a library

To view a list of all the datasets in a specific library, you can use PROC DATASETS. Here we are looking in the WORK library.

proc datasets library=work;
run;
quit;
PROC DATASETS

In the image above, the first two highlighted ones are datasets. Others are item store and catalogs. Item store is a binary system file created by the SAS. Catalogs are SAS files that can contain macros, formats, and other SAS objects.

To display only datasets in the results window, you can use memtype=data.

proc datasets library=work memtype=data;
run;
quit;

How to copy all the datasets in a library

Suppose you have a library named mylib and you want to copy all the datasets from the WORK library to "mylib" library.

Create library if you already don't have. Specify location in the library as per your location.

libname mylib '/home/deepanshu88us0/';
proc datasets;
copy in=work out=mylib;
run;
quit;

How to copy all the datasets from a library

Suppose you have a library named mylib and you want to copy all the datasets from the WORK library to "mylib" library.

Create library if you already don't have. Specify location in the library as per your location.

libname mylib '/home/deepanshu88us0/';
proc datasets;
copy in=work out=mylib;
run;
quit;

How to copy some datasets from a library

Suppose you want to copy only specific datasets from the WORK library to "mylib" library. You can mention them in the SELECT statement.

proc datasets;
copy in=work out=mylib;
select cars class;
run;
quit;

How to move datasets from a library

We can use the MOVE option in the COPY statement to move a dataset from one library to another. HERE "CARS" dataset has been moved to the "mylib" library from the "work" library.

proc datasets;
copy in=work out=mylib move;
select cars;
run;
quit;

To check if the moved dataset is available in the WORK library, we can run the command below. We can see that CARS dataset is no more available in the WORK library. "MOVE" option is like cut-paste from a old libarry to a new library.

proc datasets library=work memtype=data;
run;
quit;
PROC DATASETS: Move Dataset

How to delete the whole library in SAS

We can use the KILL option in PROC DATASETS to delete the whole SAS library. Here we are removing the library named "mylib".

proc datasets lib=mylib kill;
run;
quit;

How to delete datasets from a library

We can use the DELETE statement in the PROC DATASETS to delete specific datasets from a SAS library. Here we are deleting "CLASS" dataset from the "WORK" library.

proc datasets lib=work memtype=data;
delete class;
run;
quit;

How to rename SAS dataset

We can use the CHANGE old-dataset-name = new-dataset-name statement in the PROC DATASETS procedure to rename a SAS dataset. In the code below, we are renaming "cars" dataset to "automobiles".

data cars;
set sashelp.cars;
run;

proc datasets lib=work nolist;
change cars = automobiles;
quit;
run;

How to combine SAS dataset

In the code below, we are combining CARS dataset to itself (doubling it). You can follow the same approach if you have two datasets with the same variables and you need to combine them.

  • Appended the file WORK.CARS to itself.
  • There were 428 observations read from the data set WORK.CARS.
  • Added 428 observations.
  • The data set WORK.CARS now has 856 observations.
data cars;
set sashelp.cars;
run;

proc datasets lib=work;
append out=cars data=cars;
run;
quit;
PROC DATASETS: Append
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "PROC DATASETS in SAS: Learn with Examples"
Next → ← Prev