Check that a SAS data set exists (and other data maintenance tips)

3

In Base SAS you can use the DATASETS procedure to determine the SAS data sets in a library, and you can use the DELETE statement to delete data sets. Did you know that you can do the same operations from within the SAS/IML language?

The following DATA step creates four SAS data sets, each with one observation:

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

In Base SAS, you can use the following statements to list the data sets in the WORK library:

proc datasets library=work;
run;

The DATASETS procedure is an interactive procedure (like PROC IML), so the procedure is still active after a RUN statement. You can submit the following statements to delete the WORK.A data set:

delete a;
run;

You can perform similar operations from within the SAS/IML language. For example, the following statements use the EXIST function in Base SAS to check whether WORK.B exists. If so, the DELETE subroutine deletes the data set.

proc iml;
if exist("b") then
   call delete("work", "b");

You can also use the DATASETS function in SAS/IML to determine which data sets exist in a library:

ds = datasets("work");
print ds;

A new feature in SAS/IML 9.22 is that you can call the DELETE subroutine to delete all data sets specified in a vector:

call delete(ds);

If you are using a version of SAS/IML prior to 9.22, you have to call the DELETE subroutine in a loop to delete multiple data sets.

Share

About Author

Rick Wicklin

Distinguished Researcher in Computational Statistics

Rick Wicklin, PhD, is a distinguished researcher in computational statistics at SAS and is a principal developer of SAS/IML software. His areas of expertise include computational statistics, simulation, statistical graphics, and modern methods in statistical data analysis. Rick is author of the books Statistical Programming with SAS/IML Software and Simulating Data with SAS.

3 Comments

  1. Pingback: Cleaning up after yourself: Deleting data sets - The DO Loop

Leave A Reply

Back to Top