Dear forum members,

my data is a panel structured as follows: id change nid1 nid2 nid3 nid4 nid5
For each id, i would like to create a variable that determines how many changes happened in the nids in the previous year. I tried:
egen nn_changes = count(change[_n-1]) if change[_n-1]==1, by( nid1 nid2 nid3 nid4 nid5) but it doesnt seem to work. Is a loop required here?

Your help is appreciated.

Kind regards