Friday, April 10, 2009

Are You Sure - SAS Code

Update on May 20, 2009: Users may wish to add "memtype=data" option to the proc datasets statement to drop only SAS tables (see below). I believe the proc datasets procedure will delete formats and possibly other types of data.

Today I wrote a macro program in SAS to delete all of the contents of a specified library. I programmed a pop-up window to ask the user if they really want to delete everything. Here's the code:

%macro DeleteLib(lib);

%let rusure=N;

/* Pop up a window to ask a question */
%window surewin
#1 @20 "DELETING ALL TABLES!"
#3 @20 "ALL TABLES WILL BE DELETED FROM &lib"
// @20 "Are you sure you would like to delete all tables from &lib?"
@81 rusure 1 attr=rev_video required=yes " (Y/N)"
// @20 "HIT THE ENTER KEY TO CONTINUE"
%display surewin delete;

%put rusure=&rusure;

/* Delete all tables if Yes */
%if &rusure=Y %then %do;

data dummy;
run;

proc datasets library=&lib memtype=data;
save dummy;
quit;

proc sql;
drop table &lib..dummy;
quit;

%put NOTE: All tables have been deleted from &lib.;

%end;

/* Put a comment if No */
%if &rusure=N %then %do;

%put NOTE: No tables have been deleted.;

%end;

%mend DeleteLib;


The first and last line are the beginning and end of the macro program. The second line sets the variable "rusure" (that is, "Are you sure?") to "N" for "No" by default. The next block creates the window and pops it up. In the window the user can specify "Y" or "N" to specify whether to delete the library (think of it as a folder or directory with SAS datasets in it). (The code uses a macro variable "&lib" to insert the name of the library.) If "Y", then the code creates a dummy dataset with nothing in it, runs a program that keeps only the dummy dataset and delete everything else, then it drops the dummy dataset and puts a note in the log. If "N", then it just puts a note in the log.

At first I thought of putting another set of code in there as a double-check: An additional window asking "Are you REALLY sure you want to delete the library?" then running another Y/N if statement. But then I thought that might be a little annoying to have to deal with so many windows.

It would have been a funny April Fool's joke to insert a series of windows into someone's program asking, "Do you want to run this program?", "Are you sure you want to run this program?", "Are you REALLY sure?", "REALLY, REALLY sure?", "Wouldn't you rather run another program?". The user would have to keep hitting Y or N over and over again to get it to work. It would be easy to insert a small line of code referencing another piece of code using a %include statement (for example, %include "c:\AprilFools\rusure.sas";). A trickster could even hide the code after a line of spaces far to the right of the window where no one would find it.

If anyone tries that, let me know what happens!

1 comment:

  1. I just wanted you to know that I found value in your post about deleting all the SAS datasets in a library. It had occurred to me to use the SAVE feature of proc datasets in that way, but I wasn't sure about the dummy thing. Seeing that you had done it encouraged me to try it, and it works perfectly. Just what the doctor ordered.

    ReplyDelete

Note: Only a member of this blog may post a comment.