Announcement

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

  • Need to calculate age (in years)

    Hi All,

    I hope I explain this clearly and with enough information, please let me know if I can provide more information. I need to calculate age (in years) for summary statistics. I have the 'dob' variable in a format such as 08/13/1950 and the 'medicationscantime' variable as 11/25/2000 8:45:00PM. How do I subtract 'medicationscantime' from 'dob' to get age in years? I have STATA 15.1

  • #2
    For this you can use the Stata date functions, consult the manual:
    Code:
    help datetime_functions
    Some silly example:

    Code:
    clear
    input str10 dobstr
    "9/18/1752"
    "4/7/1761"
    "4/30/1777"
    "2/18/1871"
    "6/13/1876"
    "2/17/1890"
    "4/16/1894"
    "8/11/1895"
    "6/16/1915"
    "7/15/1924"
    "8/10/1924"
    "8/7/1928"
    "5/24/1938"
    "12/22/1943"
    end
    
    // dob into a Stata date
    gen dob = date(dobstr,"MDY")
    format dob %td
    label variable dob "Date of birth"
    
    // compute age on 10/08/2014
    gen age = 2014 - year(dob)
    sum age
    
    // subtract 1 if did not have his/her birthday yet
    if month(dob) == 10 {
        if day(dob) > 8 {
            replace age = age - 1 
        }
    }
    if month(dob) > 10 {
        replace age = age - 1 
    }
    But, possibly, an even more convenient option is to install the Stata package personage, contributed by N.J. Cox, which you can install in this way:
    Code:
    ssc install personage , replace
    h personage
    The help file provides an example that you can follow for your own work.

    http://publicationslist.org/eric.melse

    Comment


    • #3
      Stata 16.1 introduced dedicated age-related functions. That is a signal to those interested in this question able to use them.

      In view of Eric Melse's generous recommendation, it seems ungracious to point out that -- perhaps through over-exposure to certain other software -- his last few statements need rewriting because the if command and the if qualifier are only rarely equivalent.

      You shouldn't try to subtract a birth date (in days) from a date-time (in milliseconds) without correcting for units. Despite all the detail of leap years and so forth, people who deal in medical statistics seem typically to assert that something like gives you enough precision in practice:


      Code:
      .  di (dofc(clock("11/25/2000 8:45:00PM", "MDY hms")) - daily("08/13/1950", "MDY"))/ 365.25
      50.286105
      
      .  di floor((dofc(clock("11/25/2000 8:45:00PM", "MDY hms")) - daily("08/13/1950", "MDY"))/ 365.25)
      50

      Comment


      • #4
        Novice Stat Please note our request at https://www.statalist.org/forums/help#realnames to use a full real name.

        Comment


        • #5
          Nick Cox that didn't seem to work - was a variable supposed to be generated? My "'medicationscantime' variable as 11/25/2000 8:45:00PM" is IN str22 format and thank you, I will change my name

          Comment


          • #6
            I showed you self-contained code that works for the specific examples you gave and I used display (di).

            If you copy the command lines one by one into Stata (the lines starting with periods, stops or dots) you should see the numeric output.

            That code does not do anything with your variables because it doesn't even refer to them.

            If you like the approach then something like

            Code:
             
             gen wanted = floor(dofc(medicationscantime)- dob)/ 365.25)
            should work if and only if those variables are respectively a Stata datetime variable and a Stata daily date variable.

            The word "format" is ambiguous unless we can see a data example using dataex as requested. If it means that your time and date data are string variables, then as in my code -- and as underlined by Eric in #2 -- you need to use datetime functions to convert, but my reply already showed that clock() and daily() will do the job.

            I hope that helps.

            Comment


            • #7
              SOLVED: exported data sheet to excel. used the 'text to colums' feature to separate the 'medicationscantime' variable from 11/25/2000 8:45:00PM to two different columns (one column had 11/25/2000 and the other column had 8:45:00PM" and I did it in the date format. Then I imported to STATA and used this code

              .gen mage=medicationscantime-dob
              .gen age=floor(mage/365.25)

              thanks All!

              Comment


              • #8
                I suspect #7 crossed with #6 showing that this is one line in Stata without any need to use anything else.

                Comment

                Working...
                X