Statalist


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

RE: st: SUM question


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: SUM question
Date   Thu, 15 May 2008 15:04:28 +0100

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
[email protected] 

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/



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