I have a panel data set identified by an id variable and one specific string variable with different values for each time period (weekly). Not every id is represented in every week (new can come and older can vanish).
I created a dummy when this variable contains a specific term, but it only captures the single appearance in a week. What I would like to have is that each id has a specific dummy that indicates whether the term is contained in the string variable in at least one week's occurrence. So in case in week 34 id x contains the term, i'd like to have a dummy for all the other weeks as well, that shows a "1", as the term once was contained for id x
I tried formatting as an var res1 = teamHistories.Where(th => th.TeamName.ToLower() == "xxx")
.GroupBy(th=>th.Date)
.SelectMany (grp1 => grp1.GroupBy (th => th.Event), (grp1, grp2) => new {grp1 = grp1, grp2 = grp2})
.GroupBy (temp0 => temp0.grp1.Key, temp0 => temp0.grp2);
and replacing via xtset
, but that didn't work as expected.
答案 0 :(得分:1)
我认为以下是你想要的,假设你已经创建了term
变量,如果字符串包含该项,则设置为1,否则(我假设)为0。
by id (time), sort: egen newterm = max(term)
replace term = newterm
drop newterm
by id (time), sort: ...
命令将为每个egen
单独运行id
。 egen
会为每个term
找到id
的最大值,因此如果newterm
为1,term
将为1。