Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Missing values

From   "Nick Winter" <>
To   <>
Subject   st: RE: Missing values
Date   Tue, 25 Jun 2002 10:43:52 -0400

> -----Original Message-----
> From: Babigumira Ronnie [] 
> Sent: Tuesday, June 25, 2002 10:23 AM
> To:
> Subject: st: Missing values
> Hi Listers
> I have data on household expenditure. Households bought "x" 
> kgs (quan) of
> food crops (exp) worth a certain amount of money (unitvalu). 
> However, I
> have cases where the quantity bought is missing (either because the
> household couldn't recall or an error in data collection) however the
> amount spent by these households is known. The variables are
> lc1code   housecode  exp   quan  unitvalu
> 11233      112331    566    1        500
> I would like to replace the missing quantities purchased with 
> community
> (lc1code) averages. If the lc1code, food item (exp), unitvalu 
> are the same
> then we can deduce the quantity (quan) that can be purchased by that
> amount of money. What I now want to do is to replace all missing
> quantities with a value imputed from community averages. To 
> make it more
> clear
> If we know that 500/= buys 1kg of cassava in a given community, then a
> respondent in the community who spends 500/= on cassava should
> automatically be purchasing 1kg. 
> I want to write a code that would automatically execute this for all
> missing cases however, I can't figure out where to start. I would
> appreciate any help.

One way to go might be to create a dataset of mean community prices
using -collapse-, and then use -merge- to apply that data back to your

	. collapse unitvalu , by(lc1code exp)
	. rename unitvalu meanprice
	. sort lc1code exp
	. save pricedata
	. use <original dataset>
	. save pricedata
	. merge lc1code exp using pricedata

	. generate estqty = <amtspent> / meanprice

As it stands, the -collapse- will calcuate the mean price for each
community-exp combination, based on a simple average of the prices in
the dataset.  If you want it weighted based on quantity purchased, I
think this will do it:

	. collapse (sum) unitvalue quan, by(lc1code exp)
	. gen meanprice = unitvalue/quan
	. drop unitvalue quan

--Nick Winter

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index