# st: Re: how to calculate mean of other individuals in same group

 From Kit Baum To statalist@hsphsun2.harvard.edu Subject st: Re: how to calculate mean of other individuals in same group Date Wed, 14 Feb 2007 07:17:16 -0500

```. egen gsum = sum(var3),by(var2)

. egen gsize=count(var3),by(var2)

. g adjm = (gsum-var3)/(gsize-1)

. l

+----------------------------------------------+
| var1   var2   var3   gsum   gsize       adjm |
|----------------------------------------------|
1. |    1      a      5     29       4          8 |
2. |    2      a      7     29       4   7.333333 |
3. |    3      a      9     29       4   6.666667 |
4. |    4      a      8     29       4          7 |
5. |    5      b      3     16       3        6.5 |
|----------------------------------------------|
6. |    6      b      4     16       3          6 |
7. |    7      b      9     16       3        3.5 |
+----------------------------------------------+

Kit Baum, Boston College Economics
http://ideas.repec.org/e/pba1.html
An Introduction to Modern Econometrics Using Stata:
http://www.stata-press.com/books/imeus.html

On Feb 14, 2007, at 2:33 AM, statalist-digest wrote:

```
```A simplified version of my data looks as follows:

ID      Group     X
1       a             5
2       a             7
3       a             9
4       a             8
5       b             3
6       b             4
7       b             9
..........................

I would like to generate a new variable whose value is the average of
other individual in the same group as the concerned individual. For
example, for the first individual (ID=1), this will be: (7+9+8)/3= 8.
For the 6th individual, this will be (3+9)/2=6 and so on.
```
```*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```