Dear users, hope everyone is well.

i want to assign value of 3 to the variable "dum" when the current year "survival" not equal zero and it become zero in the next period.

i tried the following command but it assign the value only in the next period (no matter the next period "survival" is equal zero or not).



Code:
bys stkcd: replace dum = 3 if survival != 0 & F.survival == 0
is there any solution command for this?

Code:
* Example generated by -dataex-. To install: ssc install dataex clear input double stkcd float(survival year dum) 1 0 1990 2 1 0 1991 2 1 0 1992 2 1 0 1993 2 1 0 1994 2 1 0 1995 2 1 0 1996 2 1 0 1997 2 1 0 1998 2 1 0 1999 2 1 0 2000 2 1 0 2001 2 1 0 2002 2 1 0 2003 2 1 0 2004 2 1 0 2005 2 1 0 2006 2 1 0 2007 2 1 0 2008 2 1 0 2009 2 1 0 2010 2 1 0 2011 2 1 0 2012 2 1 0 2013 2 1 0 2014 2 1 0 2015 2 1 0 2016 2 1 0 2017 2 1 0 2018 2 1 0 2019 2 1 0 2020 2 2 0 1991 2 2 0 1992 2 2 0 1993 2 2 0 1994 2 2 0 1995 2 2 0 1996 2 2 0 1997 2 2 0 1998 2 2 0 1999 2 2 0 2000 2 2 0 2001 2 2 0 2002 2 2 0 2003 2 2 0 2004 2 2 0 2005 2 2 0 2006 2 2 0 2007 2 2 0 2008 2 2 0 2009 2 2 0 2010 2 2 0 2011 2 2 0 2012 2 2 0 2013 2 2 0 2014 2 2 0 2015 2 2 0 2016 2 2 0 2017 2 2 0 2018 2 2 0 2019 2 2 0 2020 2 3 0 1991 2 3 0 1992 2 3 0 1993 2 3 0 1994 2 3 0 1995 2 3 0 1996 2 3 0 1997 2 3 0 1998 2 3 0 1999 2 3 0 2000 2 3 0 2001 2 4 0 1991 1 4 0 1992 1 4 0 1993 1 4 0 1994 1 4 0 1995 1 4 0 1996 1 4 0 1997 1 4 0 1998 1 4 0 1999 1 4 0 2000 1 4 0 2001 1 4 0 2002 1 4 0 2003 1 4 0 2004 1 4 0 2005 1 4 0 2006 1 4 598120 2007 . 4 64400 2008 3 4 0 2009 1 4 0 2010 1 4 0 2011 1 4 0 2012 1 4 0 2013 1 4 0 2014 1 4 53908.8 2015 . 4 65308.84 2016 . 4 4000 2017 . 4 7908000 2018 3 end