我想link几个变量到stata中的一个ID

I want to link several variables to one ID in stata

我是 Stata 的新手,我正在努力解决以下问题:

我有这个table:

我想要这样的东西:

这是一种可能的解决方案:

clear

input id_household child mother
1781 1 0
1781 0 0
1781 0 0
1781 0 1
1784 0 1
1784 0 0 
1786 0 1
1786 0 0
1786 0 0
end

bysort id_household: egen childcount = sum(child)
bysort id_household: egen mothercount = sum(mother)

contract id_household childcount mothercount

gen     mother_with_child = 0
replace mother_with_child = 1 if childcount >= 1 & mothercount >= 1

list id_household mother_with_child

     +---------------------+
     | id_hou~d   mother~d |
     |---------------------|
  1. |     1781          1 |
  2. |     1784          0 |
  3. |     1786          0 |
     +---------------------+

请注意,这将破坏您在原始数据集中拥有的任何其他数据(从您的示例中不清楚您是否拥有任何其他数据)。您可能需要将我的回答结果合并回您的原始数据集中。

这是一个不需要将结果合并到主数据集的解决方案:

input id_household child mother
1781 1 0
1781 0 0
1781 0 0
1781 0 1
1784 0 1
1784 0 0 
1786 0 1
1786 0 0
1786 0 0
end

* if you want to add the flag to each row do these
egen mother_in_house = sum(mother), by(id_household) // Sums the number of mother flags in the house
egen children_in_houses = sum(child), by(id_household) // Sums the number of children flags
egen mother_with_child = max(children_in_houses & mother_in_house), by(id_household) // = 1 if both mother count and child count are > 0

* Afterwards if you only care about household ID and the mother_with_child flag
keep id_household mother_with_child
duplicates drop 

我感觉您的数据比您的屏幕截图向右扩展得更远,因此可能需要相应地调整解决方案。