Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Using -egen- with existing variable


From   David Kantor <dkantor@jhu.edu>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Using -egen- with existing variable
Date   Fri, 03 Jun 2005 13:06:00 -0400

At 06:18 PM 6/2/2005 -0600, Mike Lacey wrote:
I want to use -egen- to create a total out of a1, a2, a3 in one situation, b1, b2, b3 in another situation, etc., but am running
into problems about how to use egen on an existing variable.

For example:

*I would like to do something like:
egen tot = rowtotal(a1 a3 a3) if situation = 1
egen tot = rowtotal(b1 b2 b3) if situation = 2
... etc.
egen tot = rowtotal(z1 z3 z3) if situation = 10
*
This of course is syntactically invalid. So, the kludge I
came up with was:

egen tot1 = rowtotal(a1 a3 a3) if situation = 1
egen tot2 = rowtotal(b1 b2 b3) if situation = 2
...etc.
egen tot10 = rowtotal(z1 z3 z3) if situation = 10
generate total = rowtotal(tot1-tot10)
* Make total missing where appropriate
egen nmiss = rowmiss(tot1-tot10)
replace total = . if nmiss == 10
drop nmiss tot1=tot10

What would be a Stata-ish way to do this?

Thanks,

Mike Lacy
Fort Collins CO USA
(970) 491-6721 office
What you did is fine, putting aside minor grammatical and typographic missteps. I also don't know about rowtotal. You may have intended rsum instead -- unless there is a rowtotal egen function in Stata 9, which I don't have yet.

Here's another way to do it, just a bit different from yours:

egen tot1 = rsum(a1 a2 a3)
egen tot2 = rsum(b1 b2 b3)
...
egen tot10 = rsum(z1 z2 z3)

#delimit ;
gen total =
cond(situation==1, tot1,
cond(situation==2, tot2,
...
cond(situation==10, tot10,
. ))))))))));

----
I hope this helps.
-- David

David Kantor
Institute for Policy Studies
Johns Hopkins University
dkantor@jhu.edu
410-516-5404

*
* 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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index