Announcement

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

  • Age in Months to the exact day?

    Hi everyone,

    I'm looking to find the exact age of survey participants in months. Right now I'm subtracting the date the participant was born from the date the survey took place with the code

    Code:
     gen ageinmonths= (CB_DOIW - participant_DOB)/(365.25/12)
    both CB_DOIW and participant_DOB are in %td format.

    This gives the wrong results. Participants who did the survey on their birthday, ie:

    CB_DOIW = 19/3/2021
    participant_DOB = 19/3/2013

    should have their ageinmonths be equal to 96, instead the above code gives .6668489 as an answer. I'm unsure of what I'm doing wrong here.

    To be clear, I need the ageinmonths to be exact, ie,

    if CB_DOIW = 19/3/2021 and participant_DOB = 18/3/2013, I should get a value of 96.032, or if the month was April

    CB_DOIW = 19/4/2021
    participant_DOB = 18/4/2013

    ageinmonths should be 96.033
    Last edited by Joel Ng; 07 Apr 2021, 21:33.

  • #2
    Well, I can't reproduce your error:
    Code:
    . clear*
    
    . set obs 1
    number of observations (_N) was 0, now 1
    
    . gen CB_DOIW = td(19mar2021)
    
    . gen participant_DOB = td(19mar2013)
    
    . display (CB_DOIW - participant_DOB)/(365.25/12)
    96
    That said, I don't even know what it means to speak of an exact age in months given that months have different numbers of days and are therefore not a true constant unit of time. Putting that aside, it is also just an approximation to say that a year is 365.25 days. In fact, no calendar year is that length. Most are 365, and some are 366. And depending on the number of years between the dates being compared, the average of 365.25 is a better or worse approximation to the truth. However, you can get around that using Stata's age_frac() function, which actually takes exact dates and actual numbers of days in each year into account.

    Code:
    gen age_in_months = 12*age_frac(participant_DOB, CB_DOIW)

    Comment


    • #3
      Just use -datediff_frac-

      Code:
      clear
      input ye1 mo1 da1 ye2 mo2 da2
      2013 3 19 2021 3 21
      end
      
      gen dob = mdy(mo1, da1, ye1),
      gen date1 = mdy(mo2, da2, ye2)
      format dob %td
      format date1 %td
      
      **** Differece in months ****
      gen agemos = datediff_frac(dob, date1, "month")
      list
      Results:
      Code:
           +------------------------------------------------------------------------+
           |  ye1   mo1   da1    ye2   mo2   da2         dob       date1     agemos |
           |------------------------------------------------------------------------|
        1. | 2013     3    19   2021     3    21   19mar2013   21mar2021   96.06451 |
           +------------------------------------------------------------------------+
      But if you would like that kind of precision, it's easier just to use number of days.

      Comment

      Working...
      X