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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 7 replies
  • 2462 views
  • 2 likes
  • 3 in conversation