Announcement

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

  • Find no. of months b/w two dates and multiply them by their monthly targets for that year.

    Hello!

    I have two dates, 01jan2020 and 31aug2021. Similarly, I have two variables for targets, tar_1 and tar_2, both indicating the number of sells need to be made in 2020 and 2021, respectively. How do I calculate the number of months from 01jan2020 - 31dec2020, multiply it by the monthly target for 2020, do the same for the other date using tar_2, and then sum the two to obtain the target needed to be achieved between 01jan2020 and 31aug2021? This is my first time using this forum, so apologies if I have phrased my query weirdly.

    Just for reference, I have a multiple pairs of dates. If I can figure out how to find the months between two dates, I will be able to perform the rest of the task using a loop. Thanks!
    Last edited by Ubaidullah Jamal; 26 Jan 2023, 09:30.

  • #2
    There were 12 months in 2020. You don't need to calculate that in Stata. Otherwise, given two daily dates, you can calculate the difference in months as the difference between

    Code:
    mofd(date1)
    and

    Code:
    mofd(date2)
    and add 1 according to taste. That is, either Jan 2023 minus Jan 2023 is 0 or it is 1 for your purposes.

    Comment


    • #3
      Also, given two daily dates, d1, and d2, you can calculate the difference between them in months with -datediff(d1, d2, "m")-.

      Comment


      • #4
        Originally posted by Nick Cox View Post
        There were 12 months in 2020. You don't need to calculate that in Stata. Otherwise, given two daily dates, you can calculate the difference in months as the difference between

        Code:
        mofd(date1)
        and

        Code:
        mofd(date2)
        and add 1 according to taste. That is, either Jan 2023 minus Jan 2023 is 0 or it is 1 for your purposes.
        Apologies, I made a rather stupid error. I meant the difference in months between 01jan2020 & 31aug2022.

        Comment

        Working...
        X