Statalist


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

st: Re: Summing variables by row


From   Martin Weiss <mw.stlist@googlemail.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Re: Summing variables by row
Date   Tue, 16 Sep 2008 19:16:10 +0200

Here is a -program- that Leonor may try and stress-test to see whether it accomplishes what she wants.

*************
* 1.0.0 MW 16.09.2008
* to calculate row sum, but return missing if all arguments missing
program rowsummiss
version 8
syntax varlist(min=1 numeric) [if] [in], GENerate(name)
confirm new variable `generate'
local nvars : word count `varlist'

quietly {
marksample touse, novarlist
count if `touse'
if r(N) == 0 error 2000

tempvar nmiss
gen double `generate' = 0 if `touse'
gen `nmiss' = 0
foreach v of local varlist {
replace `generate' = `generate' + cond(missing(`v'), 0, `v')
replace `nmiss' = `nmiss' + missing(`v')
}
replace `generate' = . if `nmiss' == `nvars'
}
end

*************

With rowsummiss.ado placed in your personal directory:

*************
sysuse auto, clear
*manipulate
replace rep78 = . in 12
replace headroom = . in 12
replace headroom = . in 14
replace rep78 = . in 16
replace rep78 = . in 17
replace headroom = . in 17
*see what happens
rowsummiss headroom rep78, gen(mymisssum)
ta mymiss, m
l if mymiss==.
**************

HTH
Martin Weiss
_______________________
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/




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