DATA Step, Macro, Functions and more

need to delete txt files older than 30 days

Reply
Contributor
Posts: 60

need to delete txt files older than 30 days

Hi All,

 

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

 

ex.

 

ab_dfg20170503.txt,

 

Super User
Posts: 17,840

Re: need to delete txt files older than 30 days

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.

 

Contributor
Posts: 60

Re: need to delete txt files older than 30 days

Yup, this is new requirements

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Super User
Posts: 17,840

Re: need to delete txt files older than 30 days

[ Edited ]

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. 

Super User
Posts: 17,840

Re: need to delete txt files older than 30 days

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;
Super User
Posts: 6,946

Re: need to delete txt files older than 30 days

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 17,840

Re: need to delete txt files older than 30 days

[ Edited ]

Ouch! 

 

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

Contributor
Posts: 60

Re: need to delete txt files older than 30 days

Hi ,

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

 

Ask a Question
Discussion stats
  • 7 replies
  • 166 views
  • 2 likes
  • 3 in conversation