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

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
RE: st: data loop question |

Date |
Wed, 11 May 2005 20:52:53 +0100 |

Eric Wruck's warnings about ties and missings are very important. If you use a string variable to keep track of which variable or variables hold the maximum, you can also note ties. egen max = max(x1-x10) gen which = "" forval v = 1/10 { replace which = which + " `v'" if x`v' == max } replace which = ltrim(which) tab which Also check out -rowranks- from SSC. Nick n.j.cox@durham.ac.uk Eric G. Wruck > Ok, here's a way of doing it: I made up a little example & > wrote some code. This looks like it'll work: > > . l > > +--------------+ > | x1 x2 x3 | > |--------------| > 1. | 1 2 3 | > 2. | 7 5 4 | > 3. | 10 34 18 | > 4. | 16 12 16 | > +--------------+ > > . gen maxX = 0 > > . > . egen max = rowmax(x*) > > . > . forvalues i = 1(1)3 { > 2. replace maxX = `i' if x`i' == max > 3. } > (2 real changes made) > (1 real change made) > (2 real changes made) > > . > . l > > +---------------------------+ > | x1 x2 x3 maxX max | > |---------------------------| > 1. | 1 2 3 3 3 | > 2. | 7 5 4 1 7 | > 3. | 10 34 18 2 34 | > 4. | 16 12 16 3 16 | > +---------------------------+ > > . > . > However, you need to watch out for at least two things: (1) > ties & (2) missing values. The code I wrote takes the last > maximum tie as the variable of interest -- that is, in > observation 4, x3 is determined to hold the max even though > x1 holds the same value. Missing values are effectively > treated as positive infinity, so you may have to control for > that separately -- this depends in part on what egen does > with missing values. Shawn > >I have a series of variables, say x1-x10, and I would like > to determine > >the numeric suffix associated with the variable that has the maximum > >value for each case. For example, maybe x9 has the max for > case 1 and > >x7 has the max for case 2, then I would like to end up with > a variable, > >say MaxX, that has a 9 and 7 for the first two cases respectively. I > >think a data loop is the way to go, but I've tried working > out a loop to > >do this and it's not coming to me. * * 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/

- Prev by Date:
**st: endogeneity in a multinomial logistic regression** - Next by Date:
**st: global curve fitting** - Previous by thread:
**RE: st: data loop question** - Next by thread:
**st: RE: Generating a New Variable** - Index(es):

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