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

st: RE: RE: max value (solved)


From   "Ronnie Babigumira" <ronnie.babigumira@ios.nlh.no>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: max value (solved)
Date   Tue, 8 Apr 2003 16:23:11 +0200

Thanks Nick, David, and Euler, for your responses. Both of Nicks solutions
solve my problem so to close.

My problem I have 3 variables
+--------------------------------+
p1         p2         p3
--------------------------------
.0193647   .7684672   .2121681
.0110648   .0962676   .8926677
.00574   .8762589   .1180012
.0045997   .1808127   .8145875
.0024693   .3957042   .6018265
--------------------------------
.0067998   .3438436   .6493566
.0113164   .0771485   .9115351
.0123369    .133595    .854068
.0103629   .2671735   .7224636
.0085421   .1814262   .8100317
+--------------------------------+

I need to generate a 4th variable that takes on the values 1 2 and 3 if p1
p2 p3 are the largest in the group respectively (I am not concerned about
ties at the moment because they have not occured)

A specific fix (Nick1)
gen pred = 1
replace pred = 2 if p2 == max(p1,p2,p3)
replace pred = 3 if p3 == max(p1,p2,p3)

A more general fix for pn (where n is the p1 p2 p3 ....pn) (Nick2)

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

Thanks (I will add that they are both neat fixes)

Ronnie

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Nick Cox
Sent: 8. april 2003 14:53
To: statalist@hsphsun2.harvard.edu
Subject: st: RE: max value


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
n.j.cox@durham.ac.uk
*
*   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/

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