I need to delete 5 days older log files from a folder.I need to do from SAS EG and need a SAS macro.Unix command not required only SAS coding.
You can easily get a directory listing in either of two ways:
1) Use e.g. the code as in this 2012 SGF paper. The trick is to assign a filename to a directory:
filename mydir "/my/logdir";
This will allow you to get filenames and filter them. Caveat here is that on Linux the FINFO() does not give you a creation date. So it is required that the filename contains a date- and timestamp (which is a good habit for log files anyway). If so, parse the filename and use FDELETE() if it matches the 5-day criterium.
2) Use the Linux command ls in a pipe:
filename dirlist pipe "ls -l yourlogdir" ;
Many variations can be chosen even up to use of find with the mtime / ctime options to directly only return the files older than 5 days. And heck, find can even delete them so SAS will not be involved at all. But this seems to be against your requirement of not using Linux commands. Also it could be that your workspace server run with -noxcmd, defeating this option entirely.
So easiest way would be to challenge the "no Linux command" restriction. Otherwise let's hope a date and time are in the filename.
Hope this helps,
- Jan.
I don't know of a way to extract file information without using Unix functions 😞
FDELETE will delete files once you identify them though.
This really is a job for a clean up script though, similar to cleanwork utility from SAS. Probably worth looking at to see if it can help you?
You can easily get a directory listing in either of two ways:
1) Use e.g. the code as in this 2012 SGF paper. The trick is to assign a filename to a directory:
filename mydir "/my/logdir";
This will allow you to get filenames and filter them. Caveat here is that on Linux the FINFO() does not give you a creation date. So it is required that the filename contains a date- and timestamp (which is a good habit for log files anyway). If so, parse the filename and use FDELETE() if it matches the 5-day criterium.
2) Use the Linux command ls in a pipe:
filename dirlist pipe "ls -l yourlogdir" ;
Many variations can be chosen even up to use of find with the mtime / ctime options to directly only return the files older than 5 days. And heck, find can even delete them so SAS will not be involved at all. But this seems to be against your requirement of not using Linux commands. Also it could be that your workspace server run with -noxcmd, defeating this option entirely.
So easiest way would be to challenge the "no Linux command" restriction. Otherwise let's hope a date and time are in the filename.
Hope this helps,
- Jan.
Here's an approach that doesn't require the server to have X command enabled. This could be done in one step, but I'm a fan of using generic macros so I'll do that to get my directory listing first. Two choices for doing this:
* Filelist macro (Don Henderson)
* mp_dirlist macro (yours truly)
Now we have a list of filepaths, we can use the `finfo` function to find the last modified date, convert it to a datetime (using a trusty anydtdtme informat), compare the age, and delete as appropriate!
data _null_;
set work.mp_dirlist;
drop rc fid close;
format modified_dttm datetime19.;
rc=filename("fref",filepath);
fid=fopen("fref");
if fid>0 then do;
modified=finfo(fid,"Last Modified");
modified_dttm=input(modified,anydtdtm24.);
end;
close=fclose(fid);
/* 60secs * 60 mins * 24 hours * 5 days */
if modified_dttm>0 and datetime()-modified_dttm > (60*60*24*5) /* and ext='???'*/ then do;
putlog 'deleting' filename;
rc=fdelete("fref");
end;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.