0
votes

I have a panel data set with product purchases identified for unique household ids and need to generate a dummy variable "brand loyally" that will equal to 1 if the same brand was purchased by the household in the previous period. My periods are not equally timed. For some households it can be 1 week, for others - 10 weeks. Does this code sound about right?:

panid - unique household id
l5 - brand name
loy - wanted dummy


bysort panid week: egen loy=1 if l5=l5[_n-1]
1
You need to xtset your data and use lag operators.Dimitriy V. Masterov

1 Answers

3
votes

I assume that the unit of the variable week is weeks. In that case you can type

tsset panid week
by panid: gen byte loy = ( L.l5 == l5 ) if !missing(L.l5,l5) & _n > 1