*-
https://bbs.pinggu.org/thread-1192417-1-1.html
*-你给出的 xx 不太对,这里生成的 yy 才是你想要的
clear
input ///
id year x xx
1 2005 1 1
1 2006 1 1
1 2007 1 1
2 2005 1 2
2 2006 1 2
2 2007 1 2
2 2008 1 2
2 2009 0 2
3 2005 1 1
3 2006 1 1
3 2007 1 1
3 2008 1 1
4 2007 0 0
4 2008 0 0
4 2009 0 0
5 2005 0 0
5 2006 0 0
5 2007 0 2
6 2007 0 2
6 2008 0 2
6 2009 1 2
end
bysort id: gen id_N = _N
bysort id: egen id_sum = sum(x)
gen yy = 2
replace yy = 1 if (id_sum==id_N)
replace yy = 0 if (id_sum==0)