Dear Stata Users,

Can you please help me identify the following setting. I need to create a variable “Event” that equals one for years that have a positive “contract_amount” (i.e. > 0) and where previous (at least) two years had “0” “contract_amount” value. And variable “First_time” is equal to “1” for year when a firm first time had “Event” = 1. For instance, Firm A is expected to have “Event” equal to 1 in 2004 and in 2009 and “First_time” equal to 1 in 2004. Thank you.
Firm Year contract_amount Event First_time
A 2000 0
A 2001 334
A 2002 0
A 2003 0
A 2004 443 1 1
A 2005 4556
A 2006 0
A 2007 0
A 2008 0
A 2009 546 1
A 2010 45



Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str6 gvkey double(year contract_amount)
"001004" 2004 108838862.96
"001004" 2005 144547458.15
"001004" 2006 145560310.39
"001004" 2007  90250333.12
"001004" 2008  98392104.99
"001004" 2009 120356352.16
"001004" 2010  98381518.73
"001004" 2011  93699166.77
"001004" 2012  45898137.34
"001004" 2013  46369762.64
"001004" 2014  52028095.04
"001004" 2015  22543211.43
"001004" 2016   54150447.8
"001004" 2017  64161022.63
"001013" 2004            0
"001013" 2005            0
"001013" 2006            0
"001013" 2007            0
"001013" 2008            0
"001013" 2009    121646.12
"001013" 2010      66006.4
"001021" 2004         3195
"001021" 2005        16095
"001021" 2006      3819.66
"001021" 2007       147.91
"001021" 2008       870.14
"001072" 2004            0
"001072" 2005            0
"001072" 2006            0
"001072" 2007            0
"001072" 2008            0
"001072" 2009            0
"001072" 2010            0
"001072" 2011      3185000
"001072" 2012       597174
"001072" 2013            0
"001072" 2014            0
"001072" 2015       749923
"001072" 2016       485827
"001072" 2017            0
"001078" 2004  32224229.67
"001078" 2005  50964163.98
"001078" 2006  57310508.36
"001078" 2007  71746259.77
"001078" 2008  87347209.27
"001078" 2009  84032745.23
"001078" 2010  90233395.46
"001078" 2011  87675259.32
"001078" 2012  62336431.01
"001078" 2013  48312108.91
"001078" 2014  49995958.66
"001078" 2015  60302643.77
"001078" 2016  62174470.55
"001078" 2017  66338352.38
"001094" 2004            0
"001094" 2005            0
"001094" 2006            0
"001094" 2007         4703
"001094" 2008            9
"001094" 2009            0
"001094" 2010            0
"001094" 2011            0
"001094" 2012            0
"001094" 2013            0
"001094" 2014            0
"001094" 2015            0
"001094" 2016            0
"001094" 2017            0
"001165" 2004            0
"001165" 2005      2994577
"001165" 2006            0
"001173" 2004      5232768
"001173" 2005      3946948
"001173" 2006   2528104.28
"001173" 2007    2035234.7
"001173" 2008   4354723.23
"001173" 2009   2948959.23
"001173" 2010   1122818.34
"001173" 2011   6381071.48
"001173" 2012   7332599.66
"001209" 2004   6117080.56
"001209" 2005  21480632.08
"001209" 2006  41250789.59
"001209" 2007  26297780.55
"001209" 2008  27654172.59
"001209" 2009   27991307.5
"001209" 2010   20605167.7
"001209" 2011   21875012.1
"001209" 2012  19629078.07
"001209" 2013  10854180.47
"001209" 2014   9191042.13
"001209" 2015  16736443.87
"001209" 2016  20090921.03
"001209" 2017  19709812.74
"001239" 2004      4333603
"001239" 2005      8971293
"001239" 2006  14851484.02
"001239" 2007   8347345.78
"001239" 2008  14026475.71
"001239" 2009  13399159.06
end