BookmarkSubscribeRSS Feed
Malathi13
Obsidian | Level 7

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

3 REPLIES 3
Reeza
Super User

Datetimes are seconds. Personally, it seems easiest to convert your requirement, 60 minutes into seconds and use that criteria.

SASJedi
Ammonite | Level 13

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;

Check out my Jedi SAS Tricks for SAS Users
Malathi13
Obsidian | Level 7

Thank you all, I got the answer.

 

M

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1278 views
  • 1 like
  • 3 in conversation