Stata:根据组中的其他变量为组生成具有所有值(例如,不只是最大值或最小值)的新变量
我想为集团国家/地区(iso_o/iso_d)创建具有变量IndUpdate特征的新变量 到目前为止,我一直在键入:Stata:根据组中的其他变量为组生成具有所有值(例如,不只是最大值或最小值)的新变量,stata,Stata,我想为集团国家/地区(iso_o/iso_d)创建具有变量IndUpdate特征的新变量 到目前为止,我一直在键入: 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
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
中的值创建两个新变量,但如果有三个以上的变量,我无法得到解决方案。这是一张小桌子
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 . .
因此,对于这个小示例,我可以尝试使用区间,但是数据集非常大,因此我无法确定一个区间中有多少个值
关于这方面的另一种方法有什么提示吗?您可以
重塑
,然后合并
:
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
以删除只有缺失值的变量
但更大的问题是你为什么要这样做?我不清楚这一点。为什么(iso_d);那是打字错误吗?你想计算什么是你不能计算的?没有输入错误。我正在处理贸易流的面板数据。我还需要
iso_d
(指定国家)独立日期的信息。例如,如果您有1960年,国家Aiso_o
和Biso_d
。A独立4年(独立年=1956年)和B两年(独立年=1958年),四年和两年的虚拟将打开,以便观察殖民独立后几年的贸易影响。因此,我需要将indupdate
的信息传播给其他分组,分别是iso\u o
和iso\u d
。希望这能让事情变得更清楚!感谢Roberto的编辑和回答!这帮了大忙!解释原因:indepdate
是殖民地独立的一年iso_o
是原产国,iso_d
是贸易目的地国。我想在独立后的几年里制作一些假人来观察它们的效果。对于殖民地大都市,兄弟姐妹(同一个殖民者)和世界其他地方之间的贸易。在给出的数据中,indupdate
仅针对殖民地大都市贸易给出,而非其他分组。一个国家也可以被殖民不止一次。因此,这种方法对我来说是有意义的。希望这能说明目的!谢谢你的反馈。如果你觉得有用的话,你可以。这样做,您将提高我们的声誉。