Stata:根据组中的其他变量为组生成具有所有值(例如,不仅仅是最大值或最小值)的新变量

Stata: Generate new variable with all values (e.g. not just max or min) for a group based on other variable in the group

我想为组国家(iso_o/iso_d)创建具有独立变量特征的新变量。

到目前为止我一直在输入:

gen include=1 if heg_o != 1 
egen iso_o_indepdate1=min(indepdate * include), by(iso_o)
egen iso_o_indepdate2=max(indepdate * include), by(iso_o)
replace iso_o_indepdate2=. if iso_o_indepdate1==iso_o_indepdate2
drop include
*
gen include=1 if heg_d !=1 
egen iso_d_indepdate1=min(indepdate * include), by(iso_d)
egen iso_d_indepdate2=max(indepdate * include), by(iso_d)
replace iso_d_indepdate2=. if iso_d_indepdate1==iso_d_indepdate2
drop include

问题是我可以结合使用 min()max()indepdate 中的值创建两个新变量,但如果有超过三个我还没有能够得到解决方案。这里有个小table.

iso_o   group  indepdate   new1    new2    new3
FRA      1      1960       1960    1980    1999
FRA      1      1980       1960    1980    1999
FRA      1      1999       1960    1980    1999
FRA      1      .          1960    1980    1999
USA      2      1955       1955     .       .
USA      2      .          1955     .       .
USA      2      .          1955     .       .

所以对于这个小例子,我可以尝试使用间隔,但是数据集非常大,因此我无法确定一个间隔中有多少个值。

关于此的另一种方法有任何提示吗?

您可以 reshape 然后 merge:

clear all
set more off

*----- example data ---

input ///
str3 iso_o   group  indepdate   new1    new2    new3
FRA      1      1960       1960    1980    1999
FRA      1      1980       1960    1980    1999
FRA      1      1999       1960    1980    1999
FRA      1      .          1960    1980    1999
USA      2      1955       1955     .       .
USA      2      .          1955     .       .
USA      2      .          1955     .       .
end

drop new*

list, sepby(group)

tempfile orig
save "`orig'"

*----- what you want -----

bysort group (indepdate) : gen j = _n 

reshape wide indepdate, i(group) j(j)
keep group indepdate*

merge 1:m group using "`orig'", assert(match) nogenerate

// list
sort group indepdate
order iso_o group indepdate indepdate*
list, sepby(group)

请参阅 help dropmiss 删除仅具有缺失值的变量。

但更大的问题是你为什么要这样做?