Desktop productivity for business analysts and programmers

Months Difference between two dates with exact figures including decimals

Accepted Solution Solved
Reply
Contributor
Posts: 46
Accepted Solution

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

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

Posted in reply to Data_User
@Data_User you are welcome. If your question has been answered, please mark one of the answers as an accepted solution.

View solution in original post


All Replies
Contributor
Posts: 46

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

Posted in reply to Data_User

Expecting the results as follows:

 

Date1Date2Difference in months
28-Mar-0820-Oct-0529.25806452
20-Jun-1111-Mar-0927.29032258
24-Aug-0623-Feb-066.032258065
PROC Star
Posts: 1,471

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

Posted in reply to Data_User

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

Posted in reply to Data_User

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
Super User
Posts: 9,796

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

[ Edited ]
Posted in reply to Data_User

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

Posted in reply to Data_User
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

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

Posted in reply to Data_User
@Data_User you are welcome. If your question has been answered, please mark one of the answers as an accepted solution.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 224 views
  • 4 likes
  • 5 in conversation