Solved
Contributor
Posts: 46

# Months Difference between two dates with exact figures including decimals

Hi,

Is there a way to get the exact difference in months with decimals between two dates in SAS?

data want;

input

date1='01Jan2017'd;

date2='01Jan2018'd;

months1=(date2-date1)/(365.25/12);

run;

I have the above method, but not sure it's gives the accurate results...

Accepted Solutions
Solution
‎02-26-2018 09:24 AM
PROC Star
Posts: 1,471

All Replies
Contributor
Posts: 46

## Re: Months Difference between two dates with exact figures including decimals

Expecting the results as follows:

 Date1 Date2 Difference in months 28-Mar-08 20-Oct-05 29.25806452 20-Jun-11 11-Mar-09 27.29032258 24-Aug-06 23-Feb-06 6.032258065
PROC Star
Posts: 1,471

## Re: Months Difference between two dates with exact figures including decimals

How are you computing the expected difference in months?

I tried a few different guesses, but none of them matched what you are hoping for:

``````data want;
input date1 : date9. date2 : date9.;

days=date2-date1 ;

months1=(date2-date1)/(365.25/12);
months2=(date2-date1)/30.5 ;
months3=(date2-date1)/30 ;

format date1 date2 date9. ;

cards ;
20Oct2005 28Mar2008
11Mar2009 20Jun2011
23Feb2006 24Aug2006
;
run;``````

Returns:

```    date1        date2    days    months1    months2    months3

20OCT2005    28MAR2008     890    29.2402    29.1803    29.6667
11MAR2009    20JUN2011     831    27.3018    27.2459    27.7000
23FEB2006    24AUG2006     182     5.9795     5.9672     6.0667
```
Super User
Posts: 10,527

## Re: Months Difference between two dates with exact figures including decimals

No. There is no strict definition for the duration of a month, so it makes no sense to calculate fractions of it.

Your approximation is valid, but it's just that.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 9,796

## Re: Months Difference between two dates with exact figures including decimals

[ Edited ]

There really isn't any such thing as a fixed unit of month, so you can't really have parts of a month without an approximation when comparing days difference.  However in your case given, the number of months between is:

`((year(date2) - year(date1)) * 12) + (month(date2)-month(date1))`

So first take number of years difference and each one is 12, then add the difference in months.

Just hit post and then thought you could probably do the same far simpler using intnx('month',date1,date2);

Super User
Posts: 10,846

## Re: Months Difference between two dates with exact figures including decimals

``````data want;
input date1 : date9. date2 : date9.;

days=yrdif(date1,date2,'age')*12;
format date1 date2 date9. ;

cards ;
20Oct2005 28Mar2008
11Mar2009 20Jun2011
23Feb2006 24Aug2006
;
run;``````
Contributor
Posts: 46

## Re: Months Difference between two dates with exact figures including decimals

Thank you all!

Solution
‎02-26-2018 09:24 AM
PROC Star
Posts: 1,471