BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sastef41
Calcite | Level 5

I have the below code that deletes all files within a SAS folder.  I would like to modify it, or come up with a different solution that would only delete the files in the said folder if older than 5 days.  Any help would be appreciated!

 

%macro delete_all_the_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);
            rc = fclose(file_id);
            rc = filename('delete',member_name,,,'filelist');
            rc = fdelete('delete');
         end;
         rc = fclose(file_id);
      end;
      rc = dclose(dir_id);
   run;
%mend;
%delete_all_the_files_in_folder(/sasem/service/Face_IT/Export/)
1 ACCEPTED SOLUTION

Accepted Solutions
LeonidBatkhan
Lapis Lazuli | Level 10

Hi sastef41,

Your exact same problem is solved in my recent blog post SAS administrators tip: Automatically deleting old SAS logs .

There, I used the following code:

%macro mr_clean(dirpath=,dayskeep=30,ext=.log);
   data _null_;
      length memname $256;
      deldate = today() - &dayskeep;
      rc = filename('indir',"&dirpath");
      did = dopen('indir');
      if did then
      do i=1 to dnum(did);
         memname = dread(did,i);
         if reverse(trim(memname)) ^=: reverse("&ext") then continue;
         rc = filename('inmem',"&dirpath/"!!memname);
         fid = fopen('inmem');
         if fid then 
         do;
            moddate = input(finfo(fid,'Last Modified'),date9.); /* see WARNING below */
            rc = fclose(fid);
            if . < moddate <= deldate then rc = fdelete('inmem');
         end;
      end; 
      rc = dclose(did);
      rc = filename('inmem');
      rc = filename('indir');
   run;
%mend mr_clean;

You can easily modify it to adapt to your scenario.

The key here is the following statement to capture file date stamp:

moddate = input(finfo(fid,'Last Modified'),date9.);

and IF statement to delete files of specified date interval:

if . < moddate <= deldate then rc = fdelete('inmem');

Hope, this help.

 

 

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User
%macro delete_all_the_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 finfo(file_id,"info_item") < value then do;
            /* info_item is operating system- and locale-dependent */
            /* determine it by using foptname() once */
            /* value must match the type, usually datetime */
              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; 

As stated in the comment, you need to run a loop from 1 to foptnum once and use foptname(file_id,num) to find the correct name for your "file information item".

 

 

Kurt_Bremser
Super User

But since this is highly dependent on the operating environment anyway, I'd much rather use OS tools to get this done, e.g. by using ls with proper options to retrieve the last modification timestamps for all files; Linux (GNU) ls provides options for outputting the modification timestamp in GMT in ISO 8601 notation.

LeonidBatkhan
Lapis Lazuli | Level 10

Hi sastef41,

Your exact same problem is solved in my recent blog post SAS administrators tip: Automatically deleting old SAS logs .

There, I used the following code:

%macro mr_clean(dirpath=,dayskeep=30,ext=.log);
   data _null_;
      length memname $256;
      deldate = today() - &dayskeep;
      rc = filename('indir',"&dirpath");
      did = dopen('indir');
      if did then
      do i=1 to dnum(did);
         memname = dread(did,i);
         if reverse(trim(memname)) ^=: reverse("&ext") then continue;
         rc = filename('inmem',"&dirpath/"!!memname);
         fid = fopen('inmem');
         if fid then 
         do;
            moddate = input(finfo(fid,'Last Modified'),date9.); /* see WARNING below */
            rc = fclose(fid);
            if . < moddate <= deldate then rc = fdelete('inmem');
         end;
      end; 
      rc = dclose(did);
      rc = filename('inmem');
      rc = filename('indir');
   run;
%mend mr_clean;

You can easily modify it to adapt to your scenario.

The key here is the following statement to capture file date stamp:

moddate = input(finfo(fid,'Last Modified'),date9.);

and IF statement to delete files of specified date interval:

if . < moddate <= deldate then rc = fdelete('inmem');

Hope, this help.

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 2571 views
  • 3 likes
  • 3 in conversation