Announcement

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

  • Converting date of birth to age

    I have a date of birth variable called "EL_DOB" in the format "15 Jan 81" and I am trying to convert it to an "age" variable, where the timestamp is Jan 1 2006. I don't actually have a timestamp variable, so I think I need to generate that first, but can anyone help me do that, and also convert the DOB to age? Thanks!

  • #2
    There is no need to create a timestamp "variable" that will only take on one value. In fact, inn over 20 years of Stata use, I have yet to encounter any situation where it is necessary, or even useful, to create a "variable" whose value is a single constant. When you're thinking of doing that, you can be certain you are on the wrong track.

    You need to convert EL_DOB from a string variable that looks like a date to a numeric date variable. (If it already is a numeric date variable with a display format that allows it to display as a date, then you can skip that part. You can tell the difference by running -des EL_DOB-. If the output shows %str... in the display format column, it's a string. If it shows %td... it's already a numeric date.)

    Code:
    // CONVERT EL_DOB TO A STATA NUMERIC DATE VARIABLE
    gen int date_of_birth = daily(EL_DOB, "DMY", 2005)
    format date_of_birth %td
    
    // NOW CALCULATE AGE IN YEARS
    gen age = (td(1jan2006) - date_of_birth)/365.25
    This will give you age including fractions of years. If you would prefer for age to be age at previous birthday in whole numbers, -replace age = floor(age)-.

    Note: EL_DOB apparently gives only two digits for the year. This is potentially problematic: does year 2012 denote somebody born in 1912 or 2012: people of both ages are still alive today. Since you are working with a reference date of Jan 1, 2006, I made the assumption that nobody in your data set was born after 12/31/2005. That is why 2005 appears as the third parameter in the -daily()- function call. It means that if the year part of EL_DOB is 06 or greater, it will be assumed to be 19whatever, not 20whatever, but 00-05 will be taken to be 2000-2005.

    You should read the manual sections relating to date and time variables and date and time functions. But the above code will get you past your present obstacle.

    Comment


    • #3
      See also personage (SSC). The name should perhaps have been person_age.

      Code:
      clear
      mat values = (28, 19, 28, 29, 29\3, 11, 2, 2, 2\1952, 1952, 1999, 2000, 2004)
      set obs `=colsof(values)'
      gen bdate = mdy(values[2, _n], values[1, _n], values[3, _n])
      format bdate %td
      
      personage bdate, currdate(mdy(1,1,2006)) gen(age days)
      list
      
           +------------------------+
           |     bdate   age   days |
           |------------------------|
        1. | 28mar1952    53    279 |
        2. | 19nov1952    53     43 |
        3. | 28feb1999     6    307 |
        4. | 29feb2000     5    307 |
        5. | 29feb2004     1    307 |
           +------------------------+
      Following Clyde's dictum, "ages" or other durations can be calculated relative to some fixed date with this command (and also with variable dates, not documented here).

      However, I'll note that I've found need for, and use of, variables containing constants for graphical purposes for creating lines, bar or spike ends and the like for fixed horizontal or vertical values.

      Comment


      • #4
        if i have two datasets data1 and data2 entered by two different data staff, what stata program can compare the two to see the discrepancies in the data?

        Comment


        • #5
          Maarten Buis showed another way to get age at previous birthday in whole numbers in another forum: Note that it does not use division by 365.26.
          --
          Bruce Weaver
          Email: [email protected]
          Version: Stata/MP 18.5 (Windows)

          Comment


          • #6
            Originally posted by Abayomi Olufemi View Post
            if i have two datasets data1 and data2 entered by two different data staff, what stata program can compare the two to see the discrepancies in the data?
            Abayomi, please start a new topic with an appropriate title. Many members will not see your question when it is buried in a thread about a different topic.
            --
            Bruce Weaver
            Email: [email protected]
            Version: Stata/MP 18.5 (Windows)

            Comment


            • #7
              I want to calculate age from "dob" which is the date of birth, & "visiting_date" which is different through out the data set. (ik how to do it when we are calculating age from the present date but here the visiting date is different for each person), any help would be much appreciated, TIA (:.

              Comment


              • #8
                The age() function introduced in Stata within the life of Stata 16 takes two daily dates and returns the age in years.

                Code:
                . di age(mdy(3,28,1994), mdy(12,21,2023))
                29
                The arguments can be variables too:

                Code:
                gen age = age(dob, visiting_date)

                Comment


                • #9
                  Nick Cox

                  Hey thanks, that was very helpful.

                  Comment

                  Working...
                  X