# Re: st: data loop question

 From "Eric G. Wruck" To statalist@hsphsun2.harvard.edu Subject Re: st: data loop question Date Wed, 11 May 2005 15:04:09 -0400

```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.   }

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

Hope this helps,

Eric

>Statalist,
>
>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.
>
>
>Thanks,
>Shawn

--

===================================================

Eric G. Wruck
Econalytics
Columbus, OH  43209

ph:      614.231.5034
cell:    614.330.8846
eFax:    614.573.6639
eMail:   ewruck@econalytics.com
website: http://www.econalytics.com

====================================================
*
*   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/
```