Announcement

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

  • create new variable with value equal to other variable name

    I want to create new variable based on row maximum of three variables (employment duration) so that this new variable takes value equal to the name (or number) of the variable for which rmax command finds maximum. Please see the picture for example. In this case in the fourth row the maximum is 10.249878 for variable empdur_cl2 so I want to assign value of 2 to the newly created variable. Hope you'll help me to sort it out

    Thanks
    Kris

  • #2
    Code:
     
    gen max = . 
    gen which_max = . 
    
    forval j = 1/3 { 
         replace max = max(max, empdur_cl`j') 
         replace which_max = `j' if empdur_cl`j' == max 
    }
    This relies on the Stata fact that max(., y) is y whenever y is non-missing. That may seem non-intuitive if you know that in Stata numeric missing counts as larger than any numeric non-missing value, but it is the right behaviour given the attitude that you should ignore missings when you want to calculate maximums.

    There is much more at http://www.stata-journal.com/sjpdf.h...iclenum=pr0046 which is a fairly systematic review of working rowwise.

    Comment


    • #3
      Works! Great thank you!

      Comment


      • #4
        Good. Now please contact the administrators to register with your full real name. See "Contact us" on the forum home page.

        Comment

        Working...
        X