BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tarunchitkara
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
jklaverstijn
Rhodochrosite | Level 12

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.

View solution in original post

5 REPLIES 5
LinusH
Tourmaline | Level 20
Check out the external files function category.
Is the date reflected on the file name?
If not, you need to extract file attribute information.
Data never sleeps
Reeza
Super User

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?

 

http://support.sas.com/documentation/cdl/en/hostunx/67929/HTML/default/viewer.htm#n13ozwpq7az8v6n1s7...

jklaverstijn
Rhodochrosite | Level 12

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.

tarunchitkara
Fluorite | Level 6
Thank you very much jan.
AllanBowe
Barite | Level 11

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;

 

 

/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs

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 connect to databases in SAS Viya

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.

Discussion stats
  • 5 replies
  • 4042 views
  • 3 likes
  • 5 in conversation