From: Anaconda on 19 Mar 2010 14:38 Hi, I have made a macro to delete empty files from a folder, and wonder how it can be justified a bit to delete only those files that were created yesterday or earlier. It this task cannot be accomplished in this way, is there another way to do it? Here is the macro: %macro delete_empty_files_in_folder(folder); filename filelist "&folder"; data _null_; dir_id = dopen('filelist'); total_members = dnum(dir_id); do i = 1 to total_members; /* walk through all the files in the folder */ member_name = dread(dir_id,i); file_id = mopen(dir_id,member_name,'i',0); if file_id > 0 then do; /* if the file is readable */ freadrc = fread(file_id); if freadrc = -1 then do; /* if the file is empty */ rc = fclose(file_id); rc = filename('delete',member_name,,,'filelist'); rc = fdelete('delete'); end; end; rc = fclose(file_id); end; rc = dclose(dir_id); run; %mend; - Anaconda
From: Patrick on 19 Mar 2010 21:03 I usually use shell escapes (x-command, systask..) for such tasks. On which OS should this code run (Windows, Unix, ..)?
From: Andrew Karp Sierra Info Services on 20 Mar 2010 16:55 Here is an approach that I think will work regardless of operating system. It may not be the most elegant method, but as the old saying goes, if the tree fell, the axe was sharp enough. I've put a bunch of comments in the code to explain what each PROC and DATA step is (hopefully) doing. * library test has datasets that were created on 3/18, 3/19 and 3/20; * create a data set that has the creation date (crdate) of every data set in library test. Noprint option instructs CONTENTS procedure to not put any output in the output window. Only required vars are placed in output data set; proc contents data=test._all_ noprint out=info(keep=libname memname crdate); run; * create a new data set that has one row/obs per data set in the test library; proc sort nodupkey data=info out=info2; by libname memname crdate; run; * the variable crdate (creation date) in data set info2 is the SAS datetime variable (num of seconds from midnight 1/1/1960) with the date and time the data set was created; * use the datepart function to find the SAS date "part" of crdate and compare it to today's date returned by the today() function. If the date the data set was created is less than (that is, before) today's date, then output the data set name to a data set that holds a list of all "old" data sets we want to be delete; data to_be_deleted; set info2; if datepart(crdate) < today(); run; /* optional step: look at list of data sets to be deleted */ * proc print data= to_be_deleted; * title 'to be deleted'; * run; * put the names of the data sets to be deleted in to a macro symbol table variable called to_delete using the PROC SQL step below; * MEMNAME is the data set name ("member of the SAS library"); proc sql noprint; select trim(memname) into :to_delete separated by ' ' from to_be_deleted; quit; * execute proc datasets to delete all data sets in the test library that were created before today. Notice reference to macro symbol table variable in the DELETE statement. List of deleted data sets will appear in the SASLOG; proc datasets library = test; delete &to_delete; run; quit; Hope this helps.... Andrew Karp Sierra Information Services http://www.sierrainformation.com On Mar 19, 6:03�pm, Patrick <patrick.mat...(a)gmx.ch> wrote: > I usually use shell escapes (x-command, systask..) for such tasks. > > On which OS should this code run (Windows, Unix, ..)?
|
Pages: 1 Prev: generate SAMPLE correlations Next: aggregate remaining obs /records SQL |