Announcement

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

  • Finding the maximum value in a matrix

    I am working with firm-level panel data with a firm identifier (ident) and a year variable (year). For the same firm (here ident=102) and for different years (year=2010-2013), I have values on four variables (X1-X4). I would like to write a code that generates for each firm (here ident=102) and for all the years a new variable that contains the maximum (non-missing) value in the matrix, that is 5. I used the code egen new=rowmax(X1-X4) but it didn't work. Any suggestion is welcome.


    ident year X1 X2 X3 X4
    102 2010 2 3 4 5
    102 2011 2 3 4 .
    102 2012 2 3 . .
    102 2013 2 . . .

  • #2
    Code:
    clear
    input int(ident    year    X1    X2    X3    X4)
    102    2010    2    3    4    5
    102    2011    2    3    4    .
    102    2012    2    3    .    .
    102    2013    2    .    .    .
    103    2010    2    3    4    5
    103    2011    2    6    4    .
    103    2012    2    3    .    .
    103    2013    2    .    .    .
    end
    
    egen rowmax = rowmax(X1-X4)
    bysort ident: egen wanted = max(rowmax)
    list, clean noobs
    Code:
    . list, clean noobs
    
        ident   year   X1   X2   X3   X4   rowmax   wanted  
          102   2010    2    3    4    5        5        5  
          102   2011    2    3    4    .        4        5  
          102   2012    2    3    .    .        3        5  
          102   2013    2    .    .    .        2        5  
          103   2010    2    3    4    5        5        6  
          103   2011    2    6    4    .        6        6  
          103   2012    2    3    .    .        3        6  
          103   2013    2    .    .    .        2        6

    Comment


    • #3
      Dear Daniel, thank you for the solution.

      Comment

      Working...
      X