Announcement

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

  • How to count the number of business days between two date (%td) variables?


    Hi,

    I want to count the number of business-days between two date (%td) variables.

    My data looks like this.


    Name request_date answer_date

    a 01mar2023 .
    b 13jan2022 25jan2022
    c 0dec2022 05jan2023
    d 02jul2022 10jul2022
    e 17feb2023 24feb2023
    f 03mar2021 .
    g 22may2021 .


    I created 'stbcal' format file putting purpose, date format, range, omit holidays.

    How can I use this 'stbcal' calendar file in my current STATA dataset, which has 'request date' and 'answer date' variables, to calculate the number of business days?



    Thank you!
    Last edited by Heejung Yun; 31 Jul 2023, 12:14.

  • #2
    HTML Code:
    https://stats.oarc.ucla.edu/stata/faq/stata-faq-how-can-i-count-the-number-of-days-between-two-dates/

    Comment


    • #3
      Thank you for the answer.
      However, if I just subtract two date variables, I still need to subtract, Saturday, Sunday, and holidays.
      How can I count business days not just count number of days between two?

      Comment


      • #4

        HTML Code:
        https://www.statalist.org/forums/forum/general-stata-discussion/general/645500-difference-in-trading-days-between-two-date-variables

        Comment


        • #5
          You have to convert the regular date variables to business-date variables. If you have already created your business calendar under the name my_calendar.stbcal:

          Code:
          gen request_bdate = bofd("my_calendar", request_date)
          gen answer_bdate = bofd("my_calendar", answer_date)
          
          format *_bdate %tbmy_calendar
          
          gen bdate_difference = answer_bdate - request_bdate
          I don't actually use business calendars myself in my work, so I may have the details slightly wrong here. But if you read -help datetime_business_calendars- these commands, an examples of how they are used, are all there.

          Added: By the way, you might want to add +1 to that -gen bdate_difference = ....- command, depending on whether you want to count both endpoints among the days "between two dates." That is, if the request was made today and the answer comes tomorrow, do you want to count that as 1 day's difference (leave code as is) or 2 (add +1).
          Last edited by Clyde Schechter; 31 Jul 2023, 15:05.

          Comment

          Working...
          X