Nov 6, 2009

SAS macro to Create / Remove a PC Directory...

Here’s a SAS macro to Create and Remove a PC Directory… Often we ignore Notes and warning in the SAS log when we try to create/remove a directory that does/doesn’t exist…This macro first checks for the existence of the directory and then create/delete it or else put a message to the SAS log…try it out :-)
/* Macro to Create a directory */
%macro CheckandCreateDir(dir);
   options noxwait;
   %local rc fileref ;
   %let rc = %sysfunc(filename(fileref,&dir)) ;   
   %if %sysfunc(fexist(&fileref)) %then
      %put The directory "&dir" already exists ;
   %else
     %do ;
         %sysexec mkdir "&dir" ;
         %if &sysrc eq 0 %then %put The directory &dir has been created. ;
          %else %put There was a problem while creating the directory &dir;      
   %end ;
%mend CheckandCreateDir ;
 
 
 
/* Macro to Remove a PC directory */
 
%macro RemoveDir(dir);
  options noxwait; /* Option noxwait specifies that the command processor automatically returns to the SAS session
after the specified command is executed. You do not have to type EXIT... */
 
  %local rc fileref ;
  %let rc = %sysfunc(filename(fileref,&dir)) ; /* This sysfunc and the filename statements check for the existence of the directory */ 
  %if %sysfunc(fexist(&fileref)) %then
   %do;
      %put Removing directory &dir....;      
      %sysexec rmdir /Q /S "&dir" ; /* Options /Q for quiet mode with no prompting and /S for removing sub directories */
      %if &sysrc eq 0 %then
          %put The directory &dir and its sub-directories have been Deleted. ;
      %else
          %put There was a problem while Removing the directory &dir;
   %end;
   %else %put The directory &dir does NOT  EXIST;        
%mend RemoveDir ;
 
/*
%sysexec - executes the command
&sysrc   - stores the return code of the above command sysexec
*/

No comments:

Post a Comment

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