Hi

I am working with panel data and I want to generate the variable "polstability" which measures the polical stability of countries. It should be based on my variable polity, which is given in my dataset. More precisely, polstabiltiy should be based on a difference between the values of polity. That is, if there occurred a change of more than 3 points in the last 3 years, then the variable polstability should give a value of 0. On the other hand, if such a change does not occur, the variable polstability should add1 to the value in the previous year. I hope the idea becomes more clear with this example (with fictional data):
country ccode year polity polstability
US 1 2000 19 13
US 1 2001 19 14
US 1 2002 18 15
US 1 2003 17 16
US 1 2004 19 17
Albania 2 2000 9 8
Albania 2 2001 9 9
Albania 2 2002 9 10
Albania 2 2003 3 0
Albania 2 2004 4 1
Albania 2 2005 5 2
Albania 2 2006 3 3
Albania 2 2007 1 0
Albania 2 2008 1 1
The US are politically stable as their polity score is between 17 and 19 over the whole time period. In contrast, Albania is not politically stable over the whole time. between 2000 and 2002 it is stable in the polity score but then there is a sharp drop in the variable. Hence, the polstabiltiy value starts again with 0. Until 2006 Albania is again pretty stable and thus the polstability value goes on counting. in 2007, the change in the last 3 years is larger than 3 points so the polstability score should go back to 0 and should start adding 1 afterwards again. Somehow, the polstability value thus counts the number of years since a country had a severe political change.

I do not know, how to generate such a variable & I hope someone can give me advice.

As some of you might have noticed, my variable follows the logic of the durable variable in the PolityIV dataset. I know, I could try to use their variable and merge it with my dataset but I rather want to understand how to do it by myself in order to improve my STATA skills for the future.

Thanks!