Announcement

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

  • Generate variables based on siblings

    I have data where children within a family (i.e., siblings) can be connected via their shared mother's ID. So in the data below, children 201 and 202 are siblings and share a mother. Same with 301 and 302.

    I then have a series of variables about one's own experiences (cjc and alc being two example vars below). What I'm hoping to do is then create a variable that indicates if one's *sibling* has experienced something. So if child 202 experienced cjc_SELF in 2014, I'd then want to create a var called cjc_SIB that =1 for child 201 in 2014. Does that make sense? My initial attempts with -bysort (max)- did not work out the way I was hoping.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long child_id int(mother_id year) float(cjc_SELF alc_SELF)
    201 2 2010 0 .
    201 2 2012 0 .
    201 2 2014 0 .
    201 2 2016 0 .
    201 2 2018 0 .
    201 2 2020 0 .
    202 2 2010 0 .
    202 2 2012 0 0
    202 2 2014 1 0
    202 2 2016 0 0
    202 2 2018 0 0
    202 2 2020 0 0
    301 3 2010 0 .
    301 3 2012 0 1
    301 3 2014 0 0
    301 3 2016 0 0
    301 3 2018 1 0
    301 3 2020 0 0
    302 3 2010 0 .
    302 3 2012 0 0
    302 3 2014 0 1
    302 3 2016 0 0
    302 3 2018 1 1
    302 3 2020 0 .
    303 3 2010 0 0
    303 3 2012 0 1
    303 3 2014 0 0
    303 3 2016 0 0
    303 3 2018 0 .
    end
    Last edited by Garrett Baker; 18 Sep 2024, 09:07.

  • #2
    You could use some of the tricks discussed at Stata | FAQ: Creating variables recording properties of the other members of a group

    Otherwise rangestat from SSC may help:

    Code:
     
     * Example generated by -dataex-. For more info, type help dataex clear input long child_id int(mother_id year) float(cjc_SELF alc_SELF) 201 2 2000 0 . 201 2 2002 0 . 201 2 2004 0 . 201 2 2006 0 . 201 2 2008 0 . 201 2 2010 0 . 201 2 2012 0 . 201 2 2014 0 . 201 2 2016 0 . 201 2 2018 0 . 201 2 2020 0 . 202 2 2000 0 . 202 2 2002 0 . 202 2 2004 0 . 202 2 2006 0 . 202 2 2008 0 . 202 2 2010 0 . 202 2 2012 0 . 202 2 2014 1 . 202 2 2016 0 . 202 2 2018 0 . 202 2 2020 0 0 301 3 2000 0 0 301 3 2002 0 0 301 3 2004 0 1 301 3 2006 0 0 301 3 2008 0 . 301 3 2010 0 . 301 3 2012 0 . 301 3 2014 0 0 301 3 2016 0 0 301 3 2018 0 0 301 3 2020 0 . 302 3 2000 1 . 302 3 2002 0 . 302 3 2004 0 . 302 3 2006 0 . 302 3 2008 1 . 302 3 2010 0 . 302 3 2012 0 . 302 3 2014 0 . 302 3 2016 0 . 302 3 2018 0 . 302 3 2020 0 . 303 3 2000 0 . 303 3 2002 0 0 303 3 2004 0 1 303 3 2006 0 0 303 3 2008 0 0 303 3 2010 0 0 303 3 2012 0 1 303 3 2014 0 0 303 3 2016 0 . 303 3 2018 0 . end  sort mother_id year   rangestat (max) cjc_SIB=cjc_SELF, excludeself interval(year 0 0) by(mother_id)   list, sepby(mother_id year)        +------------------------------------------------------------+      | child_id   mother~d   year   cjc_SELF   alc_SELF   cjc_SIB |      |------------------------------------------------------------|   1. |      202          2   2000          0          .         0 |   2. |      201          2   2000          0          .         0 |      |------------------------------------------------------------|   3. |      202          2   2002          0          .         0 |   4. |      201          2   2002          0          .         0 |      |------------------------------------------------------------|   5. |      202          2   2004          0          .         0 |   6. |      201          2   2004          0          .         0 |      |------------------------------------------------------------|   7. |      201          2   2006          0          .         0 |   8. |      202          2   2006          0          .         0 |      |------------------------------------------------------------|   9. |      202          2   2008          0          .         0 |  10. |      201          2   2008          0          .         0 |      |------------------------------------------------------------|  11. |      202          2   2010          0          .         0 |  12. |      201          2   2010          0          .         0 |      |------------------------------------------------------------|  13. |      202          2   2012          0          .         0 |  14. |      201          2   2012          0          .         0 |      |------------------------------------------------------------|  15. |      202          2   2014          1          .         0 |  16. |      201          2   2014          0          .         1 |      |------------------------------------------------------------|  17. |      202          2   2016          0          .         0 |  18. |      201          2   2016          0          .         0 |      |------------------------------------------------------------|  19. |      202          2   2018          0          .         0 |  20. |      201          2   2018          0          .         0 |      |------------------------------------------------------------|  21. |      201          2   2020          0          .         0 |  22. |      202          2   2020          0          0         0 |      |------------------------------------------------------------|  23. |      301          3   2000          0          0         1 |  24. |      303          3   2000          0          .         1 |  25. |      302          3   2000          1          .         0 |      |------------------------------------------------------------|  26. |      302          3   2002          0          .         0 |  27. |      301          3   2002          0          0         0 |  28. |      303          3   2002          0          0         0 |      |------------------------------------------------------------|  29. |      301          3   2004          0          1         0 |  30. |      303          3   2004          0          1         0 |  31. |      302          3   2004          0          .         0 |      |------------------------------------------------------------|  32. |      301          3   2006          0          0         0 |  33. |      303          3   2006          0          0         0 |  34. |      302          3   2006          0          .         0 |      |------------------------------------------------------------|  35. |      301          3   2008          0          .         1 |  36. |      303          3   2008          0          0         1 |  37. |      302          3   2008          1          .         0 |      |------------------------------------------------------------|  38. |      301          3   2010          0          .         0 |  39. |      302          3   2010          0          .         0 |  40. |      303          3   2010          0          0         0 |      |------------------------------------------------------------|  41. |      302          3   2012          0          .         0 |  42. |      303          3   2012          0          1         0 |  43. |      301          3   2012          0          .         0 |      |------------------------------------------------------------|  44. |      301          3   2014          0          0         0 |  45. |      303          3   2014          0          0         0 |  46. |      302          3   2014          0          .         0 |      |------------------------------------------------------------|  47. |      302          3   2016          0          .         0 |  48. |      303          3   2016          0          .         0 |  49. |      301          3   2016          0          0         0 |      |------------------------------------------------------------|  50. |      301          3   2018          0          0         0 |  51. |      302          3   2018          0          .         0 |  52. |      303          3   2018          0          .         0 |      |------------------------------------------------------------|  53. |      302          3   2020          0          .         0 |  54. |      301          3   2020          0          .         0 |      +------------------------------------------------------------+

    Comment

    Working...
    X