Announcement

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

  • I want to calculate the age of the baby.

    Sir I want to calculated the age of baby from these 4 variables.
    please tell how can i calculate age using these 4 variables in stata.
    here I am writing an example.

    Date of birth- DOB
    Time of birth- TOB
    Date of admission - DOA
    Time of admission -TOA

    DOB TOB DOA TOA
    20-NOV-2019 15:10 23-NOV-2019 10:12
    23-NOV-2019 13:10 25-NOV-2019 21:35
    26-NOV-2019 11:42 26-NOV-2019 12:40
    26-NOV-2019 11:46 26-NOV-2019 12:40
    here i am also attaching CSV file.
    Attached Files

  • #2
    to get, e.g., age in hours,
    Code:
    egen a = concat(doa toa)
    egen b = concat(dob tob)
    
    gen double age = (clock(a,"DMYhm") - clock(b,"DMYhm")) / 3600000

    Comment


    • #3
      Thank you sir
      one more thing how can we convert age into days, where hours is more than 24hr?

      Comment


      • #4
        multiply the numerator by 24,
        Code:
        gen double age = (clock(a,"DMYhm") - clock(b,"DMYhm")) / (3600000*24)

        Comment


        • #5
          thank you sir

          Comment


          • #6
            Detail. Old-fashioned jargon for terms of a fraction : numerator / denominator https://en.wikipedia.org/wiki/Fraction

            So, it is here the denominator that is multiplied by 24. To put it directly, divide that result by 24 to convert from hours to days.

            Comment


            • #7
              Pratibha Singh, please note that if you are running Stata 17, you can use functions clockdiff() and datediff() to find differences between times and dates, respectively, in various applicable time units: years, months, days, hours, etc. Our age() function is a special case of datediff() in years. These functions return the rounded down integer of the difference. If you want the fractional part as well, you can use clockdiff_frac(), datediff_frac(), and age_frac().

              For your example, building on @Øyvind Snilsberg's code, this will work too:

              Code:
              egen a = concat(DOA TOA)
              egen b = concat(DOB TOB)
              gen double diff = clockdiff_frac(clock(b,"DMYhm"), clock(a,"DMYhm"), "day")
              See help datetime duration. This may be handy for similar problems in the future.

              Comment


              • #8
                how we calculate person years with the same data?

                Comment


                • #9
                  Pratibha Singh, if you want to find the age in years, with the fractional part, you can use either age_frac() or datediff_frac() as follows (note that both functions take dates as inputs, not times):

                  Code:
                  gen double years1 = age_frac(date(DOB,"DMY"), date(DOA,"DMY"))
                  gen double years2 = datediff_frac(date(DOB,"DMY"), date(DOA,"DMY"), "year")
                  If you want to factor in the time as well to calculate years elapsed, you can take the number of days calculated above, and divide it by the number of days in a year, say 365:

                  Code:
                  egen a = concat(DOA TOA)
                  egen b = concat(DOB TOB)
                  gen double years3 = clockdiff_frac(clock(b,"DMYhm"), clock(a,"DMYhm"), "day")/365
                  The number of days in a year can be numbers like 365, 366, or 365.25 (approximately), depending on the problem.

                  Comment


                  • #10
                    I want to calculate the Incidence rate of the given data, for calculating incidence rate we need to calculate the Person years First.

                    Comment


                    • #11
                      Kreshna Gopal (StataCorp) Sir while using this command in different Data, There are all missing values

                      egen a = concat(DOA TOA) egen b = concat(DOB TOB) gen double diff = clockdiff_frac(clock(b,"DMYhm"), clock(a,"DMYhm"), "day") I found all values missed please suggest command for this problem

                      Comment


                      • #12
                        The over-arching problem here appears to be that

                        1. The real form of your data is half-hidden in a .csv file, contrary to our requests not to use attachments and to use dataex. See https://www.statalist.org/forums/help#stata

                        2. The data are not as represented in #1, which comes from somewhere else, a spreadsheet perhaps.

                        Your data have century implicit, thus the commands suggested helpfully in #2 #4 #6 #9 are almost but not quite right.

                        Consider this, which may be closer to what you want.

                        Code:
                        * Example generated by -dataex-. For more info, type help dataex
                        clear
                        input str9 dob str5 tob str9 doa str5 toa
                        "20-Nov-19" "15:10" "23-Nov-19" "10:12"
                        "23-Nov-19" "13:10" "25-Nov-19" "21:35"
                        "26-Nov-19" "11:42" "26-Nov-19" "12:40"
                        "26-Nov-19" "11:46" "26-Nov-19" "12:40"
                        "27-Nov-19" "18:57" "27-Nov-19" "19:22"
                        "25-Nov-19" "10:57" "27-Nov-19" "19:25"
                        end
                        
                        gen double diff = clockdiff_frac(clock(dob + " " + tob,"DM20Yhm"), clock(doa + " " + toa,"DM20Yhm"), "day")
                        
                        list , sep(0)
                        
                             +---------------------------------------------------+
                             |       dob     tob         doa     toa        diff |
                             |---------------------------------------------------|
                          1. | 20-Nov-19   15:10   23-Nov-19   10:12   2.7930556 |
                          2. | 23-Nov-19   13:10   25-Nov-19   21:35   2.3506944 |
                          3. | 26-Nov-19   11:42   26-Nov-19   12:40   .04027778 |
                          4. | 26-Nov-19   11:46   26-Nov-19   12:40       .0375 |
                          5. | 27-Nov-19   18:57   27-Nov-19   19:22   .01736111 |
                          6. | 25-Nov-19   10:57   27-Nov-19   19:25   2.3527778 |
                             +---------------------------------------------------+

                        Comment


                        • #13
                          Thanks nick for your reply, But I found error while using this command.
                          error is unknown function, and I have already installed -dateax- in My stata.

                          Comment


                          • #14
                            What version of Stata are you using?

                            The implication of #11 is that you can run clockdiff_frac() which was added to Stata 16 in an update on 5 November 2020.

                            Code:
                            help clockdiff_frac() 
                            will trigger a problem if the function is not in fact supported by your Stata.

                            dataex will be distributed with Stata unless you are some versions out-of-date.

                            Comment


                            • #15
                              use command #2 a b are generated successfully but while applying the below i found missing values.

                              gen double age1 = (clock(a,"DMYhm") - clock(b,"DMYhm")) / (3600000*24)
                              (167 missing values generated)


                              please suggest where I am doing wrong

                              Comment

                              Working...
                              X