####
**Live Online Training :**
SAS Programming with 50+ Case Studies

- Explain Programming Concepts in Simple English

- Live Projects

- Scenario Based Questions

- Job Placement Assistance

- Get 10% off till Oct 26, 2017

- Batch starts from October 28, 2017

The INTCK is one of the most important date function that is used to calculate the difference between two dates, two times or two datetime values.

The following is a list of common real-world examples where INTCK is used -

- Calculation of individual's age
- Tenure of an employee with company
- Customer's tenure with the organization
- Number of working days
- Number of hours spent on a particular course
- Number of quarterly payments paid

**INTCK - Syntax**

The syntax of INTCK is defined below -

INTCK(date-or-time-interval, start-date-or-time, end-date-or-time, [method])

1.

**date-or-time-interval :**Date or time period needs to be defined in the first parameter. For eg. MONTH, YEAR, QTR, WEEK, HOUR, MINUTE etc.**Specify period in single quotes**2.

**start-date-or-time**

**:**Starting date or time to calculate the number of periods.

3.

**end-date-or-time****:**End date or time to calculate the number of periods.4.

**method : Optional Parameter.**Method to calculate the difference.

**Methods are 'CONTINUOUS' or 'DISCRETE'. By default, it is DISCRETE.**

**Simplest Example of INTCK**

Calculate the number of years between two dates. In this case, two dates are 01JAN2015 and 01JAN2017.

data temp;The

date1 = '01JAN2015'd;

date2 = '01JAN2017'd;

no_of_years = intck ('YEAR', date1, date2);

format date1 date2 date9.;

proc print data = temp;

run;

**'YEAR'**keyword tells SAS to calculate the number of intervals between dates in terms of year. Since 01JAN2015 is a starting date, it is specified in the INTCK function before 01JAN2017. The FORMAT statement is used to display datevalues in date format when we print our results.

**The output is shown below -**

SAS : INTCK Function |

**Other alias of year - 'YEARS' and 'YR'-**

no_of_years = intck ('YEARS', date1, date2)

no_of_years = intck ('YR', date1, date2)

**SAS INTCK Examples**

Like calculation of years, we can use other intervals such as semiyear, quarter, month, week, day. The examples of these intervals are displayed below -

data temp;

date1 = '01JAN2015'd;

date2 = '01JAN2017'd;

no_of_years = intck ('YEAR', date1, date2);

no_of_semiyears = intck ('SEMIYEAR', date1, date2);

no_of_quarters = intck ('QUARTER', date1, date2);

no_of_months = intck ('MONTH', date1, date2);

no_of_weeks = intck ('WEEK', date1, date2);

no_of_days = intck ('DAY', date1, date2);

format date1 date2 date9.;

proc print data = temp noobs;

run;

INTCK Examples |

**Custom Intervals**

Suppose you are asked to

**calculate the number of 4 months interval between two dates**-

data temp;

date1 = '01JAN2015'd;

date2 = '01JAN2017'd;

no_of_4months = intck ('MONTH4', date1, date2);

run;

The

**MONTH4**interval implies interval is of 4 months. It is equal to the number of months divided by 4. Don't confuse it with QUARTERS. QUARTERS is equal to interval of 3 months. Remember 4 Quarters in an year.

**Result :**no_of_4months = 6

Similarly, we can use the custom intervals in YEAR, QUARTER and other periods. For example, 'YEAR2' tells SAS the interval is of 2 years. It would return 1 for the above mentioned dates.

**Set Starting Point for Calculation**

data temp;

date1 = '31JAN2015'd;

date2 = '31DEC2016'd;

diff = intck ('YEAR', date1, date2);

diff2 = intck ('YEAR.3', date1, date2);

format date1 date2 date9.;

proc print;

run;

Function | Result |
---|---|

intck ('YEAR', '31JAN2015'd, '31DEC2016'd) | 1 |

intck ('YEAR.3', '31JAN2015'd, '31DEC2016'd) | 2 |

**How it works :**

**intck ('YEAR', date1, date2)**- It checks number of times first of January appears as first of january is set as a starting point by default. The variable diff returns 1 as it includes only 01JAN 2016.**intck ('YEAR.3', date1, date2)**- It checks number of times first of March appears as YEAR.3 refers to the period starting from 1st of March to end of February next year. The variable diff2 returns 2 as it includes 01 March 2015 and 01March 2016.

