INTCK to compute minutes between dates

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

INTCK to compute minutes between dates

[ Edited ]

I'm attempting to use the INTCK function to calculate the number of minutes between two dates but am getting the wrong answer.

When I calculate intck('min',"21JUL2017 9:06"d,"23JUL2017 :10:28"d)  I get 2,666 minutes.  

When I calculate ("23JUL2017 :10:28"d-"21JUL2017 9:06"d)/(1440*60)  I get 2,665.483 minutes.  

 

When I calculate the same in MS Excel, I get 2,962 minutes which is correct.

 

I did observe that SAS calculates the difference in minutes correctly when the two dates are the same; just differs in time.


I'm certain this has something to do with how SAS defines 'intervals' but I'm at a loss how to fix this.

Please advise.
Thanks!  Sam

 


Accepted Solutions
Solution
‎08-08-2017 02:49 PM
Super User
Super User
Posts: 7,039

Re: INTCK to compute minutes between dates

[ Edited ]
Posted in reply to statsman58

First, SAS datetime values are in seconds. So if you want to calculate minuates by yourself you need to divide by 60.

Second your actual dates do not match the values you posted. 

 

data test;
  infile cards dsd dlm='|';
  input start stop;
  informat start stop datetime.;
  diff1 = (stop - start)/60;
  diff2 = intck('min',start,stop);
  format start stop datetime24.3 diff: comma12.3;
  put (_all_) (= /);
cards;
21JUL2017 9:06|23JUL2017 :10:28
;
start=21JUL2017:09:06:00.000
stop=23JUL2017:10:28:00.000
diff1=2,962.000
diff2=2,962.000

Your code is also using Date literals instead of Datetime literals. So that is going to generate nonsense results.

 

205   data test;
206     start = "21JUL2017 9:06"d ;
207     stop ="23JUL2017 :10:28"d;
208     diff1 = (stop - start)/60;
209     diff2 = intck('min',start,stop);
210     format start stop datetime24.3 diff: comma12.3;
211     put (_all_) (= /);
212   run;


start=01JAN1960:05:50:21.000
stop=01JAN1960:05:50:23.000
diff1=0.033
diff2=0.000

If you use Datetime literals instead then you can get valid results.

196   data test;
197     start = "21JUL2017 9:06"dt ;
198     stop ="23JUL2017 :10:28"dt;
199     diff1 = (stop - start)/60;
200     diff2 = intck('min',start,stop);
201     format start stop datetime24.3 diff: comma12.3;
202     put (_all_) (= /);
203   run;


start=21JUL2017:09:06:00.000
stop=23JUL2017:10:28:00.000
diff1=2,962.000
diff2=2,962.000

 

View solution in original post


All Replies
Solution
‎08-08-2017 02:49 PM
Super User
Super User
Posts: 7,039

Re: INTCK to compute minutes between dates

[ Edited ]
Posted in reply to statsman58

First, SAS datetime values are in seconds. So if you want to calculate minuates by yourself you need to divide by 60.

Second your actual dates do not match the values you posted. 

 

data test;
  infile cards dsd dlm='|';
  input start stop;
  informat start stop datetime.;
  diff1 = (stop - start)/60;
  diff2 = intck('min',start,stop);
  format start stop datetime24.3 diff: comma12.3;
  put (_all_) (= /);
cards;
21JUL2017 9:06|23JUL2017 :10:28
;
start=21JUL2017:09:06:00.000
stop=23JUL2017:10:28:00.000
diff1=2,962.000
diff2=2,962.000

Your code is also using Date literals instead of Datetime literals. So that is going to generate nonsense results.

 

205   data test;
206     start = "21JUL2017 9:06"d ;
207     stop ="23JUL2017 :10:28"d;
208     diff1 = (stop - start)/60;
209     diff2 = intck('min',start,stop);
210     format start stop datetime24.3 diff: comma12.3;
211     put (_all_) (= /);
212   run;


start=01JAN1960:05:50:21.000
stop=01JAN1960:05:50:23.000
diff1=0.033
diff2=0.000

If you use Datetime literals instead then you can get valid results.

196   data test;
197     start = "21JUL2017 9:06"dt ;
198     stop ="23JUL2017 :10:28"dt;
199     diff1 = (stop - start)/60;
200     diff2 = intck('min',start,stop);
201     format start stop datetime24.3 diff: comma12.3;
202     put (_all_) (= /);
203   run;


start=21JUL2017:09:06:00.000
stop=23JUL2017:10:28:00.000
diff1=2,962.000
diff2=2,962.000

 

PROC Star
Posts: 283

Re: INTCK to compute minutes between dates

Posted in reply to statsman58

Here is my test:

 


10 data want;
11 interval=intck('min',"21JUL2017: 9:06"dt,"23JUL2017 :10:28"dt);
12 put interval=;
13 run;

interval=2962
NOTE: The data set WORK.WANT has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.04 seconds
cpu time 0.01 seconds

Super User
Posts: 11,343

Re: INTCK to compute minutes between dates

Posted in reply to statsman58

Where are you "calculating" ("23JUL2017 :10:28"d-"21JUL2017 9:06"d)/(1440*60)? The actual issue is misunderstanding you Date, Time and DATETIME literals are expressed and stored.

If you run this code:

data junk;
 y= "23JUL2017 :10:28"d;
 z = "21JUL2017 9:06"d;

run;

You will see that y and z have values of 21023 and 21021, which are dates 23Jul2017 and 21Jul2017. The "time" portion is completely ignored. So dealing with the days difference and dividing isn't yielding minutes in either form. Dates are stored as DAYS since 1 Jan 1960, DATETIME is seconds from midnigh 1 Jan 1960, Time is seconds since midnight.

 

If you want to enter a datetime literal then the form is with a DT

data junk;
 y= "23JUL2017:10:28"dt;
 z = "21JUL2017:09:06"dt;
 format y z datetime18.;
 /* subtraction is in seconds*/
 dif= (y-z)/60;
 mindif = intck('minute',z,y);

run;

 

Which both yield 2962.

 

Notice that the order of values in intck will make a difference of positive or negative number of intervals.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 136 views
  • 2 likes
  • 4 in conversation