Announcement

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

  • Generate var3 for if var1>var2

    I am trying to make a new variable that shows if people have a higher value in one variable than another.
    For example, ideally I would have

    var1 var2 var3
    0 0 .
    2 0 1
    2 2 .
    4 17 0
    5 1 1
    3 5 0

    Is there an egen command that allows this?

    Thanks!

    Tori

  • #2
    You can do this directly with -gen-, not through -egen-:

    Code:
    gen var3 = var1 > var2 if var1 != var2
    I notice in your example that you want var3 to have a missing value of var1 == var2, and this code accomplishes that.

    You don't say what you want in var3 if var1 or var2 is missing. This code does nothing special to handle this case, so remember that in Stata, missing value is taken as greater than any non-missing value. Ordinarily when I write code like this, I modify the code so that if var1 or var2 is missing, var3 is also missing. But since you are using missing value in var3 to mark the situation where var1 == var2, this might have been confusing. If var1 and var2 are never missing, then this is simply not an issue. But if you do have missing values in one or both of these variables and this code doesn't do what you need, do post back with examples and explanations.

    Comment

    Working...
    X