Announcement

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

  • Average of a variable using xtsum

    Hi I am new to the panel variables and have encountered a problem. I am using xtsum command to get the average age of patient by panel variable (which is patient ID). However, since every patient have different frequency of data I end up with mean age (accounting the different frequencies which is 67.38). However, I'd like to get mean of age without accounting for different frequencies in each patient ID - otherwise I'd like to get the real mean age of patients which is 68.78. Are there any stata commands that allow me to do that?

    Thanks in advance, I put the commands that I used below.

    xtsum age

    Variable | Mean Std. Dev. Min Max | Observations
    -----------------+--------------------------------------------+----------------
    age overall | 67.38174 7.829393 52 85 | N = 2245
    between | 9.028221 52 85 | n = 19
    within | 0 67.38174 67.38174 | T-bar = 118.158


    tab ïpatientid, sum(age)

    | Summary of age
    ïpatientid | Mean Std. Dev. Freq.
    ------------+------------------------------------
    1 | 80 0 26
    2 | 71 0 67
    3 | 67 0 46
    4 | 76 0 128
    5 | 70 0 54
    6 | 85 0 70
    7 | 70 0 153
    8 | 63 0 266
    9 | 67 0 153
    10 | 76 0 172
    11 | 61 0 48
    12 | 67 0 174
    13 | 52 0 127
    14 | 85 0 48
    15 | 54 0 145
    16 | 68 0 130
    17 | 71 0 219
    18 | 61 0 91
    19 | 63 0 128
    ------------+------------------------------------
    Total | 67.381737 7.8293933 2,245

    display (80+71+67+76+70+85+70+63+67+76+61+67+52+85+54+68+7 1+61+63)/19
    68.789474

  • #2
    Code:
    egen flag = tag(ipatientid)
    summ age if flag
    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.



    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Thank you so much for your help Clyde. I will keep in mind to use dataex for the future.

      Comment

      Working...
      X