Announcement

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

  • Calculate date of birth from current date and age (in years, months and days)

    Dear

    I need help to calculate the date of birth from the age of people. I know the exact year, months and days that a person has until today (date_today). But, I need the dates of birth. I searched and there are several examples of how to calculate the age from the date of birth and a date of interest, but I did not find a reverse example (exact date of birth (year, day and month) from the exact age (age, months and days to current date).

    Please, I appreciate any guidance.
    the data variables are:

    id: individual identifier
    age_years: age in years to the present day
    age_months: age in months to the present day
    age_days: until today
    date_today: reference date

    A person who is 10 years, 0 months and 7 days old, means that he/she was born on February 12, 2015.
    I sincerely hope you can help me

    Best regards

    Moses


    copy starting from the next line -------------- --------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float id int age_years byte(age_months age_days) float date_today
    1   9  3 27 23791
    2  56 11 25 23791
    3  55  3 20 23791
    4  60  4 26 23791
    5  59  8  0 23791
    6  49  7  2 23791
    7  50  0  3 23791
    8  60  3 27 23791
    9  63  8 11 23791
    10  62  5 27 23791
    11  49  4 12 23791
    12  50  8  4 23791
    13  65  9 28 23791
    14  62  4 14 23791
    15  61  1 15 23791
    16  53  2 28 23791
    17  56  3 24 23791
    18  64  2  4 23791
    19  60  4 21 23791
    20  62  5 16 23791
    21  43  5  5 23791
    22  56 11 10 23791
    23  61  4 10 23791
    24  60  5 14 23791
    25  44 11  7 23791
    26  52  0 13 23791
    27  60  5 26 23791
    28  61  2 13 23791
    29  60  7  5 23791
    30  59 11 27 23791
    31  48 11 11 23791
    32  45  8 12 23791
    33  60  2 15 23791
    34  58  1 18 23791
    35  61  4 14 23791
    36  61  8  9 23791
    37  48 11 22 23791
    38  57  6  6 23791
    39  58  9 19 23791
    40  52  7 20 23791
    41  54  7 28 23791
    42  60  4 19 23791
    43  61  3  3 23791
    44  60  0 30 23791
    45  60  0 23 23791
    46  62  7 20 23791
    47  58  7 13 23791
    48  61  1 16 23791
    49  64  1 23 23791
    50  57  9  3 23791
    51  59  0 29 23791
    52  76 11  7 23791
    53  62  0  9 23791
    54  59  7 17 23791
    55  50  3 29 23791
    56  49  3 29 23791
    57  57  4 10 23791
    58  55  8  8 23791
    59  60 10  2 23791
    60 103  8 27 23791
    61  61 11 28 23791
    62  61  3 29 23791
    63  47  1  0 23791
    64  57  3 15 23791
    65  54  3 11 23791
    66  61 10 26 23791
    67  56  6  6 23791
    68  50  5  4 23791
    69  53  4 13 23791
    70  62  8 27 23791
    71  60  9 20 23791
    72  45  1 17 23791
    73  48  4 28 23791
    74  60  9 24 23791
    75  60  3 28 23791
    76  57  6 17 23791
    77  61 10 17 23791
    78  56  7 13 23791
    79  58 11  7 23791
    80  17  0 18 23791
    81  58 10 14 23791
    82  61  1  8 23791
    83  44  9 10 23791
    84  51  1  8 23791
    85  51  9 28 23791
    86  70  4 26 23791
    87  58  9 30 23791
    88  53  2 15 23791
    89  43  5 13 23791
    90  60  7 14 23791
    91  60  0 14 23791
    92  52  0  6 23791
    93  61  5 15 23791
    94  59 10  1 23791
    95  57  5  3 23791
    96  48  3  2 23791
    97  59  4  1 23791
    98  59 11 15 23791
    99  45 10 18 23791
    100  57 11 14 23791
    end
    format %td date_today
    copy up to and including the previous line --------- --------

  • #2
    There is a bundle of relevant functions in Stata 18, which is presumed to be what you're using unless you state otherwise.

    If you are using some other version, please tell us what it is, so that people can try to gauge what is accessible to you.

    Comment


    • #3
      Thank you Nick
      I am currently using stata 16 and have tried to turn it around by generating variables but got lost in the attempt.
      I appreciate any guidance

      Comment


      • #4
        This might be one way to do it:

        Code:
        clear
        input float id int age_years byte(age_months age_days) float date_today
        1 0 0 1 23791
        2 2 1 0 23791
        3 10 0 7 23791
        end
        format %td date_today
        
        gen int age_in_months = age_years * 12 + age_months
        gen double mm = dofm(mofd(date_today) - age_in_months)
        gen double this_day_that_month = mdy(month(mm), day(date_today), year(mm))
        gen double dob = this_day_that_month - age_days
        drop age_in_months mm this_day_that_month
        format %td dob
        which produces:

        Code:
        . list, noobs abbrev(20)
        
          +-----------------------------------------------------------------+
          | id   age_years   age_months   age_days   date_today         dob |
          |-----------------------------------------------------------------|
          |  1           0            0          1    19feb2025   18feb2025 |
          |  2           2            1          0    19feb2025   19jan2023 |
          |  3          10            0          7    19feb2025   12feb2015 |
          +-----------------------------------------------------------------+
        Last edited by Hemanshu Kumar; 19 Feb 2025, 14:54.

        Comment


        • #5
          Dear Hemanshu
          Thank you very much for the suggestion, I'll see how it works!!!

          Comment


          • #6
            Dear Hemanshu,

            To test the suggestion I used it in a database in which I know the date of birth.
            The results obtained vary by a few days (1, 2, 3 or more) in some cases.
            The older the person is the more difference of days there is.
            I think it must be associated with leap years (February with 29 days).
            Even in some cases, when the date is close to the change of month it also generates some differences.
            However, since there is no other way, working with a difference of a few days seems not so problematic.
            Thank you very much for your help

            Comment


            • #7
              That's strange, I thought the calculation would be exact. Could you show me a data extract that includes some of the problem cases? (please include the known date of birth)

              Comment


              • #8
                I recalculated the difference and it is 2 to 3 days. As I mentioned it is not so problematic for those who stay in the same month but in some cases it changes month.
                birth_original is the variable with the actual date (new database).




                copy starting from the next line --------------------- -
                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input int age_years byte(age_months age_days) float(date_today birth_original)    int    dob
                56 11 25 23791  2977  2975
                55  3 20 23791  3592  3590
                59  8  0 23791  1998  1996
                60  3 27 23791  1759  1757
                63  8 11 23791   526   524
                49  4 12 23791  5761  5758
                50  8  4 23791  5281  5279
                62  4 14 23791  1011  1008
                53  2 28 23791  4345  4342
                56  3 24 23791  3223  3221
                64  2  4 23791   352   349
                60  4 21 23791  1735  1732
                62  5 16 23791   978   976
                56 11 10 23791  2993  2990
                61  4 10 23791  1380  1377
                60  5 14 23791  1711  1709
                44 11  7 23791  7379  7376
                61  2 13 23791  1438  1435
                60  7  5 23791  1659  1656
                59 11 27 23791  1880  1877
                61  4 14 23791  1376  1373
                61  8  9 23791  1258  1256
                57  6  6 23791  2784  2781
                58  9 19 23791  2314  2311
                61  3  3 23791  1417  1415
                62  7 20 23791   912   910
                76 11  7 23791 -4309 -4312
                59  7 17 23791  2012  2009
                50  3 29 23791  5409  5407
                49  3 29 23791  5774  5772
                60 10  2 23791  1570  1568
                61 11 28 23791  1148  1145
                61  3 29 23791  1391  1389
                57  3 15 23791  2866  2864
                54  3 11 23791  3966  3964
                61 10 26 23791  1180  1178
                56  6  6 23791  3150  3147
                50  5  4 23791  5373  5371
                53  4 13 23791  4299  4296
                60  9 20 23791  1583  1580
                60  9 24 23791  1579  1576
                57  6 17 23791  2773  2770
                56  7 13 23791  3112  3109
                58 11  7 23791  2265  2262
                51  9 28 23791  4862  4859
                70  4 26 23791 -1923 -1926
                58  9 30 23791  2303  2300
                53  2 15 23791  4358  4355
                43  5 13 23791  7921  7919
                61  5 15 23791  1344  1342
                59 10  1 23791  1936  1934
                59  4  1 23791  2120  2117
                59 11 15 23791  1892  1889
                57 11 14 23791  2623  2620
                48  7 19 23791  6028  6025
                53  6  5 23791  4246  4243
                58  6  3 23791  2422  2419
                58  8  2 23791  2361  2359
                52  2 25 23791  4714  4711
                53  9 29 23791  4130  4127
                58  8 15 23791  2348  2346
                50  7 18 23791  5298  5295
                60  2  6 23791  1811  1808
                58 10 22 23791  2280  2278
                62 10 29 23791   812   810
                60 11 14 23791  1528  1525
                60  8  5 23791  1628  1626
                58  7 23 23791  2371  2368
                58  7 18 23791  2376  2373
                60  4 24 23791  1732  1729
                58  9 27 23791  2306  2303
                53  7 20 23791  4200  4197
                60  9 24 23791  1579  1576
                57  9 12 23791  2686  2683
                60 10  8 23791  1564  1562
                53 10 15 23791  4113  4111
                60  6 27 23791  1668  1665
                51  5 22 23791  4990  4988
                59  6 12 23791  2048  2045
                55  8  3 23791  3455  3454
                57  6 16 23791  2774  2771
                60  4 11 23791  1745  1742
                51  7 12 23791  4939  4936
                59  3 27 23791  2124  2122
                60  3 23 23791  1763  1761
                59  6  9 23791  2051  2048
                58  4 29 23791  2457  2454
                60  9 24 23791  1579  1576
                57  4 25 23791  2826  2823
                55  4  0 23791  3582  3579
                58  2  1 23791  2546  2543
                59  6 21 23791  2039  2036
                59  9 20 23791  1948  1945
                61  5  9 23791  1350  1348
                62  2 21 23791  1065  1062
                59  9  2 23791  1966  1963
                59  8  0 23791  1998  1996
                61  9  1 23791  1236  1233
                58  2 29 23791  2518  2515
                60  3  9 23791  1777  1775
                end
                format %td date_today
                format %td birth_original
                format %td dob
                copy up to and including the previous line ---------------- -


















                Comment


                • #9
                  I don't think this is an error with the way I am constructing the date of birth. There seems to be some issue with your original data. Take the third observation, for example, who is 59 years, 8 months, and 0 days old. If today's date is 19 February 2025, clearly they must have been born on the 19th (since they are exactly 59 years and 8 months old). But as per your birth_original, they were born on 21 June 1965.

                  Comment


                  • #10
                    You are right. I will check the data.
                    Thank you very much.

                    Comment

                    Working...
                    X