Ryan's solution is naturally correct.
mean of others = (sum of all - this value) / (number of values - 1)
It needs a little care given the possibility of missing values,
but the -egen- functions do that for you.
Ryan's code can be telescoped to
egen sum_var = sum(X), by(group)
egen count_var = count(X), by(group)
gen mean_X = (sum_var - X) / (count_var - 1)
You could do without -egen-:
bysort group : gen sum_var = sum(X)
by group : gen count_var = sum(X < .)
by group : gen mean_X = (sum_var[_N] - X) / (count_var[_N] - 1)
In the current version of Stata, -egen, sum()- is there
but hidden: the preferred name is -egen, total()-.
All this and more is discussed in
FAQ . . Creating variables recording prop. of the other members of a group
4/05 How do I create variables summarizing for each
individual properties of the other members of a
group?
http://www.stata.com/support/faqs/data/members.html
which underlines the scope for solving your problems by looking
at the FAQs, as recommended in the Statalist FAQ.
Nick
[email protected]
Ryan Herzog replied to Quang Nguyen:
> > 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.
> Generate the sum of each group: egen sum_var = sum(X), by(group)
> Subtract the X value for that Id: gen sum_less = sum_var - X
> Generate a count variable by group: egen count_var =
> count(X), by(group)
> Subtract one from count, to exclude the ID's observation: gen
> count_less
> = count_var - 1
> gen mean_X = sum_less / count_less
*
* 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/