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...
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 |
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
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.
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);
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;
Thank you all!
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.