A Simple Proc Datasets Example in SAS

Proc Datasets Example in SASProc Dataset is a multi-purpose data manipulation procedure in SAS. Whenever you want to modify anything but the data portion of a SAS data set, Proc Datasets should be the first procedure that comes to mind. There is a reason that Michael A. Raithel calls it the The Swiss Army Knife of SAS Procedures.

In this example page, I will provide a few simple examples of the most useful features of Proc Datasets. I will keep the explanations to a minimum. Rather, I will let the code snippets be simple templates that you can easily apply to other situations.

Copy SAS Data Set Between libraries

First, let us see how to Copy and Move Data Sets Between Libraries. In the snippet below, I use the Copy Statement and specify the input and output libraries. Also, I use the Memtype Option to tell the procedure that Class is a regular SAS data set. Finally, I use the Select Statement and specify Class. This creates a copy of sashelp.class in the work library.

proc datasets nolist;
   copy in=sashelp out=work memtype=data;
   select class;
run;quit;

Print Content

We can use the procedure to create the same output as Proc Contents. The syntax is below.

proc datasets lib=work;
   contents data=class;
run;quit;

Rename Data With Proc Datasets

One of the big advantages of Proc Datasets is that we do not need to read the entire data set with a data step to alter attributes. Suppose we want to rename a SAS data set. The code below renames the work.class data set classdata. Without reading a single observation.

proc datasets lib=work;
   change class=classdata;
run;

Use Proc Datasets to Modify Formats and Labels

The Modify Statement allows us to modify variable attributes such as formats and labels. In the snippet below, I apply a format to height and a label to name.

proc datasets lib=work nolist;
   modify classdata;
      format height 8.2;
      label name="Name";
run;

Create and Manage SAS index With Proc Datasets

One of my favorite features of Proc Datasets is that we can create SAS indexes on data sets easily. Furthermore, we can easily manage and update the index.In the snippet below, I create a simple index on the variable name. I write about SAS indexes and centiles in the posts below.

Using An Index to Increase Performance in SAS.
The Importance Of SAS Index Centiles.

proc datasets lib=work nolist;
   modify classdata;
      index delete _all_;
      index create name / nomiss;
run;quit;

If you are interested in SAS indexes, there is no better reference than The Complete Guide to SAS Indexes by Michael A. Raithel.

Delete Data Set with Proc Datasets

Finally, we can use Proc Datasets to delete SAS data sets. In the code below, we delete the work.classdata. In the blog post Delete All Data Sets in Work, I demonstrate how to delete all datasets in a library.

proc datasets library=work nolist;
   delete classdata;
quit;

Summary

In this example page, I demonstrate a few useful applications of one of my favorite SAS procedures. Proc Datasets. We see that Proc Datasets solves a variety of data manipulation tasks, that would otherwise require a different procedure or reading the entire data set with a data step. Consequently, Proc Datasets is usually the fastest way to solve a problem, since it does not read any of the data portion. Above, I only sketch a few common and simple uses of Proc Datasets. I encourage you to read the documentation and the article at the top to familiarize yourself with the procedure.

As related posts, read A Simple Proc Tabulate Example and A Simple Proc Summary Example.

You can download the entire code from this example here.