**Is it a month difference?**

INTCK says there is a month difference between 25OCT2016 and 03NOV2016. But there is no month difference between 01OCT2016 and 31OCT2016. How?

data temp;

month1= intck('month', '25OCT2016'd, '03NOV2016'd);

month2= intck('month', '01OCT2016'd, '31OCT2016'd);

proc print;

run;

Function | Result |
---|---|

intck ('month', '25OCT2016'd, '03NOV2016'd) | 1 |

intck ('month', '01OCT2016'd, '31OCT2016'd) | 0 |

**INTCK checks whether the first day of the month lies with in the range**. In the first case, 01 November falls between October 25 and November 03 so it returns 1. In the second case, it returns 0 as 01 November does not fall between 01OCT2016 and 31OCT2016.

*How to correct it?*Add one more parameter at end of INTCK function. In the parameter, specify

**'C'**which refers to

**continuous method**for calculation.

data temp;

month1= intck('month', '25OCT2016'd, '03NOV2016'd,'C');

proc print;

run;

**The above function returns 0.**

The

**CONTINUOUS method**calculates continuous time from the start-of-period date specified in the second parameter of INTCK function.

**Calculating Weekdays**

Suppose you are asked to calculate the number of weekdays -

data eg;

weekdays = intck('WEEKDAY', '11DEC2016'd ,'18DEC2016'd);

proc print;

run;

**It returns 5**. In this case,

**saturday and sunday are considered weekends**and excluding from the calculation.

**Define 6 days working**

If you need to calculate number of working days between 2 dates considering

**6 weekdays**-

data eg;WEEKDAY1W implies sunday as weekend (1=Sunday, 2= MONDAY... 7=Saturday)

weekdays = intck('WEEKDAY1W', '11DEC2016'd ,'18DEC2016'd);

proc print;

run;

**Set Custom Weekends**

data eg;

weekdays = intck('WEEKDAY24W', '11DEC2016'd ,'16DEC2016'd);

proc print;

run;

**WEEKDAY24W**means MONDAY and WEDNESDAY are weekends.

**The above function returns 3**

**Calculate between Datetime values**

Suppose you need to calculate hours, minutes and seconds between two datetime values.

data temp2;

hours=intck('hour','01jan2016:10:50:00'dt,'01jan2016:11:55:00'dt);

minutes=intck('minute','01jan2016:10:50:00'dt,'01jan2016:11:55:00'dt);

seconds=intck('second','01jan2016:10:50:00'dt,'01jan2016:11:55:00'dt);

proc print noobs;

run;

Time Calculation |

**Result -**1 hour, 65 minutes and 3900 seconds

**Time Difference**

data temp3;

hours=intck('hour','12:00:00't, '23:05:00't);

minutes=intck('minute','12:00:00't,'23:05:00't);

seconds=intck('second','12:00:00't,'23:05:00't);

proc print noobs;

run;

**Result :**11 hours 665 minutes 39900 seconds

Thank u so much Deepanshu Bhalla. Awesome Explanation.

ReplyDeleteI have a doubt,

In the Set Custom Weekends part the differnce b/w the dates 11DEC2016 and 16DEC2016 is '6 days'. So on passing the function 'WEEKDAY24W' it will consider Monday and Wednesday as weekends, so the number of weekdays must be

(6-2) = 4 right? But y is it returning 3? Can u please explain?

It is because the difference between two days are 5 (not including 16DEC). See the code below -

Deletedata eg;

days = intck('DAY', '11DEC2016'd ,'16DEC2016'd);

proc print;

run;

Thank You got it. :)

DeleteHi thank you for your help. I just have one question - When I am trying to find out the no of weeks between 01/11/2014 and 01/03/2015, it is showing 51 when the answer should be 52. Can you please suggest why this is so?

ReplyDeleteWeek start from Sunday. As you are taking date which is starting from Saturday, it is calculating Saturday as 1 week.

Deletetry to use continuous method for calculation.

Great explaination. Thank you

ReplyDeleteHi Deepanshu, when i m using "C" in intck fn sas hitting error saying "intck function call has too many arguments". please explain.

ReplyDelete"c" method is not supported in older version.

DeleteApp_Yr_Mon = intnx('month',disbursed_dt,0,'E').....What does E stand for ?

ReplyDelete