- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I have two dates and I need difference in months (exact).
I found inck function, but it always rounds result on whole months. I need output as decimal number, for example 8.214 months. How to do this? Is there any function ot his purpose?
I can calculate difference in days and divide it by (30 ? 31), but the result would not be exact and i do not treat this as clean solution.
Thank you for an answer
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi julo,
How do you want to calculate the decimal point in months. Is it no of days/30 0r no of days/31. An example would be helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
that's the question, I would say that there would be some ISO standard about this, but i don't know.... do you?
Now I solve it that I divide number of days by (365.25/12).
J.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I'm not aware of any standard way of doing this, and I'd be interested to understand why you want to see months in this way as I can't think off-hand of a reason for using this.
However, maybe something like the below will help. The variable month3 is a single-line version of the rest, which I left in to help make the calculation more clear. Presumably it could be tidied up a bit more...
data test;
format date1 date9.;
format date2 date9.;
date1 = '09FEB2011'd;
do i = -60 to 60 by 5;
date2 = intnx('day',date1,i);
output;
end;
run;
data test;
set test;
months = intck('month',date1,date2);
temp = intnx('month',date1,months,'sameday');
days_shift = intck('day',temp,date2);
days_in_month = day(intnx('month',date2,0,'end'));
month2 = months + days_shift/days_in_month;
month3 = intck('month',date1,date2) + intck('day', intnx('month',date1,intck('month',date1,date2),'sameday'),date2) / day(intnx('month',date2,0,'end'));
run;
To look at an example then (UK dates):
Date1: 09/02/2011
Date2: 06/03/2011
Using intck will say there is 1 month interval between the two, so Month = 1;
Temp is set to Date1 + Month, but to the same day, hence Temp = 09/03/2011.
There are -3 days between Temp and Date2, hence Days_Shift = -3.
There are 31 days in March, therefore Days_in_Month = 31.
Adding this together then says 1 full month, minus 3 days (which we take as the fraction 3 / 31), which is 28 / 31 or 0.9032 months.
The fraction element is determined by the number of days in the month of Date2, and it should work regardless as to which date is greater.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The reason why is specification which I have to implement. There are intervals in months.
Thanx for example.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Julo
What is the exact number of months between 1-Jan and 31-Jan ?
What is an exact difference in months between two dates which fall on different days in the month?
Will it be fractional?
What should the fraction be when the startmonth has a different number of days from the finishing month?
sounds like an incomplete spec
best of luck
peterC
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Bad specification. You should argue against it.
0.5 month in February is 14 days typically.
0.5 of December is 15.5 days. 1.5 days/30 days = 5% error introduced for no reason.
Use 365.25/12 = 30.4375 days as a month and calculate the difference as days and then divide by 30.4375 to get a slightly better definition.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Guys;
please note that the question was asked about 3 years back and probably he might have fixed it and who knows.
Let's wait for his response to find what step he took. What do you say?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Out of curiosity why did you restart a 3 year old thread?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Exactly, just out of curiosity:) .
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Try this:
enddate1=intnx('month',date1,0,'end'); /*end date of date1 ? 30/31/28/29*/
enddate2=intnx('month',date2,0,'end');
format enddate1 enddate2 date9.; /*ddmmmyyyy*/
numdays1=day(enddate1); /*number of max days in that month*/
numdays2=day(enddate2);
Months = (datdif (date1,enddate1, 'act/act')/numdays1 ) + intck('month',date1,date2) + (day(date2)/numdays2) -1; /* due to the limitation of intck function a -1 is adjusted*/
run;
/*date1- date2*/
/*date1month+ date2month +and all months in between*/
Example:
5feb2014 - 17may2014
5feb-28feb/28 + 2+17may-1may/31
Cheers!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data _null_;
n=yrdif('02jan2013'd,'12aug2013'd,'act/act')*12 ;
put n= ;
run;
Xia Keshan