Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: RE: generate sum of variables & ignore missing


From   Andrea Bennett <[email protected]>
To   [email protected]
Subject   Re: st: RE: RE: generate sum of variables & ignore missing
Date   Thu, 5 Jun 2008 15:18:34 +0200

This is the solution where I can lean some more of Stata coding...

Thank you!


On Jun 5, 2008, at 2:55 PM, Nick Cox wrote:

A solution from first principles is

gen total = 0

qui foreach v of var varA varB varC {
	replace total = total + `v' if !missing(`v')
}

Notice that -total- ends up with 0 if all are missing.

Suppose you want

missing if all are missing
the total of non-missings otherwise

That is only a little more complicated.

gen total = .

qui foreach v of var varA varB varC {
	replace total = cond(missing(total), `v', total + `v') if
!missing(`v')
}
*
*   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/



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