Return to Archive

SAS Tip of the Month
March 2009

There are times, when programming in SAS, where you need to "delete all the datasets in the WORK directory, except for these." I had such a situation recently, where some datasets were being generated before a program I was editing --I wanted to delete all except these. Running the following code would delete ALL the datasets in the WORK library:

   proc datasets library=work mt=data kill;
       quit;
   run;     

This was not what is wanted. One solution is to create a create a list of datasets in the WORK directory, removing those datasets that were not to be deleted, then using that list in the DELETE statement inside a PROC DATASETS call. Code like this could look like:

   *** Create a list of members in the WORK library, except the one
       that is needed to keep;
   proc sql noprint;
      select memname
         into :mlist separated by ' '
         from sashelp.vstable
         where libname='WORK' and memname ^in('TREATMENT_MAPPING');
      quit;
   run;
   *** Remove datasets in list;
   proc datasets library=work mt=data;
      delete &mlist
      quit;
   run;     

Note that in the example above, the dataset TREATMENT_MAPPING is not deleted in the WORK library, but all other datasets are.

Another solution is to use the SAVE statement in the DATASETS procedure, as the following example demonstrates:

   proc datasets library=work mt=data;
      save TREATMENT_MAPPING;
      quit;
   run;     

This last solution is more easier to write and maintain.

This month the SAS Global Forum is held at the Gaylord National Resort and Convention Center, at National Harbor in Maryland. If you are lucky enough to go, then please use this event, the premier event on the SAS Calendar, to hear what others are doing at the presentations, network, have some fun, and take back to your own groups of some lessons learned. If you cannot go, keep an eye on the SAS website for the videos of selected segments of the conference, and don't miss the papers presented at conference -- there will be a nugget or two for everyone.

________________________________
Updated Updated March 1, 2009