Announcement

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

  • Generate variable that evaluates rows, returns corresponding varname

    Have a panel dataset of the following form, where t is time variable and id is panel variable.

    t id x y z
    1 1 2 0 1
    1 2 2 0 6
    1 3 5 0 3
    2 1 0 0 4
    2 2 2 1 7
    2 3 0 2 1

    Trying to generate a new variable, a, that looks across rows in variables x, y and z, evaluates the maximum value and generates the name of the variable in which that maximum exists to get the below output.

    t id x y z a
    1 1 2 0 1 x
    1 2 2 0 6 z
    1 3 5 0 3 x
    2 1 0 0 4 z
    2 2 2 1 7 z
    2 3 0 2 1 y

    Evaluating the maximum part is pretty straightforward ( e.g. gen a = max(x, y, z) ), but getting stata to return the name of the variable has me stumped.

    Thanks
    Alban

  • #2
    This is a common question here. For example,

    http://www.statalist.org/forums/foru...value-in-a-row

    http://www.statalist.org/forums/foru...-variable-name

    http://www.statalist.org/forums/foru...erate-variable
    Last edited by Nick Cox; 30 Sep 2014, 08:37.

    Comment

    Working...
    X