Announcement

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

  • Generating age from date of birth and current date

    I wave a dataset of participants defined by a random id, collected in several waves. I have the date of birth and the wave time, and would like to generate the participant's age.

    clear
    input long Participant int MonthDayYearofParticipant long Month
    1107759 1735 201112
    1327808 -3532 201112
    1446079 4900 201112
    1547776 -1918 201112
    1728968 7487 201112
    1791894 8279 201112
    1964639 2039 201112
    1970861 8309 201112
    2092827 3257 201112
    2115337 882 201112
    2198333 5022 201112
    2569518 -2983 201112
    2576255 6453 201112
    2616888 2557 201112
    2702223 5204 201112
    2707228 9010 201112
    2714485 5783 201112
    2743220 8948 201112
    2776586 -2587 201112
    2956741 1552 201112
    2956746 -334 201112
    2956750 1035 201112
    2956757 -2375 201112
    2956760 2192 201112
    2956774 60 201112
    2956777 3712 201112
    2956778 -1248 201112
    2956780 5204 201112
    2956781 -883 201112
    2956783 -1036 201112
    2956785 -549 201112
    2956786 5114 201112
    2956788 -1614 201112
    2956796 -1370 201112
    2956801 1277 201112
    2956803 7305 201112
    2956808 -944 201112
    2956812 -1522 201112
    2956813 -487 201112
    2956815 -5205 201112
    2956817 3834 201112
    2956819 2373 201112
    2956821 1369 201112
    2956824 3561 201112
    2956825 -1309 201112
    2956828 -2801 201112
    2956837 -2953 201112
    2956839 -6635 201112
    2956846 4352 201112
    2956847 3104 201112
    2956852 4718 201112
    2956853 1035 201112
    2956854 517 201112
    2956855 1277 201112
    2956859 -1248 201112
    2956861 4383 201112
    2956863 6241 201112
    2956864 3834 201112
    2956867 -1706 201112
    2956870 -549 201112
    2956871 2373 201112
    2956874 -92 201112
    2956875 -3256 201112
    2956876 1947 201112
    2956877 6300 201112
    2956879 4169 201112
    2956881 425 201112
    2956883 60 201112
    2956884 -214 201112
    2956886 -457 201112
    2956887 -1340 201112
    2956889 -487 201112
    2956890 -2405 201112
    2956894 -5478 201112
    2956895 -4932 201112
    2956901 3013 201112
    2956903 -2770 201112
    2956904 5599 201112
    2956905 1521 201112
    2956909 -1736 201112
    2956910 -4414 201112
    2956911 91 201112
    2956915 3926 201112
    2956917 1369 201112
    2956919 6118 201112
    2956925 3439 201112
    2956926 1978 201112
    2956929 2070 201112
    2956931 -3440 201112
    2956933 4291 201112
    2956934 -5144 201112
    2959224 -3228 201112
    2959225 6391 201112
    2959227 152 201112
    2959228 2953 201112
    2959230 -4809 201112
    2959233 -1553 201112
    2959236 -3986 201112
    2959238 -2497 201112
    2962769 1461 201112
    end
    format %tddd-Mon-YY MonthDayYearofParticipant

  • #2
    this is what I did:

    gen month = mod(Month, 100)
    gen date = mdy(month, 1, year)
    format date %td
    gen age = date - MonthDayYearofParticipant
    gen age_years= round( (age/365) )

    Comment


    • #3
      The obstacle here is that Month variable which is pretty useless. It's a 6 digit number that a human eye, with some effort, can understand as a date, but is not an actual Stata date variable. The key is to convert it to a real Stata date variable. Then it's just a matter of applying Stata's -age()- function.
      Code:
      gen year = floor(Month/100)
      gen month = mod(Month, 100)
      gen wave_date = mdy(month, 1, year)
      format wave_date %tddd-Mon-YY
      drop Month year month
      
      gen age_at_wave = age(MonthDayYearofParticipant, wave_date)
      Note: As both the "Month" and birthdate variables are accurate only to the month, the calculated age may be off by 1 in some cases. There is no getting around this without exact day variables. The MonthDayYearofParticipant variable is a bit strange. From its name you would expect it to be accurate to the day. But, in fact, the day part is always the 1st day of the month, so it is really a monthly date variable disguised as a daily date variable. One has to calculate with it as if it were truly a daily date variable, but the results have accuracy only to the month level.
      Last edited by Clyde Schechter; 14 Jul 2023, 19:44.

      Comment

      Working...
      X