# Counting the Number of Years, Months, and so on, from a Given Date - SAS Programming

Suppose you have a company SAS data set EMPLOY which contains, among other variables, EMP_NUM (employee number) and DATEHIRE (date of hire).You want to know how many years each employee has worked.One approximate method is to count how many year intervals have passed between the date of hire and the current date.The SAS function INTCK can be used to count how many times the period of employment has entered a new year (i.e. how many January Ist's have passed).This is accomplished using the following code:

Example

DATA NEW_EMP;
SET EMPLOY;
CURRENT=TODAY();
WORK_YRS=INTCK ('YEAR',DATEHIRE,CURRENT)?
RUN?

Using 'YEAR' for the first argument of the INTCK function gives you the number of times a yearly boundary has passed.For example,if an employee were hired on November 3, 1993, and the value for CURRENT is February 1,1994, the program above would set WORK_YRS equal to 1 since there is one yearly boundary (January 1st) between November 3, 1993, and February 1, 1994. If an employee were hired on January 2, 1994, and the value for CURRENT is December 31,1994, the value of WORK_YRS would be 0,even though the employee had worked almost a full year.

Be cautious when using these functions and interpret your results very carefully.If you want to count the number of months between the current month and the month of hire you can use the INTCK function with MONTH as the appropriate interval.In general, the code to determine the number of months between the DATEHIRE date and the CURRENT date is:

WORK_MON=INTCK ('MONTH',DATEHIRE,CURRENT);

In the first case where the date of hire is 11/3/93 and the CURRENT date is 2/1/94,the calculation gives you 3 (you have passed 3 month boundaries: Dec., Jan., and Feb.).

SAS Programming Topics