SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
sklm
Calcite | Level 5

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?

3 REPLIES 3
snoopy369
Barite | Level 11

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). 

sklm
Calcite | Level 5

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.);

JohnC_Munoz
Calcite | Level 5

@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;

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 11848 views
  • 0 likes
  • 3 in conversation