Announcement

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

  • Counting the number of days between two dates, excluding specific days of the week

    Hi,
    I need to calculate the number of elapsed days between two dates (visitdate and new_apptdate) for each observation excluding certain days of the week. Here is what the data look like:
    +-----------------------------------------------+
    | id visitdate new_app~e daysa~pt |
    |-----------------------------------------------|
    40. | 00542KPP-4 25nov2016 25nov2016 0 |
    41. | 00542KPP-4 17feb2017 17feb2017 0 |
    42. | 00542KPP-4 12may2017 12may2017 0 |
    43. | 00542KPP-4 10jul2017 07jul2017 3 |
    44. | 00542KPP-4 10oct2017 02oct2017 8 |
    |-----------------------------------------------|
    45. | 00542KPP-4 17jan2018 21nov2017 57 |
    46. | 00542KPP-4 13feb2018 14feb2018 -1 |
    47. | 00751KLM-1 25may2016 . . |
    48. | 00751KLM-1 31may2016 31may2016 0 |
    49. | 00751KLM-1 28jun2016 28jun2016 0 |
    |-----------------------------------------------|
    50. | 00751KLM-1 26jul2016 26jul2016 0 |
    +-----------------------------------------------+


    The workdays package only excludes weekends and holidays - I need to also exclude Fridays, and for a subset of observations, Thursdays as well.
    Thanks for any help you can provide.


  • #2
    No specific solution, but it sounds like you may want to investigate the use of business calendars, which were made for this purpose.

    See:
    1) the output of -help datetime_business_calendars-
    2) https://blog.stata.com/2016/02/04/ha...ess-calendars/

    Comment


    • #3
      Perhaps something like this:

      Code:
      clear
      input str10 date1 str10 date2
      3apr2020 16apr2021
      3apr2020 15apr2021
      4apr2020 16apr2021
      1apr2020 15apr2021
      end
      
      gen mydate1=date(date1, "DMY")
      gen mydate2=date(date2, "DMY")
      format mydate* %td
      drop date*
      gen length = mydate2 - mydate1+1
      gen number_of_weeks =floor(length/7)
      gen remainder = mod(length, 7) 
      //DOW starts at 0 = Sunday
      gen dow_start= word(c(Weekdays), dow(mydate1)+1)
      gen dow_end= word(c(Weekdays), dow(mydate2)+1)
      gen last_dow = word(c(Weekdays), dow(mydate1+7))
      
      gen number_of_fridays = number_of_weeks /// 
          +  cond(dow(mydate2)+1 -dow(mydate1+7)>0 /// 
          & dow(mydate2)>4 , 1,0)
      list, ab(15)

      Comment

      Working...
      X