Hi,
I need help on SAS datetimes. I always struggle with datetimes in SAS. I need to find a difference of two datetimes and then delete times that are <0 and >60 minutes.
I know how to find the difference, here's ,my sample code. I have two Time variables with datetime1=01OCT17:16:11:30 and datetime2=01OCT17:16:16:50.
Date new;
Set have;
Time1=timepart(datetime1);
Time2=timepart(datetime2);
Format time1 time2 time.;
Diff=time2-time;
Format diff time8.;
If diff <=0 and diff >60 then delete; (I’m having problem here, as it has seconds and the diff variable is not a whole number.)
Run;
How can I get the output in 00:30:05 format and delete <0 and >60 minutes and get the 90th percentiles in 00:30:05 format (include minutes and seconds).
Thank you
M
Datetimes are seconds. Personally, it seems easiest to convert your requirement, 60 minutes into seconds and use that criteria.
This is much more easily accomplished with the SAS INTCK function, which is designed to calculate intervals between dates, times or
data have;
call streaminit(123456);
ID=0;
do datetime1='01JAN2017 00:00:00'dt to '31JAN2017 00:00:00'dt by 24*60*60;
ID+1;
if rand('UNIFORM') > .1 then datetime2=datetime1+rand('NORMAL',3600,500);
else if rand('UNIFORM') > .9 then datetime2=datetime1+72000;
else datetime2=datetime1-rand('NORMAL',3600,500);
output;
end;
format da: datetime.;
run;
title 'Have';
proc print;
run;
data new;
set have;
diff=INTCK('dtminute',datetime1,datetime2) ;
If diff <=0 or diff =>60 then delete;
run;
title 'Want';
proc print;
run;
title;
Thank you all, I got the answer.
M
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.