 Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# RE: st: Replace value for some observation with average values of some other observations?

 From Nick Cox To "'statalist@hsphsun2.harvard.edu'" Subject RE: st: Replace value for some observation with average values of some other observations? Date Wed, 18 Jan 2012 13:21:04 +0000

```Scott's answer is two command lines and answers the question perfectly.

I've edited it below because as received the two lines were run together.

The solution from "Stata Chris" that does "not quite work" doesn't work for the following reasons:

1. A invocation to -mean()- on the right-hand side of a -replace- statement could only work if there a Stata function (strict sense) -mean()-, but no such function exists.

2. There is an -egen- function -mean()- but it can't be used in -replace- and it doesn't have a syntax anything like Chris' example.

3. -outcome[id==2]-, say, with reference to a variable -outcome- would in context evaluate to -outcome- which is missing or -outcome- which would be the first value of -outcome-, depending on the evaluation of -id == 2-. It never means subset of -outcome- for which -id- is 2.

Nick
n.j.cox@durham.ac.uk

Scott Merryman

sum outcome  if id == 2 | id ==3

replace outcome = r(mean) if id == 1

On Wed, Jan 18, 2012 at 6:16 AM, Stata Chris <statachris@gmail.com> wrote:

> Suppose I have a dataset with variables id and outcome.
> Now for the observation with id=1 I want to replace the value of the
> outcome by the average outcome values of the observations with id=2
> and id=3.
>
> I thought of
>
> replace outcome=mean(outcome[id==2],outcome[id==3]), but this does not
> quite work.
>
> Would anyone know how to do this correctly?

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```