How to delete datasets in SAS?
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.
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.
libname sasdsn '/home/subhroster20070/sasdsn'; data sasdsn.data1; set sashelp.class; run; data sasdsn.data2; set sashelp.classfit; run;
To delete all datasets from the SASDSN library, type the code snippet below.
proc datasets library=sasdsn kill;
CAUTION! The KILL option deletes all library members 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.
Once, you run the code, you can check the log to ensure that datasets have been deleted.
When we examine the SASDSN library after running the code above, we see that all data sets from the SASDSN library have been deleted.
To eliminate the results window, you can use the nolist option as below.
proc datasets library=sasdsn kill nolist
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; quit;
Examining the Log after running this code reveals that the data2 data set is deleted.
Keep 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; quit;
Once again, if you see the log it confirms that only data set data2 remains, and Data set data1 and data3 are deleted.
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 have 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.