How to delete datasets in SAS?3 min read

It's an everyday SAS activity to manually delete all or some of the data sets that your SAS Program generates. It is an efficient programming practice to delete a dataset if it is not used anymore.

There's no need to add up unnecessary disk space. Additionally, it will make your libraries simpler to navigate and gives you more control.

How to delete all datasets in a library?

Deleting all data sets in a library is a very simple task. To delete all datasets in a library, specify the Work library in the PROC DATASET procedure statement, and specify the KILL option. 

The KILL option deletes all data sets in the library. Also, I have used the NOLIST option to suppress output in the HTML viewer.

First, let us create three arbitrary datasets, data1,data2 and data3, in the SASDSN library.

data sasdsn.data1;
set sashelp.class;
data sasdsn.data2;
set sashelp.classfit;
data sasdsn.data3;
run;data sasdsn.class;
set sashelp.class;

Now, to delete all datasets from the SASDSN library, type the below code snippet.

proc datasets library=sasdsn kill nolist;

CAUTION! The KILL option deletes all members of the library immediately after the statement is submitted.

You are not asked to verify the Delete operation, so ensure that you intend to delete the files before submitting the program.

SAS LOG Deleting All Members of the SASDSN Library

NOTE: Deleting SASDSN.DATA1 (memtype=DATA).
NOTE: Deleting SASDSN.DATA2 (memtype=DATA).
NOTE: Deleting SASDSN.DATA3 (memtype=DATA).

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

Note: All data sets and catalogs are deleted from the SAS library, but the libref is still assigned for the session. The name that is assigned to the library in your operating environment is not removed when you delete the files that are included in the library.

Delete specific SAS data sets in a library

To delete specific datasets in a library do not use the KILL Option. Rather, use the DELETE Statement and specify the exact data set to delete it.

proc datasets library=sasdsn nolist;
delete data2;

Examining the Log after running this code reveals that the data2 data set is deleted. 

SAS LOG Deleting DATA2 dataset from SASDSN Library

NOTE: Deleting SASDSN.DATA2 (memtype=DATA).

Delete Specific SAS data sets in a library.

Finally, let see how to delete all data sets in a library, but keep specific data sets. To do this, 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=sasdsn nolist;
save data2;

Once again, if you see the log it confirms that only data set data2 remains. and Data set data1 and data3 is deleted.

SAS LOG Deleting DATA2 dataset from SASDSN Library

NOTE: Saving SASDSN.DATA2 (memtype=DATA).
NOTE: Deleting SASDSN.DATA1 (memtype=DATA).
NOTE: Deleting SASDSN.DATA3 (memtype=DATA).

Proc Delete can also be used to delete SAS datasets. The PROC DELETE procedure is another SAS procedure that isn't documented. That is, that was the case before the release of SAS 9.4,

Unlike PROC DATASETS, PROC DELETE does not validate if the selected data set exists before trying to remove the file.

Since SAS 9.4, PROC DELETE has had more capabilities than ever -- find out more in Chris Hemedinger's blog.

If you liked this article, you might also want to read Ten Quick Uses of Proc Datasets as well.

Do you any tips to add Let us know in the comments.

Please subscribe to our mailing list for weekly updates. You can also find us on Instagram and Facebook.

by Subhro Kar
Been in the realm with the professionals of the IT industry. I am passionate about Coding, Blogging, Web Designing and deliver creative and useful content for a wide array of audience.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap