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

st: RE: max value


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: max value
Date   Tue, 8 Apr 2003 13:52:53 +0100

Ronnie Babigumira
> 
> Hi (stata8, win2k)
> I have 3 variables p1 p2 p3. I need to generate a new variable pred
> 
> "pred" ==
> 1 if p1 is the largest of the 3,
> 2 if p2 is the largest
> and 3 if p3 is the largest
> 
> Any elegant ideas (thats all I seem to get from the list 
> and for that I am
> forever grateful)

None of these is very elegant. 

gen pred = 1 
replace pred = 2 if p2 == max(p1,p2,p3)
replace pred = 3 if p3 == max(p1,p2,p3) 

gen pred = . 
forval i = 1/3 { 
	replace pred = `i' if p`i' == max(p1,p2,p3) 
}

gen id = _n 
reshape long p, i(id) 
bysort id (p) : gen pred = _j[_N] 
reshape wide 

Are you worried about ties? 

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