0
votes

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 xtset and replacing via F., but that didn't work as expected.

1

1 Answers

1
votes

I think the following is what you want, given that you have already created the term variable set to 1 if the string contains the term, and (I assume) 0 otherwise.

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

The by id (time), sort: ... command will run the egen separately for each id. The egen will find the maximum value of term for each id, so newterm will be 1 if any term is 1.