BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
freshstarter
Obsidian | Level 7

Hi,

 

I have a requirement to extract the time ( i.e hours ) based on current time and last modofied date and time of a file. I have written a code to extract the last modified time which output as '01 January 2024  16:08:04'.

 

How to derive the hours ( i.e Current timestamp - 01 January 2024 16:08:01 ) in a SAS code  ? Please help me out. 

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

Your main code looks close.  You're missing a \ in the file name, should be C:\test.txt.  

 

You don't need to use the macro language, and this bit doesn't make sense to me:

%let timer_start = %sysfunc (%flm);

You can use the DATETIME() function in your first DATA step, and do the subtraction in that step to calculate the age of a file (in seconds), like:

 

filename fname 'C:\test.txt';

data want ;
  fid = fopen('fname');
  if fid then do ;
    flm = finfo(fid,'Last Modified');
    lm=input(flm,datetime18.);
  end;
  else put 'Cannot openfile';

  *age of the file, in seconds ;
  dur=datetime() -lm ;

  format lm datetime18. dur time.;
run;

proc print data=want;
run;
The Boston Area SAS Users Group is hosting free webinars!
Next up: SAS Trivia Quiz hosted by SAS on Wednesday May 21.
Register now at https://www.basug.org/events.

View solution in original post

9 REPLIES 9
freshstarter
Obsidian | Level 7

filename fname 'C:test.txt';

data _null_;

fid = fopen('fname');

if fid then dol

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

lm=input(flm,datetime18.);

end;

else put 'Cannot openfile';

run;

 

%let timer_start = %sysfunc (%flm);

 

data _null_;

dur=datetime() - &timer_start ;

run;

 

But there are erros in the code, which Im looking to resolve.

Quentin
Super User

Your main code looks close.  You're missing a \ in the file name, should be C:\test.txt.  

 

You don't need to use the macro language, and this bit doesn't make sense to me:

%let timer_start = %sysfunc (%flm);

You can use the DATETIME() function in your first DATA step, and do the subtraction in that step to calculate the age of a file (in seconds), like:

 

filename fname 'C:\test.txt';

data want ;
  fid = fopen('fname');
  if fid then do ;
    flm = finfo(fid,'Last Modified');
    lm=input(flm,datetime18.);
  end;
  else put 'Cannot openfile';

  *age of the file, in seconds ;
  dur=datetime() -lm ;

  format lm datetime18. dur time.;
run;

proc print data=want;
run;
The Boston Area SAS Users Group is hosting free webinars!
Next up: SAS Trivia Quiz hosted by SAS on Wednesday May 21.
Register now at https://www.basug.org/events.
freshstarter
Obsidian | Level 7

Thanks all..I slightly tweeked the code and it worked fine.

 

filename fname 'C:\test.txt';

data want ;
  fid = fopen('fname');
  if fid then do ;
    flm = finfo(fid,'Last Modified');
    lm=input(flm,anydtdtm60.);
  end;
  else put 'Cannot openfile';

  *age of the file, in seconds ;
  dur=datetime() -lm ;
dur_hour=hour(dur);

  format lm datetime18. dur time. ;
run;

proc print data=want;
run;
Tom
Super User Tom
Super User

Note that the format of the strings returned by FINFO() depend on your language settings.  I have found the using NLDATM informat works consistently.  Also the format of the strings used to specify the information to return depends on the language setting.  You can use FOPTNAME() to fix that. 

lastmod = input(finfo(fid,foptname(fid, 5)), nldatm100.);

 

To see this in action check out https://github.com/sasutils/macros/blob/master/dirtree.sas

 

Also the DATETIME format has a bug and you should not use DATETIME18. Even though that should be enough room to include 4 digit years it will not.  So either use DATETIME19. (four digit years) or DATETIME16. (two digit years).

Quentin
Super User

@Tom wrote:

Note that the format of the strings returned by FINFO() depend on your language settings.  I have found the using NLDATM informat works consistently.  Also the format of the strings used to specify the information to return depends on the language setting.  You can use FOPTNAME() to fix that. 

lastmod = input(finfo(fid,foptname(fid, 5)), nldatm100.);

 

To see this in action check out https://github.com/sasutils/macros/blob/master/dirtree.sas

 

Also the DATETIME format has a bug and you should not use DATETIME18. Even though that should be enough room to include 4 digit years it will not.  So either use DATETIME19. (four digit years) or DATETIME16. (two digit years).


That DATETIME18. format bug drives me crazy every time I re-encounter it.

The Boston Area SAS Users Group is hosting free webinars!
Next up: SAS Trivia Quiz hosted by SAS on Wednesday May 21.
Register now at https://www.basug.org/events.
PaigeMiller
Diamond | Level 26

@freshstarter wrote:

I have written a code to extract the last modified time which output as '01 January 2024  16:08:04'.


Is this value above stored in a text (character) variable or a numeric variable? Use PROC CONTENTS (or similar) to look it up.

--
Paige Miller
freshstarter
Obsidian | Level 7

THe value stored in a char data type. Im trying to change to proper format and then subracting with the current time. But the code didnot help

PaigeMiller
Diamond | Level 26

Example:

 

data have;
    dt='01 January 2024  16:08:04';
    dt_numeric=input(dt,anydtdtm60.);
    hour=hour(dt_numeric);
    format dt_numeric datetime20.;
run;
--
Paige Miller

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

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
  • 9 replies
  • 1956 views
  • 4 likes
  • 5 in conversation