Hi,

I am using STATA version 16 and would need some help in counting the maximum number of observation before and after a specific value in a column.


Array


As you can see in the image above, the column "pt_output" has values 1 and 2. "1" represents the star of a recession and "2" represents the end of a recession. I want to know what is the maximum number of quarters before the value "1" and after the value "1".


My code for calculating quarters before value "1":

. count if id < 21
20


. count if id < 36
35



Then I will see that 35 is the maximum number of observations before a specific value. Is there a more efficient way of doing this? There is a lot of looking at the data to find out which id to choose from before setting up the code. Any help would be appreciated. Thanks!