Announcement

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

  • Generating a new variable on non-missing condition

    Hi

    I have a small query. I am trying to generate a new variable (X), based on my existing 3 variables (A, B and C). I want the new variable X to equal A if A has a non-missing value. In case of A's value is missing, then X should equal B, if however if B's value is missing then X should equal C's value. I tried using if and else if commands but I am not being able to execute it properly. Kindly share any help or suggestions. Thank You.

  • #2
    No need for if/else:
    Code:
    gen x = a
    replace x = b if a==.
    replace x = c if b==.

    Comment


    • #3
      Code:
      gen x = cond(a < ., a, cond(b < ., b, c))
      is another way to do it. You're free to regard that as too confusing.

      Comment

      Working...
      X