Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Identifying variable where value for rowmin was obtained

    Hello,

    I have 1200 observations for which I'm trying to find: a) the min value among some 30 variables and; b) which variable was this with the minimum value.

    My dataset looks like this:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id a b c)
    1 .1  .2  .3
    2 .2  .3  .4
    3 .4  .1  .4
    4 .3 .37  .5
    5 .2  .4 .22
    6 .3  .5  .6
    7 .1  .3  .1
    end
    I can find the min value among a b c using:

    Code:
    egen min = rowmin(a b c)
    However, I'm having trouble determining which variable was the one with the minimum value. Please note that there are cases (such as with id == 7) in which the minimum value is equal among two different variables. Ideally, the code should return this:

    Code:
         +--------------------------------------+
         | id    a     b     c   min   whichvar |
         |--------------------------------------|
      1. |  1   .1    .2    .3    .1          a |
      2. |  2   .2    .3    .4    .2          a |
      3. |  3   .4    .1    .4    .1          b |
      4. |  4   .3   .37    .5    .3          a |
      5. |  5   .2    .4   .22    .2          a |
         |--------------------------------------|
      6. |  6   .3    .5    .6    .3          a |
      7. |  7   .1    .3    .1    .1        a c |
         +--------------------------------------+
    Any ideas on how to obtain this?

  • #2
    You put your finger on the problem. If there are ties you need to record two or more variable names.

    Code:
    gen whichisit = "" 
    
    quietly foreach v in a b c { 
          replace whichisit = cond(missing(whichisit), "`v'", whichisit + " `v'") if `v' == min 
    }

    Comment


    • #3
      Works like a charm. Thanks Nick!

      Best;

      Comment


      • #4
        There is a slight wrinkle, that may not apply in Igor Paploski's data, but could bite others. Assuming the code shown in #1 for creating the variable min, it will have a -float- storage type, by default. In the example data shown, a, b, and c also have a float storage type, and, so the comparison `v' == min in Nick's code will work as intended. But in a different data set, if any of these variables had -double- storage type, then the code could fail due to precision issues. The solution to this problem, when it arises, is to recast all of the variables a, b, and c to -double- before calculating min, and to create min itself as a -double- with -egen double min = rowmin(a b c)-

        Comment


        • #5
          Perfect. In my case, all my 30 something variables were indeed stored as -float-, but it's good to have "ifs and buts" scenarios registered - who knows who might find this topic in the future. Thanks Clyde!

          Comment

          Working...
          X