Dear All,
I have an individual level panel data with 108 monthly waves.

Code:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input double(ID Month_count) float y
25316   1         0
25316   2         0
25316   3         0
25316   4         0
25316   5         0
25316   6         0
25316   7         0
25316   8         0
25316   9         0
25316  10         0
25316  11         0
25316  12         0
25316  13        10
25316  14 4.3396225
25316  15         5
25316  16         0
25316  17         5
25316  18         5
25316  19         0
25316  20         0
25316  21       200
25316  22         0
25316  23       800
25316  24      1270
25316  25  846.6666
25316  26  423.3333
25316  27       800
25316  28      1620
25316  29 1334.4578
25316  30 1652.1237
25316  31  1935.835
25316  32  1455.876
25316  33  975.9167
25316  34 1295.9578
25316  35  770.5695
25316  36 1045.1812
25316  37  495.0858
25316  38         0
25316  39       800
25316  40  228.5714
25316  41         0
25316  42       950
25316  43  316.6667
25316  44         0
25316  45       950
25316  46  1597.222
25316  47 2273.0903
25316  48   2953.94
25316  49  3411.617
25316  50 2878.5515
25316  51 2345.4863
25316  52 2912.4214
25316  53  3405.667
25316  54  3350.731
25316  55 3806.3596
25316  56  4230.675
25316  57 4133.3105
25316  58 4551.9956
25316  59 3848.0786
25316  60  3144.162
25316  61  2440.245
25316  62 1736.3282
25316  63 1032.4114
25316  64  328.4945
25316  65         0
25316  66         0
25316  67         0
25316  68       225
25316  69       645
25316  70 528.29785
25316  71 191.08646
25316  72         0
25316  73         0
25316  74         0
25316  75         0
25316  76         0
25316  77         0
25316  78         0
25316  79         0
25316  80         0
25316  81         0
25316  82         0
25316  83         0
25316  84         0
25316  85         0
25316  86         0
25316  87         0
25316  88         0
25316  89         0
25316  90         0
25316  91         0
25316  92         0
25316  93         0
25316  94         0
25316  95         0
25316  96         0
25316  97         0
25316  98         0
25316  99         0
25316 100         0
end
For each ID I want to create episodes that includes periods of 3+ months where y>0. For instance, in the example above, in Months 13-15 is the first episode as y>0 in 3 consecutive months. In months 17-18 y>0 but this spell only last 2 months (<3 months) and therefore does not count as an episode. Similarly, in month 21 y>0 but its only 1 month and should not be considered an episode. Month 23-37 is the second episode, 24-64 is third episode, and 68-71 is the fourth. It seems I should use some combination of egen or rangestat but I am struggling to specify a rule that will take care of this. I will be grateful for your guidance.
Sincerely,
Sumedha.