Hello everyone,

I have a data set with 5 indicators. I would like to save as a macro, for each "cluster", the name of the indicator that has the maximum value.

At first I thought I could use "egen rowmax" to select the highest value for each one, and then use "ds" to tell me the variable names. However, "ds" does not accept any "if" condition, so I don't really know how to approach this.

I had thought of doing the following code (which does not work). Any ideas?

Thank you!

Alipio

Code:
egen maximumindicator = rowmax(sumindicator*)

quietly sum cluster, d
forvalues c = 1/`r(max)'{
preserve
   keep if cluster == `c'
       foreach v of varlist sumindicator* {
          ds `v' if `v' == maximumindicator
          local namevar`c' `r(varlist)'
        }
restore
}
end
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input double(cluster sumindicatorAIS1 sumindicatorAIS2 sumindicatorAIS3 sumindicatorAIS4 sumindicatorAIS5)
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
1 14.849999999999994 14.849999999999994 0 18.888888888888886                 10
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
2 12.650000000000002 12.649999999999999 0 16.956521739130423 16.956521739130437
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
3 18.150000000000002 18.150000000000002 0 18.150000000000002 21.818181818181813
end