Announcement

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

  • Generate new variable with first non-missing value of other variables

    I have several variables, and they are either missing, or a 1 .

    I would like to keep the first non-missing value of the several variables, or return a missing value if all the variables are missing.

    I cannot wrap my head around how to do this.

    I have searched for a function similar to COALESCE , but have been unsuccessful.

    I know that the code below is syntactically incorrect, but I wanted to give a flavor of what I'm trying to accomplish.



    Code:
    
    clear
    input a b c
    1 . .
    . . .
    . . 1
    1 . .
    . 1 .
    . . .
    . . .
    1 . .
    . . 1
    end
    
    
    
    
    *****  this code works, but feels like a kludge...
    
    
    egen byte flag_1 = rowmiss(a b c)
    recode     flag_1      (1/2 = 1 "G") ///
                           (3 = 0 "N") , gen(flag_calc) test
    
    
    
    *****  version 1 : code is incorrect
    egen         flag_1 = 1 if rowmiss(a b c) < 3
    replace     flag_1 = . if rowmiss(a b c) == 3
    
    
    *****  version 2 : code is incorrect  
    generate     flag_1 = .
    replace     flag_1 = a if a!= .
    replace        flag_1 = b if a == . & if b != .
    replace     flag_1 = c if a == . & if b == . & if  c != .
    
    
    *****  version 3 : code is incorrect
    generate     flag_1 = .
    replace     flag_1 = 1 if rowmiss(a b c) < 3
    Last edited by jonathan.j.beard; 18 Aug 2014, 09:33.

  • #2
    Code:
    egen firstnm = rowfirst(a-c)

    Comment


    • #3
      Knew there had to be something within STATA's internals... thanks so much!

      Comment

      Working...
      X