Announcement

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

  • Calculating number of children with same mother and father

    Hello

    I am trying to calculate the number of siblings in a family with the criterion for being siblings that the children share minimum one parent.

    I have generated two variables counting the number of children with the same mother (mother_sib) and father (father_sib) and now I want to calculate the number of siblings sharing both the same mother and father (parent_sib).
    Code:
    bysort mother_id:gen byte father_sib=_N
    
    bysort father_id:gen byte father_sib=_N
    Eventually, I want to add mother_sib and father_sib together and subtract the number of children sharing both the same father and mother to avoid adding the same children twice (mother_sib + father_sib - parents_sib). Then the number of children sharing minimum one parent should appear. But I still need to figure out how to calculate the number of children sharing both the same father and mother - any ideas?

    Code:
    input (mother_id father_id morther_sib father_sib)
      22126   2230 1 1
       2256   2206 3 3
       2256   2206 3 3
       2256   2206 3 3
    2278868   2259 1 1
          .   8532 . 1
      22472   2272 3 3
      22472   2272 3 3
      22472   2272 3 3
    2219586   2251 1 1
          .  22615 . 1
    2206309  22428 1 1
    2232781  22450 2 1
    2252360  22780 2 2
    2252360  22780 2 2
    Thank you!
    Last edited by Line Bachmann; 23 Aug 2018, 02:50.

  • #2
    I changed some of your data to provide a better test for the code I show.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(mother_id father_id)
      22126  2230
       2256  2206
       2256  2206
       2256     .
          1  2206
    2278868  2259
          .  8532
      22472  2272
      22472  2272
      22472  2272
    2219586  2251
          2     .
    2206309 22428
    2232781 22450
    2252360 22780
    2252360 22780
          .    
    end
    bysort father_id: generate f_sib = _N if !missing(father_id)
    bysort mother_id: generate m_sib = _N if !missing(mother_id)
    bysort mother_id father_id: generate fm_sib = _N if !missing(mother_id,father_id)
    list, clean noobs
    Code:
    . list, clean noobs
    
        mother~d   father~d   f_sib   m_sib   fm_sib  
               1       2206       3       1        1  
               2          .       .       1        .  
            2256       2206       3       3        2  
            2256       2206       3       3        2  
            2256          .       .       3        .  
           22126       2230       1       1        1  
           22472       2272       3       3        3  
           22472       2272       3       3        3  
           22472       2272       3       3        3  
         2206309      22428       1       1        1  
         2219586       2251       1       1        1  
         2232781      22450       1       1        1  
         2252360      22780       2       2        2  
         2252360      22780       2       2        2  
         2278868       2259       1       1        1  
               .       8532       1       .        .

    Comment


    • #3
      Works just perfect - thank you so much, William!

      Comment

      Working...
      X