Announcement

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

  • Combining multiple observayions

    Dear all,

    I am trying to construct birth histories in a rich data source. Some individuals have two entries of their child history which do not match. I would like to merge these into one line so that I can merge it into other
    Essentially I would like to go from this

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id child1 child2 child3 child4)
    1 100 134 250 .
    1 210   .   . .
    2 111 143   . .
    2 121   .   . .
    3  22 109   . .
    3  92   .   . .
    4  40  62   . .
    4  51  76   . .
    end
    and the end result should be this
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id child1 child2 child3 child4)
    1 100 134 210 250
    2 111 121 143   .
    3  22  92 109   .
    4  40  51  62  76
    end
    Any help appreciated

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id child1 child2 child3 child4)
    1 100 134 250 .
    1 210   .   . .
    2 111 143   . .
    2 121   .   . .
    3  22 109   . .
    3  92   .   . .
    4  40  62   . .
    4  51  76   . .
    end
    
    gen long obsno = _n 
    reshape long child, i(obsno) j(which)
    drop obsno 
    drop if child == . 
    bysort id (child) : replace which = _n 
    reshape wide child, i(id) j(which)
    
    list 
    
         +----------------------------------------+
         | id   child1   child2   child3   child4 |
         |----------------------------------------|
      1. |  1      100      134      210      250 |
      2. |  2      111      121      143        . |
      3. |  3       22       92      109        . |
      4. |  4       40       51       62       76 |
         +----------------------------------------+

    Comment

    Working...
    X