create_delete_folder
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 :-)
View sourceprint?
/* 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
*/

 (courtesy: sasblogs)

ss logo-2small

Comments

  1. Thanks for posting these blog related to SAS.It will be really useful for my preparation.Keep posting

    such essential blogs.
    SAS course

    ReplyDelete

Post a Comment

Popular posts from this blog