Announcement

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

  • #16
    Originally posted by Hemanshu Kumar View Post
    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))
    Thank you very much for that Hemanshu - can i just clarify, does that code round down to the nearest integer? I typically use the floor command for this, so I am just trying to understand your code - Thanks again!

    Comment


    • #17
      No rounding is needed since the code is in terms of years and months. The last bit, which uses day(), is actually a boolean evaluation and so can only be 0 or 1.

      Comment


      • #18
        Originally posted by Hemanshu Kumar View Post
        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)&lt;day(BirthD))
        Thank you very much for that Hemanshu - can i just clarify, does that code round down to the nearest integer? I typically use the floor command for this, so I am just trying to understand your code - Thanks again!

        Comment


        • #19
          Originally posted by Hemanshu Kumar View Post
          No rounding is needed since the code is in terms of years and months. The last bit, which uses day(), is actually a boolean evaluation and so can only be 0 or 1.
          Ah I see, thanks for clarifying!

          Comment


          • #20
            Originally posted by Hemanshu Kumar View Post
            No rounding is needed since the code is in terms of years and months. The last bit, which uses day(), is actually a boolean evaluation and so can only be 0 or 1.
            If I wanted to tabulate the mean age in months by another variable how would I go about this? The other variable is: state (a numerical variable that codes the state of origin for each patient)

            Comment


            • #21
              Related but distinct issues are being batted and back and forth.

              @Nicholas, good name that, I'd suggest that

              * if you want to take means later, then you shouldn't use the floor() function earlier, or do any rounding any way

              * if the data are daily dates, why not calculate using days as the units?

              Comment


              • #22
                @Nick (agreed, excellent name):
                1) Noted - thank you for this
                2) Unfortunately the powers above me would like it that way (haha)

                Any suggestions as how to tabulate the mean age in months by my state variable would be greatly appreciated (appreciate this is a somewhat distinct issue, but would be the last issue on this thread/data query).

                Been trying to use: tabulate mean(age_months) state

                However, this is coming up an error that: factor-variable and time-series operators not allowed

                Thanks for your help!

                Comment


                • #23
                  If I was obliged to report a mean age in months, then for me calculating months should be the very last step.

                  The syntax you cite is a long way from tabulate syntax, but a little closer to table syntax.

                  Comment


                  • #24
                    Originally posted by Nick Cox View Post
                    If I was obliged to report a mean age in months, then for me calculating months should be the very last step.

                    The syntax you cite is a long way from tabulate syntax, but a little closer to table syntax.
                    Hmm, what would you suggest then Nick? I am quite lost as to how to approach this

                    Comment


                    • #25
                      Sorry, but I am lost now on what else you seek, as I've already made suggestions about what to do and what not to do. I will add only that dividing durations in days by a notional month length of 30 days seems to me defensibly simple and likely to be precise enough for all statistical and scientific purposes I can imagine. If there are different conventions in your field, you'll need others to tell you about them. I gather that the data arise in medical statistics, which is not my area.

                      Comment


                      • #26
                        Apologies, I am trying to figure out the appropriate code to produce a table of mean of age in months (variable: age_months) by another variable (variable: state (which is numerical))

                        Comment


                        • #27
                          If I recall correctly, you are on Stata 16, so

                          Code:
                          tabulate state, summarize(age_months)
                          
                          table state, c(mean age_months)
                          seem possible. For more explanation, see the help for either command, or indeed for tabstat.

                          Comment

                          Working...
                          X