BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
statsman58
Calcite | Level 5

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

3 REPLIES 3
Tom
Super User Tom
Super User

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

 

novinosrin
Tourmaline | Level 20

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

ballardw
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 9246 views
  • 3 likes
  • 4 in conversation