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.
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;
Show us your code 🙂
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.
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;
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;
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).
@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.
@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.
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
Example:
data have;
dt='01 January 2024 16:08:04';
dt_numeric=input(dt,anydtdtm60.);
hour=hour(dt_numeric);
format dt_numeric datetime20.;
run;
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!
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.