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
SAS Super FREQ

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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