DATA Step, Macro, Functions and more

SAS DateTImes

Reply
Frequent Contributor
Posts: 78

SAS DateTImes

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

Super User
Posts: 23,980

Re: SAS DateTImes

Posted in reply to Malathi13

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

SAS Employee
Posts: 127

Re: SAS DateTImes

Posted in reply to Malathi13

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;

Frequent Contributor
Posts: 78

Re: SAS DateTImes

Posted in reply to Malathi13

Thank you all, I got the answer.

 

M

Ask a Question
Discussion stats
  • 3 replies
  • 89 views
  • 1 like
  • 3 in conversation