The help for egen
is quite explicit:
"Explicit subscripting (using _N
and _n
), which is commonly used with generate
, should not be used with egen
"
The reason is that egen
often changes the sort order temporarily to do what it does, so subscripts will not necessarily mean what you think.
So you need to create your own growth variable first and then use egen
.
Simplifying your problem to one transaction for each city and week, and assuming no gaps in time within each panel and no missings, this should suffice:
bysort city_id (week) : gen tr_diff = tr_cw - tr_cw[_n-1]
by city_id: egen tr_speed = mean(tr_diff)
egen tag = tag(city_id)
l city tr_speed if tag
You will need to complicate that for multiple transactions. You don't make clear how they are to be combined.