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]