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

RE: st: Using -egen- with existing variable


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Using -egen- with existing variable
Date   Mon, 6 Jun 2005 10:31:38 +0100

On the last point, here is yet another way. 

gen total = . 
qui forval i = 1/10 { 
	replace total = tot`i' if situation == `i' 
} 

Nick 
[email protected] 

David Kantor replied to Mike Lacey

> >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?
> >

> 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,
> . ))))))))));
> 

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