define variable if panel data takes a value once

时间:2016-02-12 21:51:40

标签: stata panel-data

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.

1 个答案:

答案 0 :(得分:1)

我认为以下是你想要的,假设你已经创建了term变量,如果字符串包含该项,则设置为1,否则(我假设)为0。

by id (time), sort: egen newterm = max(term)
replace term = newterm
drop newterm

by id (time), sort: ...命令将为每个egen单独运行idegen会为每个term找到id的最大值,因此如果newterm为1,term将为1。