Announcement

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

  • rowmax command

    Dear all,

    I am looking for a correct way to identify the maximum value of a series of variables (values going from 0 to 10) and number of associated duplicates.

    The dataset is as follows:

    ID Var1 Var2 Var3 ...
    1 0 5 10
    2 1 10 10
    3 10 2 10
    4 0 0 0
    ...

    While using egen Pref_Max=rowmax(Var1... Var10), I am not able to correctly identify the number of duplicates of max values (whatever the highest value). I have tested with the "anycount" command but it does not correct the problem.

    Thanks a lot for your help if you have already tried to figure out such a problem.

    Best,
    Martial
    Last edited by martial foucault; 13 Nov 2017, 11:17.

  • #2
    Martial:
    welcome to the list.
    Do you mean something along the following lines?
    Code:
    . input ID Var1 Var2 Var3
    
                ID       Var1       Var2       Var3
      1.
    .  1 0 5 10
      2.
    .  2 1 10 10
      3.
    .  3 10 2 10
      4.
    .  4 0 0 0
      5. end
    
    . egen MAX=rowmax(Var*)
    
    . duplicates tag MAX, g(tag)
    
    Duplicates in terms of MAX
    
    . tab tag
    
            tag |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              0 |          1       25.00       25.00
              2 |          3       75.00      100.00
    ------------+-----------------------------------
          Total |          4      100.00
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Dear Carlo,
      Thanks a lot for your quick response. It's almost that !

      At the end, I would like to create a variable that calculates the number of times that the highest value (MAX in your example) is reported among a series of 10 variables.
      I have many cases where the highest value is reported for only 1 variable. But in some cases, I am not able to identify how many times (in row) the highest value (and which one?) is reported for more than 1 variable.

      I hope that my explanation is more clear.

      Thanks a lot.
      Best,
      martial

      Comment


      • #4
        rowmax() and anycount() are egen functions, not commands.

        Sounds as if you want to identify the row maximum and then count how values are equal to it. anycount() won't help in general as the maximum could vary.

        Adapting Carlo's helpful advice,

        Code:
        clear
        
        input ID Var1 Var2 Var3
        1 0 5 10
        2 1 10 10
        3 10 2 10
        4 0 0 0
        end
        
        egen MAX=rowmax(Var*)
        
        gen neqMAX = 0
        
        quietly forval j = 1/3 {
           replace neqMAX= neqMAX + (Var`j' == MAX)
        }
        
        list
        
            +----------------------------------------+
             | ID   Var1   Var2   Var3   MAX   neqMAX |
             |----------------------------------------|
          1. |  1      0      5     10    10        1 |
          2. |  2      1     10     10    10        2 |
          3. |  3     10      2     10    10        2 |
          4. |  4      0      0      0     0        3 |
             +----------------------------------------+

        Comment


        • #5
          Nick is, as always, inspiring and out of reach.
          Kind regards,
          Carlo
          (Stata 19.0)

          Comment


          • #6
            Dear both,
            Fantastic ! You make my working evening.
            Thanks a lot.
            Best,
            Martial

            Comment

            Working...
            X