It is tough to explain but i will try my best.
"Surge" is the dummy variable i created for capital flow surge episodes which lasts at least 2 quarters or more (so i removed single quarter episodes).

I want to count it as surge=1 only if during the period there is at least one episode of surge that goes above 2 std level ("surge_2sd=1").

So i would ideally count only those that are marked in yellow and replace the variable "surge" i.e. 2002q1-2002q4 to zero since there is no single episode during that period that goes above 2sd.

Could anyone help with the code?