Announcement

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

  • Issues with dates

    I am trying to create two age variables for the age at the time of a diagnostic test.

    I have a variable BirthD (date of birth) (formatted as %td), and I have the variable Test_Date (date of the diagnostic test) (formatted as dd/mm/yyyy).

    The first age variable I am trying to create is age in years and the second age variable I am trying to create is the number of months lived at that time. I am trying to make it so both age variables are rounded down to an integer value (i.e. if someone was 23 months and 12 days at the time, the age in years would be 1, and the age in months would be 23).

    Any help with coding for this would be greatly appreciated - I seem to keep running into errors!

  • #2
    Sympathies but we need a data example to make this clear.

    "formatted as dd/mm/yyyy" could mean another numeric date variable or could mean a string variable.

    Only a data example could make this clear. Also, we can't possibly comment on errors in your code without seeing what you tried.

    Please read https://www.statalist.org/forums/help#stata and then show a data example using dataex.

    Comment


    • #3
      My apologies, the dd/mm/yyyy is a string variable.

      Thank you for clarification, first time poster here so apologies for not posting my code and data example. Will review the FAQ page.

      Comment


      • #4
        Here's a bit of code that both creates some artificial data, and does the job you need:

        Code:
        clear
        set obs 100
        
        gen BirthD = daily("08/22/2022","MDY") in 1
        format BirthD %td 
        replace BirthD = BirthD[_n-1] - _n*5 in 2/l
        
        gen Test_Date = "22/08/2022"
        gen Test_Date_num = daily(Test_Date,"DMY")
        format Test_Date_num %td
        
        gen age_years = datediff(BirthD, Test_Date_num, "year")
        gen age_months = datediff(BirthD, Test_Date_num, "month")
        Alternatively, you can use the age() function to get age_years.

        Comment


        • #5
          Originally posted by Hemanshu Kumar View Post
          Here's a bit of code that both creates some artificial data, and does the job you need:

          Code:
          clear
          set obs 100
          
          gen BirthD = daily("08/22/2022","MDY") in 1
          format BirthD %td
          replace BirthD = BirthD[_n-1] - _n*5 in 2/l
          
          gen Test_Date = "22/08/2022"
          gen Test_Date_num = daily(Test_Date,"DMY")
          format Test_Date_num %td
          
          gen age_years = datediff(BirthD, Test_Date_num, "year")
          gen age_months = datediff(BirthD, Test_Date_num, "month")
          Alternatively, you can use the age() function to get age_years.
          Hi there, when I try and use below code, I receive an error that says: unknown function datediff()
          gen age_months = datediff(BirthD, Test_Date_num, "month") Any guidance on this would be much appreciated

          Comment


          • #6
            Perhaps you're on an old version of Stata? I believe it was introduced in Stata 17.

            It also seems to be available in an up-to-date version of Stata 16.
            Last edited by Hemanshu Kumar; 23 Aug 2022, 07:16.

            Comment


            • #7
              Originally posted by Hemanshu Kumar View Post
              Perhaps you're on an old version of Stata? I believe it was introduced in Stata 17.
              Yes, sorry I am using 16.1 for Windows. Would you happen to know the appropriate coding for the older versions of Stata?

              Comment


              • #8
                I find datediff() available even on Stata 16.1 for Mac. I have revision 16 Jun 2022.

                You can check your Stata version and revision status by typing

                Code:
                about
                If yours is an older revision, just do

                Code:
                update all

                Comment


                • #9
                  version 16.1 was released on 2/18/2020 and the datediff() function was in the 11/5/2020 release; you can tell what release you have by using "query born"; more important, updates to later releases of 16.1 are free and you should do that; see
                  Code:
                  h update

                  Comment


                  • #10
                    Originally posted by Hemanshu Kumar View Post
                    I find datediff() available even on Stata 16.1 for Mac. I have revision 16 Jun 2022.

                    You can check your Stata version and revision status by typing

                    Code:
                    about
                    If yours is an older revision, just do

                    Code:
                    update all
                    I have Stata 16.1 for windows with a Revision 01 Sep 2020. I am unable to update my revision at present (require IT approval from place of work etc. etc.)

                    Apologies for this - do you have any idea of another way I could code for this without using datediff() ?

                    Comment


                    • #11
                      I was wondering if this would work or is this oversimplifying the code..?

                      Gen TestDateMonths = floor(((test_date - BirthD)/365.25)*12)

                      Comment


                      • #12
                        That can give you wrong answers occasionally. Perhaps use:

                        Code:
                        gen age_months = (year(Test_Date_num) - year(BirthD))*12 + (month(Test_Date_num) - month(BirthD)) - (day(Test_Date_num)<day(BirthD))

                        Comment


                        • #13
                          Just to spell out what is generally known, different lengths of month, including the occurrence of leap years, are the main complications here.

                          But I've seen 365.25 (evidently an average over a four year sequnce of 365 365 365 366, in any order) justified as

                          1. good enough for epidemiological or biostatistical purposes (which I guess is the context here)

                          2. what many researchers use any way.

                          The rules for leap years are different with years ending with 00 any way!

                          Comment


                          • #14
                            Consider birth dates of 20th, 21st, or 22nd February 2022, and a test date of 22nd August 2022. The correct age in months is 6 months old in each case.

                            Now, the difference in days is 183, 182 or 181, respectively. Dividing by 365.25 and multiplying by 12 gives us 6.0123205, 5.979466, and 5.9466119 respectively, which then makes Nicholas' formula give us an age of 6, 5, and 5 months, respectively. I would say the error is significant.

                            Comment


                            • #15
                              Naturally I agree with #14. My answer would be not to use months at all for statistical purposes, as you'd be rounding any way even if the example yielded some variations on 5.8 or 6.2 months.

                              I suspect the context is clinical where medics and patients, or rather parents, alike think in terms of (say) a baby being 6 months old, not 180 or so days old.

                              Comment

                              Working...
                              X