Automatically deleting old SAS temporary files

4.1k views Asked by At

Good day,

Mundane SAS jobs are triggered periodically on our machine. Every now and then the jobs fail and leave temporary files. (Stored in C:\Users\< user_Name>\AppData\Local\Temp\SAS Temporary Files\ in windows environment.)

Eventually these clog up the machine. Currently we have to delete them by hand. I'm wondering if SAS could delete old (lets say week plus) old files periodically? Scheduling independent cleanup job is what I'm looking for.

I could use other means to do this, like python scripts, but single language environment will bring about managements blessing and benevolence.

Edit: pathname was not showing properly

2

There are 2 answers

0
Chris J On BEST ANSWER

Here's a macro which invokes the DOS forfiles command, and deletes each matching file. The pushd allows this to work with UNC paths also.

%MACRO DELFILES(PATH,MASK,AGE) ;
  %IF %SYSFUNC(fileexist(&PATH)) %THEN %DO ;
    data _null_ ;
      infile "pushd ""&PATH"" && forfiles /P ""."" /S /M &MASK /D -&AGE /C ""cmd /c del @path"" 2>nul" pipe truncover ;
      input ;
      put _INFILE_ ;
    run ;
  %END ;
%MEND ;

So, using your example :

/* Delete everything over 7 days old */
%DELFILES(C:\Users\&SYSUSERID\AppData\Local\Temp\SAS Temporary Files\,*,7) ;

You could put this in an autoexec, or at the start or end of your programs.

0
user667489 On

This answer is a pretty good starting point if you want a cross-platform approach based on SAS functions only, without using OS-specific commands:

https://stackoverflow.com/a/1412947/667489

You would just need to add an extra line or two to iterate through all the work directories and delete all the files.