Search the site...

SASCRUNCH TRAINING
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
Sentry Page Protection
Please Wait...
Handling Date Values [6-7]


INTCK Function
The INTCK function allows you to compute the duration between two dates.

For example, you can count how many years your customers have been with the company.

You can also count how many months the products have been on the shelf.

Let's look at some examples.

​Copy and run the code in the yellow box below:

The CUSTOMERS data set contains three gym members along with their membership start and end date.
Picture

All three gym members started their membership in 2018 and ended it in 2019:
Picture

Now, let's compute how many days the customers have been gym members.
data customers2;
set customers;
days_join = intck('day', start, end);
run;

The INTCK function in this example takes on three parameters:
  • Interval
  • Start Date
  • End Date
Picture

Interval

The first parameter is the interval. Below are some interval options that you can specify:
  • DAY
  • WEEK
  • MONTH
  • YEAR
  • QTR

In our example, we specified the interval as 'day':
Picture

This tells SAS to compute the duration in days between the membership start and end date:
Picture

John and Peter have been gym members for 424 and 560 days, respectively.

​Mary joined the gym 123 days ago.


'YEAR' Interval

The INTCK function can also be used to calculate the number of years between the two dates. 

Let's look at an example.
data customers2;
set customers;
days_join = intck('day', start, end);
years_join = intck('year', start, end);
run;

​In this example, we specified the interval as 'year'.
Picture

This computes the number of years between the membership start and end dates:
Picture

​
IMPORTANT

You have to be very careful when counting the years between two dates.

The way the INTCK function counts the years (or other intervals) is not the most straightforward.

The INTCK function, by default, counts how many times it changes calendar years during the period.
Picture

In our example, Mary joined the gym in October 2018 and quit in February 2019.
Picture

Although the membership lasted for just four months, it did change from one calendar year (2018) to another (2019).

As a result, SAS considers this membership to be one-year long:
Picture
Picture


INTCK Alignment

There is another way to count the years.

You can specify the 'continuous' alignment so that the years are not counted based on the calendar-year boundary.

Let's look at an example.
data customers2;
set customers;
days_join = intck('day', start, end);
years_join = intck('year', start, end);
years_join_c = intck('year', start, end, 'cont');
run;

In this example, we created a new variable called YEARS_JOIN_C.

The YEARS_JOIN_C variable has a fourth parameter.

The fourth parameter was specified as 'cont', which tells SAS to use the 'continuous' alignment instead of the default alignment.
Picture

When specifying the 'continuous' alignment, the INTCK function counts the number of full years in the entire period. 

​Let's look at Mary as an example.

Picture

Mary's membership started on October 1, 2018 but ended before October 1, 2019.

The entire period lasted for less than a full year:
Picture

SAS returns the value (0), indicating Mary has joined the gym for less than a year. 
Picture

Let's also look at the other two gym members.

John and Peter have in fact been gym members for over a full year.

The INTCK function returns the value (1) for both of them:
Picture


Important

Understanding when to specify the alignment is crucial when using the INTCK function.

Sometimes, the default alignment will return the correct results.

Let's look at another example.

Copy and run the code from the yellow box below:

The EMPLOYEE data set contains a list of five employees and the date of their hiring.
Picture

The company offers medical insurance for employees who have been with the company for at least a year.

Employees are considered to have been with the company for at least a year if they were with the company the previous calendar years.

Below are some examples of the tenure definition as of the year of 2019.
Picture

Now, we are going to compute the tenure for our five employees.

The calculation is done as of January 2, 2019 (beginning of the year).

The INTCK function without the 'continuous' alignment is used for the calculation:
data emp_tenure;
set employee;
tenure = intck('year', hiredate, '02JAN2019'd);
run;

In this example, the interval is specified as 'year'.

The start date and end date are specified as the HIREDATE and the date of January 2, 2019, respectively:
Picture

The employee tenure is calculated as shown below:
Picture

Let's quickly verify the results by looking at employee ID1002.

ID1002 joined the company on September 18, 2018.

Based on the company's HR policy, ID1002 is considered to have already been with the company for a year as of 2019, since he/she was with the company the previous year (2018).
Picture

The tenure calculated is 1, which is correct!

Exercise

Copy and run the code from the yellow box below:

The SHOP data set contains three variables:
  • PRODUCT_ID
  • STOCK_DATE
  • QUANTITY_REM

The PRODUCT_ID is the unique ID of each product.

The STOCK_DATE is the date when the products were stocked and put on the shelf.

The QUANTITY_REM is the quantity that remained in stock as of February 1, 2019. 

The shop owner wants to find out how many products have been on the shelf for three full months as of Feb 1, 2019.

Help the owner calculate the total number of products that are unsold after being in stock for over three months.
Next

Need some help? 


HINT:
You can specify the interval as 'month'. The 'cont' alignment should be used for the calculation.


SOLUTION:

data shop2;
set shop;
where intck('month', stock_date, '01FEB2019'd, 'cont') >= 3;
run;

proc means data=shop2 sum maxdec = 0;
var quantity_rem;
run;


Fill out my online form.
Already a member? Go to member's area.