DATA Step, Macro, Functions and more

How can I extract the modified date of file in datetime format?

Reply
New Contributor
Posts: 2

How can I extract the modified date of file in datetime format?

I am trying to extract the created date and last modified date of a file and print this to an output table.

I have tried the following method using the FINFO function:

filename fileref "%qtrim(%bquote(&FilePath.))";

data &_OUTPUT;

       length Created_Date 8;

       drop fid;

       infile fileref truncover;

       format Created_Date datetime20.;

       fid=fopen('fileref');                                                                            

       Created_Date=input(finfo(fid,'Create Time'), anydtdtm20.);

       Modified_Date=finfo(fid,'Last Modified');

run;

The modified date is given in a character format as "04 December 2013 10:41:07 o'cl      "

But when I try to convert this into a numeric datetime format with an input expression (as with the Created Date), I only get an empty cell.

I tried another method using the following code and it gives the date in a datetime format "04DEC2013:10:41:00", but it does not give me the seconds so it is left as 00.

filename fileref pipe "dir %qtrim(%nrbquote("&FilePath")) /t:w /a:-d";                                                                                         

data &_OUTPUT;

       keep LastModified;

       format LastModified Datetime20.;                                                                                                                         

       infile fileref firstobs=6;                                                                                                               

       input mod_date ?? : ddmmyy8. mod_time ?? & time8.;                                                                                   

       if mod_date eq . then stop;                                                                                                          

       LastModified = DHMS(mod_date,HOUR(mod_time),MINUTE(mod_time),SECOND(mod_time)); 

run;

Does anyone know if there is an easier way of getting the modified date time with seconds, or converting the character format?

Regular Contributor
Posts: 244

Re: How can I extract the modified date of file in datetime format?

What OS are you using?  I get a normal date format returned by Modified Date ("14Feb2013:13:51:19" for a test file for example). 

New Contributor
Posts: 2

Re: How can I extract the modified date of file in datetime format?

Thanks for your reply, SAS is using windows server 2008, I have noticed another thread had a similar issue with the finfo function: https://communities.sas.com/thread/38083

I don't know how to get it to extract it in the format I need, so I had to use this conversion to give me the format I need:

  format LastModified datetime20.;

  fid=fopen('fileref'); /*fid is the file-id*/

  Mod_Date=finfo(fid,'Last Modified');

  LastModified=INPUT((TRIM(SCAN(Mod_Date ,1," ") || SUBSTRN(SCAN(Mod_Date ,2," "),1,3) || SCAN(Mod_Date ,3," ")) || ":" || SCAN(Mod_Date ,4," ")),datetime20.);

New Contributor
Posts: 3

Re: How can I extract the modified date of file in datetime format?

@sklm, the lastmodified variable ended up blank for me. The code below worked, through:

   data last_mod_date;

  format LastModified datetime20.;

  fid=fopen('fileref'); /*fid is the file-id*/

  Mod_Date=finfo(fid,'Last Modified');

  LastModified=input(put(mod_date,$20.),datetime20.);

  run;

Ask a Question
Discussion stats
  • 3 replies
  • 3580 views
  • 0 likes
  • 3 in conversation