I have a sample data set with four columns (L1 to L4) and five rows. I am trying to extract the column number which contains the minimum value across the rows. If there are two rows with the same minimum value, I would like to extract the first column that contains the minimum value (ignoring the other columns).
Code:
. version version 13.1 . clist L1-L4 L1 L2 L3 L4 1. 2 2 . . 2. 3 2 . 2 3. 7 3 6 8 4. 4 3 7 3 5. 4 5 7 9
I then calculate the row minimum.
Code:
. egen min=rowmin(L1 L2 L3 L4) .clist L1 L2 L3 L4 min 1. 2 2 . . 2 2. 3 2 . 2 2 3. 7 3 6 8 3 4. 4 3 7 3 3 5. 4 5 7 9 4
I tried the following:
Code:
. gen col_no=. (5 missing values generated) . local array1 "L1 L2 L3 L4" . local i=1 . local n : word count `array1' . while `i' <= `n' { 2. local var1 : word `i' of `array1' 3. replace col_no =`i' if `var1'==min 4. local i = `i' + 1 5. } (2 real changes made) (4 real changes made) (0 real changes made) (2 real changes made) . clist L1 L2 L3 L4 min col_no 1. 2 2 . . 2 2 2. 3 2 . 2 2 4 3. 7 3 6 8 3 2 4. 4 3 7 3 3 4 5. 4 5 7 9 4 1
Thanks in advance.
0 Response to extracting column number based on minimum row value
Post a Comment