BookmarkSubscribeRSS Feed
SAS_INFO
Quartz | Level 8

Hi All,

 

I need to delte .txt files in a folder which are 30 days older(by create date)

 

ex.

 

ab_dfg20170503.txt,

 

7 REPLIES 7
Reeza
Super User

Didn't you just ask this when it was older than 24 months?

 

The solution is similar with minor tweaks for days not months. INTNX() is still the primary tool to be used here.

 

SAS_INFO
Quartz | Level 8
Yup, this is new requirements

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Reeza
Super User

The process is identical. Identify the differences, see where those features are definined the program and change accordingly. 

 

If you need to, do one at a time - first change to do text files. Then change it to handle different date format. 

You can make up fake files to work with for testing.

 

EDIT: I was wrong, Chris solution will not work. However, you should read the reference paper suggested in that thread here:

http://support.sas.com/resources/papers/proceedings17/0835-2017.pdf

 

Page 11 onwards is related to what you're doing, though a more complicated example. 

 

There is also a macro in the appendix that will list all files. From there you need to use the FDELETE function. 

Reeza
Super User

Can't test it but would be something like this. If it doesn't work, explain why including code/log. 

 

%macro list_files(dir,ext);
  %local filrf rc did memcnt name i;
  %let rc=%sysfunc(filename(filrf,&dir));
  %let did=%sysfunc(dopen(&filrf));      

   %if &did eq 0 %then %do; 
    %put Directory &dir cannot be open or does not exist;
    %return;
  %end;

   %do i = 1 %to %sysfunc(dnum(&did));   

   %let name=%qsysfunc(dread(&did,&i));

      %if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do;
        %put &dir\&name;

        data _tmp;
          length dir $512 name $100;
          dir=symget("dir");
          name=symget("name");
        run;
        proc append base=file_list data=_tmp;
        run;quit;

      %end;
      %else %if %qscan(&name,2,.) = %then %do;        
        %list_files(&dir\&name,&ext)
      %end;

   %end;
   %let rc=%sysfunc(dclose(&did));
   %let rc=%sysfunc(filename(filrf));     

%mend list_files;

%list_files('\folders\myfolders\test\',txt);

data check;
set file_list;


/*Use INPUT(), REVERSE(), SUBSTR() to get the date.
 Reverse the file name and extract the characters needed.
Use the substr() to extract the date, reverse it back to original format, and use INPUT() to convert to a SAS date. Once it's a date basic subtraction from the current date and if you need to delete it use DELETE()*/

file_date = input(reverse(substr(reverse(name), 5, 8)), yymmdd8.);

if today() - file_date >= 30 then fdelete(catx("/", dir, name));

run;
Kurt_Bremser
Super User

Maxims 14 (Use the right tool) and 15 (Know your playing field) come into play:

 

find . -name \*.txt -mtime +30 -exec rm {} \;

 

Now compare that with all the hoops you have to jump through when doing that in SAS.

Reeza
Super User

Ouch! 

 

Fortunately I only wrote 4 or 5 lines of code for above - the rest I copied and pasted. 

SAS_INFO
Quartz | Level 8

Hi ,

the text files naming convention doesnt matter and i need to delete based on the creation date.

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2854 views
  • 2 likes
  • 3 in conversation