BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14

Hello

I want to calculate difference in minutes between two datetime fields.

I want to calculate difference in seconds between two datetime fields.

my calculation provide wrong results:

INTCK('minute',application_DateTime,Loan_datetime)

INTCK('seconds',application_DateTime,Loan_datetime)

 


data have;
format  Loan_DateTime Application_DateTime  datetime20.;
input Loan_DateTime ::datetime18.  Application_DateTime ::datetime18.;
cards;
01SEP2023:09:59:00 01SEP2023:09:58:16
;
Run;

data want;
set have;
dif_minutes=INTCK('minutes',Application_DateTime,Loan_DateTime);
dif_seconds=INTCK('seconds',Application_DateTime,Loan_DateTime);
Run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

@Ronein,

 

Let's say you have a difference of 90 seconds between the two timestamps.  There are a variety of ways to express that, including (but not limited to):

 

90 seconds

1 minute 30 seconds

1.5 minutes

01:30

 

So variable names are up to the programmer.  What you want is up to you.  When I looked at the problem, I thought the second expression was what you were asking for.  So I chose DURATION as the top version (90 seconds) and used it in the calculations to obtain the numbers in the second line (1 minute, 30 seconds).  But if that is not your preference, you can fiddle with it pretty easily.

View solution in original post

7 REPLIES 7
LinusH
Tourmaline | Level 20

Not following you, they are the same value, so difference should be 0...?

Data never sleeps
Ronein
Meteorite | Level 14

sorry I wrote wrong date time values.

Anyway,I saw the correct way to do it:

for dif in  seconds need to substract the two datetimes

for dif  in minutes need to use INTCK

For 

Astounding
PROC Star

Your datetime fields are already measured in seconds.  So  you could use:

duration = Application_DateTime - Loan_DateTime;
dif_in_minutes = int(duration / 60);
dif_in_seconds = duration - 60*dif_in_minutes;
Ronein
Meteorite | Level 14

I saw that difference in seconds is calculated by:

Application_DateTime - Loan_DateTime;

Why did you call it duration and what is the difference between duration and dif_in_seconds? 

Astounding
PROC Star

@Ronein,

 

Let's say you have a difference of 90 seconds between the two timestamps.  There are a variety of ways to express that, including (but not limited to):

 

90 seconds

1 minute 30 seconds

1.5 minutes

01:30

 

So variable names are up to the programmer.  What you want is up to you.  When I looked at the problem, I thought the second expression was what you were asking for.  So I chose DURATION as the top version (90 seconds) and used it in the calculations to obtain the numbers in the second line (1 minute, 30 seconds).  But if that is not your preference, you can fiddle with it pretty easily.

Ronein
Meteorite | Level 14

Question1-

What is the way to calculate the complete number of minutes difference between two datetimes via INTCK?

In this example should get "1"  but with my INTCK I got "2"?

data have;
format  Loan_DateTime Application_DateTime  datetime20.;
input  Application_DateTime ::datetime18. Loan_DateTime ::datetime18.;
cards;
01SEP2023:09:57:16 01SEP2023:09:59:00
;
Run;
/**Note-datetime fields are already measured in seconds**/

Data want;
set Have;
duration_in_seconds=Loan_DateTime-Application_DateTime ;/***104 seconds**/
/*altenative way :duration_in_seconds=intck('seconds',Application_DateTime,Loan_DateTime);*/
dif_in_minutes = int(duration_in_seconds / 60);  /**1 minute**/
dif_in_seconds = duration_in_seconds - 60*dif_in_minutes;/**44 seconds***/
/*Dont USE this calc-intck('minutes',Application_DateTime,Loan_DateTime);*/
Run;

Question2-

Let's say that I want to show difference in minutes but also partial minutes.

So difference between 01SEP2023:09:57:16  and 01SEP2023:09:59:00 should get 1.73  (since 44 seconds divide by 60).

What is the way to calculate it please?

Here I found the solution

duration_in_seconds=Loan_DateTime-Application_DateTime ;/***104 seconds**/
duration_in_minutes=duration_in_seconds/60;/***1.73***/

Astounding
PROC Star
INTCK is counting the number of times a boundary is crossed. Your program crosses a "minute" boundary twice: once at 9:57:00, and once at 9:58:00, so you get 2.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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