# Re: st:filling missing by mean value of "inrange"

 From Maarten buis To statalist@hsphsun2.harvard.edu Subject Re: st:filling missing by mean value of "inrange" Date Sat, 12 Aug 2006 09:49:07 +0100 (BST)

```WT:
I find that a task like this becomes easier if I first make a second id
variable that gives each combination of id values you want to take
together a distinct value. See the example below:

HTH,
Maarten

*-------begin example--------------
input id           year        outcome
2000        1981          .
2011        1981          2
2013        1981          3
2020        1981          .
2021        1981          4
2022        1981          2
2023        1981          3
2000        1982          .
2011        1982          1
2013        1982          2
2020        1982          .
2021        1982          3
2022        1982          4
2023        1982          2
end

gen id2 = floor((id-2000)/20)
list id id2 year, clean
list outcome

forvalues year = 1981/1982 {
forvalues id = 0/1 {
sum outcome if /*
*/ year==`year' & id2==`id', meanonly
replace outcome = r(mean) if /*
*/ year==`year' & id2==`id' & outcome==.
}
}

list outcome
*--------------end example---------------

> Hello.
> I need your help to fill in missing values by average values of
> inrange.
> In detail, I have a dataset as follows:
>
>      Id           year        outcome
> > >  2000        1981          .
> > >  2011        1981          2
> > >  2013        1981          3
> > >  2020        1981          .
> > >  2021        1981          4
> > >  2022        1981          2
> > >  2023        1981          3
> > >    ..
> > >   ..
> > > 2000        1982           .
> > > 2011        1982           1
> > > 2013        1982           2
> > > 2020        1982           .
> > > 2021        1982           3
> > > 2022        1982           4
>     2023        1982           2
>
>   what I am trying to do is to fill in missing outcomes of ID=2000
> and ID=2020
> by following methods:
>
>    Id           year        outcome
> > >  2000        1981          2.8( average of outcomes along with
> Id=20xx)
> > >  2011        1981          2
> > >  2013        1981          3
> > >  2020        1981          3.6666(average of outcomes along with
> ID=202x)
> > >  2021        1981          4
> > >  2022        1981          2
> > >  2023        1981          3
> > >    ..
> > >   ..
> > > 2000        1982           2.5(average of outcomes w/id=20xx
> > > 2011        1982           1
> > > 2013        1982           2
> > > 2020        1982           3 (average of outcomes w/id=202x)
> > > 2021        1982           3
> > > 2022        1982           4
>     2023        1982           2
>
> I have 10 years more, which need to be filled.

-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------

___________________________________________________________
All new Yahoo! Mail "The new Interface is stunning in its simplicity and ease of use." - PC Magazine
http://uk.docs.yahoo.com/nowyoucan.html
*
*   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/
```