turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- INTCK to compute minutes between dates

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

2 weeks ago - last edited 2 weeks ago

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

2 weeks ago

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

2 weeks ago - last edited 2 weeks ago

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

All Replies

Solution

2 weeks ago

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

2 weeks ago - last edited 2 weeks ago

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

2 weeks ago

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

2 weeks ago

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.