Delete All SAS Data Sets in Work Library

It is a common task in SAS to delete all or some of the data sets that your programs produce. If you do not need a data set anymore, get rid of it. There is no need to take up unnecessary disc space. Also, it makes your libraries easier to navigate and gives you more control.  In this example, I demonstrate three common scenarios in which we delete all datasets in a library, specific datasets in a library and finally how to save specific datasets, but delete all other datasets in a library. PROC DATASETS is the ideal tool for this task.

First, let us create three arbitrary datasets a, b and c in the work library to work with. These datasets are for demonstration purposes only, so it does not matter what we put in them.

data a; a=1; run;
data b; b=1; run;
data c; c=1; run;

Delete all data sets in a library

Deleting all data sets in a library is a very simple task. Simply specify the Work library in the procedure statement and specify the KILL option. The KILL option deletes all data sets in the library. Also, we use the NOLIST option to suppress output in the HTML viewer. In this example, we do not need any other statements, since we do not direct our attention to any specific data sets. We simply delete them all. However, in the following examples, things get a little more complicated.

proc datasets library=work kill nolist;
quit;

When we examine the WORK library after running the code above, we see that all data sets have been deleted.

Delete specific SAS data sets in a library

Next, let us delete a specific data set from the library. To do this, we do not use the KILL Option. Rather, we use the DELETE Statement and specify the exact data set to be deleted.

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

Examining the Work library after running this code reveals that the b data set has been deleted. the data sets a and c are still there.

Save specific SAS data sets in a library

Finally, let us see how to delete all data sets in a library, but keep specific data sets from being deleted. To do this, simply specify the data set you do not want to delete in a SAVE Statement. Again, we do not need the KILL Option in the procedure statement.

proc datasets library=work nolist;
   save b;
quit;

Once again, we can examine the Work library and confirm that only data set b remains. Data set a and c have successfully been deleted.

Summary

In this example, I have demonstrated how PROC DATASETS easily handles deleting and saving data sets. If you are not familiar with the Datasets Procedure, I strongly encourage you to introduce yourself to it. A great introduction in given in the paper PROC DATASETS; The Swiss Army Knife of SAS Procedures by Michael A. Raithel.

Check out the Data Science Example Page for more data related code examples. Also, see the examples Finding the Number of Observations in a SAS Data Set and Drop or Keep Variables with Same Suffix.

You can download the entire code from this page here.