Hi

I have a very large panel dataset (50 time periods, 30m panel IDs) and I have a variable (called event) that is a combination of 0 and 1. I need to create another variable, timesinceevent, that counts the number of time periods since that variable was last equal to 1. An example is given in the table below,

Please could someone suggest how I could calculate the variable timesinceevent please?

Thanks
Rob

panel_ID timevar event timesinceevent
1 1 0 .
1 2 0 .
1 3 0 .
1 4 1 0
1 5 0 1
1 6 0 2
1 7 0 3
1 8 0 4
1 9 0 5
1 10 0 6
2 1 0 .
2 2 1 0
2 3 0 1
2 4 0 2
2 5 1 0
2 6 0 1
2 7 0 2
2 8 1 0
2 9 0 1