# RE: Re: st: Calculating averages when there are missing observations

 From "Nick Cox" To Subject RE: Re: st: Calculating averages when there are missing observations Date Tue, 6 Feb 2007 13:21:27 -0000

Many will spot that

replace rowtotal = cond(npresent == 0, ., rowtotal / npresent)

could be simplified to

replace rowtotal = rowtotal / npresent

If there is merit in the first coding, it's that it makes
explicit that the total is missing if all the values
are missing.

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

n j cox

> In the current version of Stata, -egen, rmean()- is
> undocumented but works. The preferred name is -rowmean()-.
> But whatever the name, it is indeed exactly what Laura needs.
>
> Suppose this function did not exist. How would you re-invent it?
> Here is one way.
>
> gen rowtotal = 0
> gen npresent = 0
>
> qui foreach v of var v1-v5 {
> 	replace npresent = npresent + !mi(`v')
> 	replace rowtotal = rowtotal + (`v' * !mi(`v'))
> }
>
> replace rowtotal = cond(npresent == 0, ., rowtotal / npresent)
> rename rowtotal rowmean
> * drop npresent
>
> There is, as said, no need to do this. But the above is
> a fairly Stataish way of approaching the problem.
>
> The code here is easier to generalise than Laura's code. Also, her
> code has at least two specific limitations. She changed
> her data; for many purposes, she would need to reverse that.
> If any zeros present had there been before, that is trickier
> than her code allows.
>
> Also, missing can also mean .a to .z, not just system missing
> (.).
>

*
*   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/