Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • End date by monthly duration given daily start date

    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
    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
    My current way is as follows:
    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"
    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?
    Last edited by Marc Kaulisch; 10 Nov 2022, 04:53.

  • #2
    I think I have found a solution:
    Code:
    gen end_date_m = start_date_m + duration - 1 if day(start_date_d) == 1
    replace end_date_m = start_date_m + duration if day(start_date_d) ~= 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))  if day(start_date_d) == 1
    replace end_date_d = (dofm(end_date_m)+ day(start_date_d) - 2) if day(start_date_d) ~= 1
    format end_date_d %td
    la var end_date_d "End Date"

    Comment


    • #3
      You said your problem was similar to that in the linked thread, but it doesn't appear to be identical. I start with the idea that you have a calculation in daily dates

      end date = start date + duration

      so how does month, or the end of the month, come into this?

      I am missing something, I fear, but if you go from one daily date to another, the months they are in are immediately evident and accessible.

      Comment


      • #4
        Originally posted by Nick Cox View Post
        so how does month, or the end of the month, come into this?
        The duration is measured in months. But the end date should be printed as daily date.

        Comment

        Working...
        X