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

 From "Nick Cox" To Subject st: RE: RE: generate sum of variables & ignore missing Date Thu, 5 Jun 2008 13:55:28 +0100

```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')
}

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

Martin Weiss

egen varD=rowtotal(varA - varC)

Andrea Bennett

I have 3
columns (varA, varB, varC) containing dummy variables (0, 1, missing
values "."). I would like to generate "varD" the sum of each row, e.g.
if varA==1 varB==0 varC==1 I should have varD==2. This works clearly
fine when there are no missing values (varD= varA + varB + varC). But
is there an easy way to let Stata read a missing value as "0" so that
varA==1 varB==. varC==1 would result in varD==2 (without actually
replacing the missing values with "0")? I am trying to get it to work
with -missing()- but so far have not succeeded.

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