Announcement

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

  • working with dates to generate age

    Hi i tried to generate an age variable by subtracting (datecycle2) from dob = date of birth
    I end up getting long numbers that don't make sense

    I suspect it's because perhaps by dob is not in the correct format?
    However i can't spot the mistake

    It automatically got generated in the right way why I imported excel file into stata - it was imported as a %td format as a integer rather than float
    meanwhile for the datecycle2 - this is a converted stata date which i did with the -date-command, which is correct.

    Not sure about the dob.

    What am I doing wrong?
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int dob float(datecyle2 age)
     7034 22719 15685
     8189 23261 15072
     5548 23222 17674
    10323 23232 12909
     9352 23248 13896
     2403 22384 19981
    -3435 22475 25910
     1050 23247 22197
    end
    format %td dob
    format %td datecyle2

  • #2
    It's just occurred to me the numbers for age are being given in days , just need to /365
    thanks anyway!

    Comment


    • #3
      Well, every four years there is a leap year, so if the people in question are adults, you will be somewhat off if you divide by 365. Dividing by 365.25 will be a bit better. Better still, if you are using a current version of Stata is to use the -age()- function which takes this into account exactly and even lets you specify what you want to consider as the birthday in non-leap years of somebody born on 29 Feb in a leap year. -help age()-. Or if you want the age including the fractional part, it's the -age_frac()- function.
      Last edited by Clyde Schechter; 02 Jun 2024, 19:08.

      Comment

      Working...
      X