Return to Homepage

Goto the Tip of the Month Archive

Other interesting pages ...
LinkedIn Profile
SAS Cheat Sheet
Useful SAS Code
Full SAS Example
Basic Statistics
Contact Information

SAS Tip of the Month
July 2012
(for SAS and WPS)

Counting the number of COMPLETE months between two dates can be easily calculated using the following code:

   months = intck('month',date1,date2) - (day(date2) < day(date1))

The INTCK function counts the number of months between DATE1 and DATE2 using the MONTH value in the first parameter of the INTCK call. However, this can still cause a problem if the day number of the first date is after the day number of the second date which means a complete calendar month has not happened -- in order for this situation to resolve correctly, a check of this is made in the second part of the equation resulting in a '1' for TRUE and '0' for FALSE. See below for and example that shows this code in use:

   data _null_;
      infile cards;
      input @1 date1 date9. @11 date2 date9.;
      months = intck('month',date1,date2) - (day(date2) < day(date1));
      put date1= date9. date2= date9. months=;
   cards;
   14APR2012 13MAY2012
   14APR2012 14MAY2012
   14APR2012 15MAY2012
   ;
   run;

   date1=14APR2012 date2=13MAY2012 months=0
   date1=14APR2012 date2=14MAY2012 months=1
   date1=14APR2012 date2=15MAY2012 months=1

Try the code yourself and see how it works, and also see what happens if you take out the "(day(date2) < day(date1))", or swap the two dates.

See you next month.

________________________________
Updated July 7, 2012