Quantcast
Channel: ListenData
Viewing all articles
Browse latest Browse all 425

SAS : INTCK Function with Examples

$
0
0
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 -
  1. Calculation of individual's age
  2. Tenure of an employee with company
  3. Customer's tenure with the organization
  4. Number of working days
  5. Number of hours spent on a particular course
  6. 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;
date1 = '01JAN2015'd;
date2 = '01JAN2017'd;
no_of_years  = intck ('YEAR', date1, date2);
format date1 date2 date9.;
proc print data = temp;
run;
The '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;
FunctionResult
intck ('YEAR', '31JAN2015'd, '31DEC2016'd)1
intck ('YEAR.3', '31JAN2015'd, '31DEC2016'd)2

How it works :
  1. 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.
  2. 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;

FunctionResult
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;
weekdays = intck('WEEKDAY1W', '11DEC2016'd ,'18DEC2016'd);
proc print;
run;
WEEKDAY1W implies sunday as weekend (1=Sunday, 2= MONDAY... 7=Saturday)

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

Viewing all articles
Browse latest Browse all 425

Trending Articles