Announcement

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

  • Calculating months between two dates

    Hello,

    I searched through this forum but didn't find a topic that was exactly similar to mine, so I figured it was worth creating a new thread. I am trying to calculate the number of calendar months between two dates, i.e., the number of calendar months between 10 Aug 2014 and 15 Jul 2012. The reason I need to use calendar months is because I am trying to replicate the programming used by the immunization registry in my state to determine time between vaccination doses and when the next dose is due. Is there a way to do this in Stata or am I limited to defining a month as 365/12=30.41666666667 days?

    Many thanks in advance for your help!
    Megha

  • #2
    Well, if you want to replicate what your state's immunization registry does, you should find out how they do it! Whatever they do can, I'm sure, be replicated in Stata.

    The main issue is how fractional month differences are handled. So, if one date is 10 Jul 2017 and the other is 20 Jul 2017, that is a difference of 10 days. Is that to be recorded as 0 months, 1 month, or 0.33 months? How about 31 Jul 2017 and 1 Aug 2017. Those are one day apart, but they are in different calendar months. Is that 0 months or 1 month?

    Comment


    • #3
      Thanks so much for your reply! I actually did email my state's immunization registry, and I am waiting to hear back from them. To answer your question, I am guessing for now that fractional months would make the most sense (i.e., difference between 10 Jul 2017 and 20 Jul 2017 should be 0.33 months).

      Comment


      • #4
        In that case, it's simply:

        Code:
        gen months_difference = (date1 - date2)/(365/12)
        This assumes that the variables date1 and date2 are Stata internal format numerical date variables. If what you have are string variables that look like dates to human eyes, then you have to convert them using the daily() function. See -help daily()- if this is unfamiliar. If, worst of all worlds, you have numeric variables with values like 20170720, then it is more difficult and you have your work cut out for you: post back.

        Comment


        • #5
          depending on what you want, and whether there are any leap years involved, Clyde's response might not be sufficient; for an involved issue I once had, you might want to take a look at Robert Picard's answer to my question in #5 of https://www.statalist.org/forums/for...ounting-months

          Comment


          • #6
            Thank you both for the help, I appreciate it.

            Comment

            Working...
            X