Stata: save name of variable with max value as a string -
i have several variables in same row: x1 x2 x3 x4
with egen , rowmax function, create new variable containing value x* highest value:
egen max_x = rowmax(x1 x2 x3 x4)
however, instead of saving maximum value, save name of variable contains maximum value string. how can that?
there might single command this, here 1 approach...
// generate test data set obs 10 forvalues i=1/4 { gen float x`i' = runiform() } tempvar valmax argmax gen `valmax' = x1 gen `argmax' = "x1" foreach v of varlist x2-x4 { // value beat current highest value? replace `argmax' = "`v'" if `v' > `valmax' & !mi(`v') replace `valmax' = max(`valmax', `v') } list you should consider how ties , missing values handled.
Comments
Post a Comment