# st: Re: Replacing Missing Values for multiple variables at one time?

 From "Michael Blasnik" To Subject st: Re: Replacing Missing Values for multiple variables at one time? Date Sat, 08 Jul 2006 19:20:03 -0400

You can do this with a loop. You need to be able to refer to all of the price variables, either by creating a list or using wildcards if the names are amenable. For example, if all of your price variable names end in "price", then you could:

foreach pricevar of varlist *price {
cap drop avg
by village rd: egen avg=mean(`pricevar')
replace `pricevar'=avg if `pricevar'==.
}
drop avg

This will use a single varible, called avg, that will be filled in with the village averages you want for each of the price vars in turn.

Michael Blasnik
michael.blasnik@verizon.net

----- Original Message ----- From: "JEFFREY ROSEN" <rosen.53@osu.edu>
To: <statalist@hsphsun2.harvard.edu>
Sent: Saturday, July 08, 2006 5:46 PM
Subject: st: Replacing Missing Values for multiple variables at one time?

I'm working with a dataset that is sorted by households villages and rounds. The dataset is set up such that if a household made a purchase, a quantity and price was inputed. If there were no purchases, a missing value for price would be inputed. I would like to have the missing value be replaced with an average price from each village in each round.

So my dataset looks something like

hhold village rd 1price 2price 3price
1 1 1 5 . 3
1 1 2 3 2 4
2 1 1 . 3 4
2 1 2 1 3 .
3 2 1 3 2 4
3 2 2 2 3 .
4 2 1 4 5 2
4 2 2 . . 2

What I've done in the the past if there was run:

bysort village rd: egen 1pricevil = mean(1price)
replace 1price = 1pricevil if 1price == . & 1pricevil > 0

This is an easy code when I only have a couple prices, but I am working with a dataset that has over 50 variables that need to be changed. Is there some way to code it without having to enter in the above code for every variable?

```*