BookmarkSubscribeRSS Feed
Aayushi_17
Quartz | Level 8

hi,

 

How to calculate difference  for these two dates 

DTC_1DTC_2
2021-06-11T22:202021-06-12 
2021-08-30T16:582021-10-05 
2021-10-05T07:17 
2021-10-07T15:142021-10-08 
2021-10-09T16:222021-10-10 
2021-08-30T09:542021-08-30 

 

Required difference should look like this

Required date diff 
46:22:00
40:58:00
 
39:14:00
40:22:00
09:54

Thanks in advance.

12 REPLIES 12
andreas_lds
Jade | Level 19

Please post data in usable form and show what you expect as result for each observation. Since the second variable is a date, not a datetime, your task needs clarification.

PaigeMiller
Diamond | Level 26

In addition to the above requests, we need additional information. Are these variables numeric or character? How are they formatted? Please show us the PROC CONTENTS output for these two variables.

--
Paige Miller
PaigeMiller
Diamond | Level 26

Please explain how for record 1, the desired output is 26:22:00.

 

It seems to me the difference is 1 hour and 40 minutes.

--
Paige Miller
Aayushi_17
Quartz | Level 8

hi,

 

How to calculate difference  for these two dates 

DTC_1DTC_2
2021-06-11T22:202021-06-12 
2021-08-30T16:582021-10-05 
2021-10-05T07:17 
2021-10-07T15:142021-10-08 
2021-10-09T16:222021-10-10 
2021-08-30T09:542021-08-30 

 

Required difference should look like this

Required date diff 
26:22:00
40:58:00
 
39:14:00
40:22:00
09:54

Thanks in advance.

PaigeMiller
Diamond | Level 26

Duplicate thread. all responses should go to the other thread at https://communities.sas.com/t5/SAS-Programming/how-to-calculate-difference-between-two-dates-when-on...

--
Paige Miller
andreas_lds
Jade | Level 19

I have merged the both threads.

Aayushi_17
Quartz | Level 8
the difference for one record is one day and 22 hours so 24+22 hours...
PaigeMiller
Diamond | Level 26

@Aayushi_17 wrote:
the difference for one record is one day and 22 hours so 24+22 hours...

I am not following this. It seems to me the difference is 1 hour and 40 minutes. The difference between 2021-06-11T22:20 and 2021-06-12 is ... lets see, midnight on 2021-06-12 is the day after 2021-06-11T22:20, and it is 1 hour and 40 minutes between 2021-06-11T22:20 and midnight on 2021-06-12.

 

Please provide more details, step-by-step about how you calculate your answer.

--
Paige Miller
Tom
Super User Tom
Super User

First some language clarification.  You appear to have character strings that you want to interpret as DATETIME values (not DATE values).  SAS stores date values as number of days. It stores datetime values as number of seconds.  The difference between two datetime values will be TIME values, not DATE values.

 

So just convert your string into datetime values and then subtract.  

data want;
  set have ;
  dt1=input(dtc_1,b8601dt20.);
  dt2=input(dtc_2,b8601dt20.);
  diff = dt2-dt1;
  format dt1 dt2 datetime19. diff time12.;
run;

But the result

Obs      DTC_1         DTC_2                   dt1                dt2      diff

 1  2021-06-11T22:20 2021-06-12 11JUN2021:22:20:00 12JUN2021:00:00:00   1:40:00
 2  2021-08-30T16:58 2021-10-05 30AUG2021:16:58:00 05OCT2021:00:00:00 847:02:00
 3  2021-10-05T07:17            05OCT2021:07:17:00                  .         .
 4  2021-10-07T15:14 2021-10-08 07OCT2021:15:14:00 08OCT2021:00:00:00   8:46:00
 5  2021-10-09T16:22 2021-10-10 09OCT2021:16:22:00 10OCT2021:00:00:00   7:38:00
 6  2021-08-30T09:54 2021-08-30 30AUG2021:09:54:00 30AUG2021:00:00:00  -9:54:00

does not look much like your requested result.  How did you get the time intervals you posted? What datetime value did you think that a string like '2021-08-30' represents?

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 12 replies
  • 1510 views
  • 1 like
  • 5 in conversation