SAS data sets are a frequent by-product of SAS programs. While we often keep these data sets as assets for use in reports and other purposes, eventually you will have data sets that no longer "spark joy" and you'll decide that it's time to delete them. Deleting data sets that you no longer need can save space and memory during processing, and it's best practice to clean up. In this article I'll discuss several ways to delete SAS data sets. (I'm not going to presume that these are all the methods you can use, but these are the most common in my experience.)
Like most tasks in SAS, "there's a PROC for that." PROC DELETE has one job and it does it efficiently.
To delete a single data set named WORK.DATA1:
proc delete data=work.data1; run;
To delete multiple named data sets, simply add them in the DATA= option:
proc delete data=work.a1 work.a2 ; run;
To delete a range of data sets named in WORK that are named in sequence, for example DATA1 through DATA5 (and here I'm using the LIBRARY= option so I can omit the libname on the DATA=):
proc delete library=work data=DATA1-DATA5; run;
Alas, it does not support "wildcard" colon-list matching for data set names, but read on for how you can accomplish this in PROC DATASETS. PROC DELETE has several options for different member types and generation data sets -- see the documentation.
Note: for many years PROC DELETE existed only as a mythical undocumented procedure. Beginning with SAS 9.4, it was brought into real life by the sheer will of SAS programmers and an enterprising SAS R&D developer. It's official now.
PROC DATASETS is a versatile utility procedure for "all things data sets" -- you can use it to copy, move, modify attributes, and of course...DELETE.
To delete three data sets named A1, A2, and A3 from the WORK library:
proc datasets nolist lib=work; delete a1 a2 a3 ; quit;
Or you can use the colon list (or range list) syntax:
proc datasets nolist lib=work; delete a: ; /* all data sets that begin with "A" */ delete a1-a3; /* or data sets in this range */ quit;
Note that I used the NOLIST option. That prevents the DATASETS procedure from generating a verbose output showing what's now in your SAS library. By default, PROC DATASETS is quite chatty -- but you can limit the output using NOLIST and NODETAILS.
If you are absolutely certain that you can discard all data sets within a given SAS library, you can use the KILL option.
/* delete all data files from WORK */ proc datasets nolist lib=work kill; quit;
Because the WORK library often contains data related to work-in-progress, sometimes I will create a library in a temp location that's specifically for transient data, which I can then "kill" when that part of the program completes.
/* create a subfolder in WORK for my data */ options dlcreatedir; libname scratch "%sysfunc(getoption(WORK))/scratch"; /* just adding some data for example purpose */ data scratch.a1 scratch.a2 scratch.a3; set sashelp.class; run; /* more operations with scratch data */ /* now clear it when ready */ proc datasets lib=scratch kill nolist; quit;
What if you want to delete most of the data from a library, but you want to spare just a few lucky files? You can use the SAVE statement.
Here's my previous KILL example, modified to use SAVE instead and save just one data set:
/* just adding some data for example purpose */ data scratch.a1 scratch.a2 scratch.a3; set sashelp.class; run; /* now delete all but A1 */ proc datasets lib=scratch nolist; save a1; quit;
I know...the syntax might seem odd because it doesn't reflect your intent to delete everything else...but that's how it works. Here's the SAS log from those statements:
NOTE: Saving SCRATCH.A1 (memtype=DATA).
NOTE: Deleting SCRATCH.A2 (memtype=DATA).
NOTE: Deleting SCRATCH.A3 (memtype=DATA).
There is more to PROC DATASETS for managing the lifecycle of SAS data sets. Check out the excellent paper by @MMMIIIIKKKKEEEE: PROC DATASETS: The Swiss Army Knife of SAS Procedures.
Finally, here's something for the SQL coders. If you're in the throes of a PROC SQL sequence and you want to clean up some data, you can use the DROP TABLE statement:
proc sql; drop table work.a1; drop table work.a2; drop table work.a3; quit;
This might make even more sense if your data is in a database that you're accessing through a SAS/ACCESS engine -- PROC SQL should send the appropriate command to the database to remove the data. There's also a DROP VIEW and DROP INDEX statement you can use for those related data types.
SAS includes a clever autocall macro that you can use to automate the PROC SQL DROP TABLE approach. %DELTABLE allows you to list multiple tables, and it's smart enough to detect whether the data file exists and whether it's a table or view...and then generate the correct statements for you.
%deltable (tables=scratch.a1 scratch.a2 scratch.a3)
How to delete temporary tables in CAS (SAS Viya)
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.