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 (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info 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 (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info 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 (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info 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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 825 views
  • 4 likes
  • 5 in conversation