# RE: st: SUM question

 From "n.j.cox@durham.ac.uk" To "statalist@hsphsun2.harvard.edu" Subject RE: st: SUM question Date Fri, 16 May 2008 07:37:23 -0700

```Svend is right, and for several variables in a row I would usually reach
for -egen-.

It is also true that for the case of two variables every possibility you
might want is achievable without recourse to -egen-.

gen sum = cond(missing(var1, var2), min(var1, var2), var1 + var2)

returns var1 if var2 is missing, and conversely, and var1 + var2
otherwise. Thus if both var1 and var2 are missing, then the result is
missing (not 0).

You could use -max()- to the same effect. Similarly, consider

gen sum = cond(missing(var1), var2,
cond(missing(var2), var1, var1 + var2))

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

Svend Juul

Satomi wrote:

..
I think  egen newvar = rowtotal(var1 var2) is equivalent to replacing
missing values with 0(zero).

The SUM function in SPSS adds cases in the variable where at least one
observation is valid:

Var 1   Var2
1        4
3        5
4        5
5        8
5
.

When  compute newvar= sum(var1, var2). is used, there will be 1 missing
case.
When compute newvar= var1+var2. is used, there are 2 missing cases.

=================================================================

You can gain control with -egen-'s -rowmiss()- and -rownonmiss()-
functions:

. generate plusvar = var1+var2
. egen egenvar1 = rowtotal(var1 var2)
. egen nomiss = rownonmiss(var1 var2)
. generate egenvar2 = egenvar1 if nomiss>0

. list , clean

var1   var2   plusvar   egenvar1   nomiss   egenvar2
1.      1      4         5          5        2          5
2.      3      5         8          8        2          8
3.      4      5         9          9        2          9
4.      5      8        13         13        2         13
5.      .      5         .          5        1          5
6.      .      .         .          0        0          .

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

_____________________________________________________________________
Mensaje analizado y protegido por Telefonica Empresas
*
*   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/

Privileged, confidential or patient identifiable information may be contained in this message. This information is meant only for the use of the intended recipients. If you are not the intended recipient, or if the message has been addressed to you in error, do not read, disclose, reproduce, distribute, disseminate or otherwise use this transmission. Instead, please notify the sender by reply e-mail, and then destroy all copies of the message and any attachments.

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