Hi, I have a panel data set and try to generate sequence numbers by group but the numbers starting from 0 using a if condition.
To be specific, I'll go straight to an example data set.

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(id time change) byte period float(nmissing desired_period)
100 1 0 -1 2 -2
100 2 0  0 2 -1
100 3 1  1 2  0
100 4 1  2 2  1
200 1 0  0 1 -1
200 2 1  1 1  0
200 3 1  2 1  1
200 4 1  3 1  2
200 5 1  4 1  3
300 2 0 -3 4 -4
300 3 0 -2 4 -3
300 4 0 -1 4 -2
300 5 0  0 4 -1
end
This is a (unbalanced) panel data set.
I want to generate desired_period variable which looks like that: when the value of change variable becomes from 0 to 1 for id 100 (which is time=3), I want the value of desired period variable to be 0. After that the value increases by 1 (e.g. 0,1,2,3,...). In a similar way, the values of desired period before time=3 go backward (-1, -2,-3,...).

I was searching on the internet and found a code that gave me somewhat similar output but the only difference is the value doesn't start from 0 but from 1 (e.g. 1,2,3,...).
The code I've found is below:
Code:
bys id, sort: egen period = seq() if change >= 1
by id, sort: egen nmissing = total(missing(period))
by id, sort: replace period = -nmissing + _n if missing(period)
I searched how I can start the sequence integers from 0 but failed to find a good answer.
I found that I can use option f() with seq() which I can specify the starting point.
I tried something like this but had an error message, which I assume that options within seq() and if condition can't go together?
Code:
bys id, sort: egen desired_period= seq() f(0) if change >= 1
bys id, sort: egen desired_period= seq(), f(0) if change >= 1
I would appreciate if someone could help me fixing the code to get the 'desired_period' variable.
Thank you.