I want to calculate the average of all members of the group I am in, but not include myself in the average. Suppose that the group variable is called group
and I want to take the average of val1
by Group
, excluding myself. The new column I wish to create is avg
. The data looks as follows (with the correct values of avg
inputed so you can see what I mean).
Obs Group val1 avg
1 A 6 8
2 A 8 6
3 B 10 13
4 C 4 4
5 C 2 5
6 C 6 3
7 B 12 12
8 B 14 11
If I wanted to include myself in the calculation it would be straightforward. I would just do:
bysort Group: egen avg = mean(val1)
But how do I implement this with the wrinkle that I don't include myself?