I am struggling with a good way to calculate an end date based on a start date (daily date) and a duration on a monthly base. It is a similar question as posted here: https://www.statalist.org/forums/for...-in-my-dataset But I struggle to get a good solution out of this:
My data looks like
My current way is as follows:
The last two paragraphs taking function lastdayofmonth(). It only works in cases when start date is the first day of a month but in cases like 30-Oct-15 or 18-Apr-17 it does not work because they should end 29th or 17th respectively.
So basically I would need to calculate the real number of days by months affected.
Is there another function I do not see or does someone has code automatically doing it and is willing to share?
My data looks like
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str9 start_date_s int duration "01-Oct-13" 66 "01-Nov-13" 66 "30-Oct-15" 62 "01-Apr-15" 48 "01-Sep-15" 57 "01-Dec-15" 66 "01-Nov-15" 60 "01-Nov-15" 48 "01-Feb-16" 41 "01-Jan-16" 45 "01-Jan-16" 48 "01-Jan-16" 36 "01-Dec-16" 54 "01-Jun-17" 69 "01-Jul-17" 72 "18-Apr-17" 24 "01-Jan-18" 70 "01-Apr-18" 66 "01-Apr-18" 24 "01-Jan-18" 69 end
Code:
**# start to end date gen start_date_d = date(start_date_s,"DM20Y") format start_date_d %td la var start_date_d "Start Date" gen start_date_m = mofd(start_date_d) format start_date_m %tm la var start_date_m "Start Date (month)" gen end_date_m = start_date_m + duration - 1 format end_date_m %tm la var end_date_m "End Date Month" **## enddate based on last day of end month gen end_date_d = lastdayofmonth(dofm(end_date_m)) format end_date_d %td la var end_date_d "End Date"
So basically I would need to calculate the real number of days by months affected.
Is there another function I do not see or does someone has code automatically doing it and is willing to share?
Comment