Difference between two dates in days weeks months quarter and years in SAS

Difference between two dates in days weeks months quarter and years in SAS is accomplished using INTCK() Function. Difference between two dates in days in SAS using INTCK() takes ‘day’ as argument, Difference between two dates in weeks in SAS using INTCK() takes ‘week’ as argument, Difference between two dates in months in SAS using INTCK() takes ‘months’ as argument. Difference between two dates in quarter in SAS using INTCK() takes ‘quarter’ as argument. Difference between two dates in year in SAS using INTCK() takes ‘year’ as argument. Let’s see an example of each

  • Difference between two dates in days in SAS
  • Difference between two dates in weeks in SAS
  • Difference between two dates in months in SAS
  • Difference between two dates in quarters in SAS
  • Difference between two dates in years in SAS

Syntax INTCK in SAS:

INTCK(‘Interval’, start_date, end_date)
  • Interval – can be in weeks, days, months,quarter, year and can also be in time
  • Start_date and end_date are between two dates which we will be finding interval

So we will be using EMP_DET Table in our example

Difference between two dates in days months and years in SAS 1

 

 

Difference between two dates in days in SAS

Difference between two dates in days is accomplished using INTCK function with ‘day’ as argument as shown below


/* difference between two dates in days */
 
data emp_det1; 
set emp_det; 
days_since = intck('day',DOJ,curr_date); 
run;

So the resultant table will be

Difference between two dates in days months and years in SAS 2

 

 

Difference between two dates in weeks in SAS

Difference between two dates in weeks is accomplished using INTCK function with ‘week’ as argument as shown below

/* difference between two dates in weeks */ 

data emp_det1; 
set emp_det; 
week_since = intck('week',DOJ,curr_date); 
run;

So the resultant table will be

Difference between two dates in days months and years in SAS 3

 

 

Difference between two dates in months in SAS

Difference between two dates in months is accomplished using INTCK function with ‘months’ as argument as shown below


/* difference between two dates in months */ 

data emp_det1; 
set emp_det; 
month_since = intck('months',DOJ,curr_date); 
run;

So the resultant table will be

Difference between two dates in days months and years in SAS 4

 

 

Difference between two dates in year in SAS – Method 1: complete year – rounding off year

Difference between two dates in year is accomplished using INTCK function with ‘year’ as argument as shown below. It rounds off to the complete year i.e. if difference between two dates are 1.5 years it will round off to 2 years.


/* difference between two dates in years - returning complete year */ 

data emp_det1; 
set emp_det; 
year_since = intck('year',DOJ,curr_date); 
run;

So the resultant table will be

Difference between two dates in days months and years in SAS 5

 

 

Difference between two dates in year in SAS – Method 2

In Second method we can find difference between two dates in days and divide it by 365.25, so you will get difference in years. It is a roundabout way to calculate difference between two dates in years which will result in exact difference between two dates in terms of years.

/* difference between two dates in years - returning year */ 

data emp_det1; 
set emp_det; 
year_since = intck('days',DOJ,curr_date)/365.25; 
run; 

So the resultant table will be

Difference between two dates in days months and years in SAS 6

 

 

Difference between two dates in Quarter in SAS  – Method 1 : complete quarter

Difference between two dates in quarter is accomplished using INTCK function with ‘quarter’ as argument as shown below. It rounds off to the complete quarter i.e. if difference between two dates are 1.5 quarter it will round off to 2 quarter.

/* difference between two dates in quarter - returning complete quarter */ 
 
data emp_det1; 
set emp_det; 
quarter_since = intck('quarter',DOJ,curr_date); 
run; 

So the resultant table will be

Difference between two dates in days months and years in SAS 7

 

 

Difference between two dates in Quarter in SAS – Method 2

In Second method we can find difference between two dates in months and divide it by 3, so you will get difference in quarters. It is a roundabout way to calculate difference between two dates in quarters,  which will result in exact difference between two dates in terms of quarters in SAS


/* difference between two dates in quarter - returning exact quarter */ 
 
data emp_det1; 
set emp_det; 
quarter_exact = intck('months',DOJ,curr_date)/3; 
run;
 

So the resultant table will be

Difference between two dates in days months and years in SAS 8

 

                                                                                           

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts