Announcement

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

  • Counting Instance of a variable and returning average

    I am a student and new to Stata. Any help is appreciated.

    I have a data set with age and weight. I would like to return a table that tells me the number of instances for each age and the average of the weight. Can someone point me in the right direction?

    I have tried egen avgwt = mean(avgwt), by(age) and then summarizing the info but it just shows me number of observations and mean, std dev, etc for all observations. I need the average weight by each age and number of instances to use for some other calcs.

    Thank you!

  • #2
    Perhaps
    Code:
    collapse (count) N=weight (mean) weight, by(age)
    list
    will point you in a helpful direction.

    Comment


    • #3
      Code:
      egen avgwt = mean(avgwt), by(age)
      egen freq = count(avgwt), by(age) 
      tabdisp age, c(avgwt freq)
      will give a table with results either side. To use those results, you can collapse as William Lisowski

      OR

      Code:
      egen tag = tag(age)
      and carry out calculations

      Code:
      ... if tag

      Comment


      • #4
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input byte(id age) int weight
         1  9  84
         2  9  98
         3  8  81
         4 11  75
         5 11 124
         6 11 119
         7 10 105
         8  9 101
         9  9  94
        10  8 108
        11 10 105
        12 11 116
        13  9  92
        14 11 104
        15  8 108
        16 12  80
        17  8 112
        18 11 113
        19  9  91
        20 12  86
        21  9 100
        22  9 115
        23 11 110
        24  8 123
        25 12 102
        end
        
        . tabulate age
        
                age |      Freq.     Percent        Cum.
        ------------+-----------------------------------
                  8 |          5       20.00       20.00
                  9 |          8       32.00       52.00
                 10 |          2        8.00       60.00
                 11 |          7       28.00       88.00
                 12 |          3       12.00      100.00
        ------------+-----------------------------------
              Total |         25      100.00
        
        
        . table age, c(n weight mean weight) row col
        
        --------------------------------------
              age |    N(weight)  mean(weight)
        ----------+---------------------------
                8 |            5         106.4
                9 |            8        96.875
               10 |            2           105
               11 |            7       108.714
               12 |            3       89.3333
                  |
            Total |           25        101.84
        --------------------------------------
        
        
        . table age, c(n weight mean weight median weight min weight max weight) row col format(%8.1fc)
        
        --------------------------------------------------------------------------------
              age |    N(weight)  mean(weight)   med(weight)   min(weight)   max(weight)
        ----------+---------------------------------------------------------------------
                8 |            5         106.4         108.0          81.0         123.0
                9 |            8          96.9          96.0          84.0         115.0
               10 |            2         105.0         105.0         105.0         105.0
               11 |            7         108.7         113.0          75.0         124.0
               12 |            3          89.3          86.0          80.0         102.0
                  | 
            Total |           25         101.8         104.0          75.0         124.0
        --------------------------------------------------------------------------------
        Last edited by David Benson; 10 Mar 2019, 00:23.

        Comment

        Working...
        X