Hello,
I have an unbalanced panel dataset and I need to create lagged (t-3, t-2 and t-1) and lead variables (t+1, t+2, and t+3).
One of my variables of interest is "interlocked_firm", for which I ran the below code:
gen lag1 = interlocked_firm[_n-1]
However, it took into consideration the previous observation regardless of it belonging to another companyid.
1. What is the right command to use to generate lagged variables?
2. What is the right command to use to generate lead variables?
This is the output with the WRONG lag1 variable:
This is the -dataex- output for Year, companyid and the interlocked_firm (the variable I want to lag and to lead).
Thank you!
I have an unbalanced panel dataset and I need to create lagged (t-3, t-2 and t-1) and lead variables (t+1, t+2, and t+3).
One of my variables of interest is "interlocked_firm", for which I ran the below code:
gen lag1 = interlocked_firm[_n-1]
However, it took into consideration the previous observation regardless of it belonging to another companyid.
1. What is the right command to use to generate lagged variables?
2. What is the right command to use to generate lead variables?
This is the output with the WRONG lag1 variable:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input int Year long companyid float(interlocked_firm lag1) 2009 33 0 . 2010 33 0 0 2011 33 0 0 2012 33 0 0 2013 33 0 0 2014 33 1 0 2015 33 1 1 2016 33 1 1 2017 33 1 1 2018 33 1 1 2019 33 1 1 2007 34 1 1 2008 34 1 1 2009 34 1 1 2010 34 1 1 2011 34 1 1 2012 34 1 1 2013 34 1 1 2014 34 1 1 2015 34 1 1 2016 34 1 1 2017 34 1 1 2018 34 1 1 2019 34 1 1 2015 55 0 1 2016 55 0 0 2017 55 0 0 2018 55 0 0 2019 55 0 0 2014 177 1 0 2015 177 1 1 2007 217 1 1 2008 217 1 1 2009 217 1 1 2010 217 1 1 2011 217 1 1 2012 217 1 1 2013 217 1 1 2014 217 1 1 2015 217 1 1 2016 217 1 1 2017 217 1 1 2018 217 1 1 2019 217 1 1 2007 282 1 1 2008 282 1 1 2009 282 1 1 2010 282 1 1 2011 282 1 1 2012 282 1 1 2013 282 1 1 2014 282 1 1 2015 282 1 1 2016 282 1 1 2017 282 1 1 2018 282 1 1 2019 282 1 1 2009 304 1 1 2010 304 1 1 2011 304 1 1 2012 304 1 1 2013 304 1 1 2014 304 1 1 2015 304 1 1 2016 304 1 1 2017 304 1 1 2018 304 1 1 2019 304 1 1 2007 335 1 1 2008 335 1 1 2009 335 1 1 2010 335 1 1 2011 335 1 1 2012 335 1 1 2013 335 1 1 2014 335 1 1 2015 335 1 1 2016 335 1 1 2017 335 1 1 2018 335 1 1 2019 335 1 1 2008 383 1 1 2009 383 1 1 2010 383 1 1 2011 383 1 1 2012 383 1 1 2013 383 1 1 2014 383 1 1 2015 383 1 1 2016 383 1 1 2017 383 1 1 2018 383 1 1 2019 383 1 1 2015 441 1 1 2016 441 1 1 2017 441 1 1 2018 441 1 1 2019 441 1 1 2008 606 1 1 2009 606 1 1 end
This is the -dataex- output for Year, companyid and the interlocked_firm (the variable I want to lag and to lead).
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input int Year long companyid float interlocked_firm 2009 33 0 2010 33 0 2011 33 0 2012 33 0 2013 33 0 2014 33 1 2015 33 1 2016 33 1 2017 33 1 2018 33 1 2019 33 1 2007 34 1 2008 34 1 2009 34 1 2010 34 1 2011 34 1 2012 34 1 2013 34 1 2014 34 1 2015 34 1 2016 34 1 2017 34 1 2018 34 1 2019 34 1 2015 55 0 2016 55 0 2017 55 0 2018 55 0 2019 55 0 2014 177 1 2015 177 1 2007 217 1 2008 217 1 2009 217 1 2010 217 1 2011 217 1 2012 217 1 2013 217 1 2014 217 1 2015 217 1 2016 217 1 2017 217 1 2018 217 1 2019 217 1 2007 282 1 2008 282 1 2009 282 1 2010 282 1 2011 282 1 2012 282 1 2013 282 1 2014 282 1 2015 282 1 2016 282 1 2017 282 1 2018 282 1 2019 282 1 2009 304 1 2010 304 1 2011 304 1 2012 304 1 2013 304 1 2014 304 1 2015 304 1 2016 304 1 2017 304 1 2018 304 1 2019 304 1 2007 335 1 2008 335 1 2009 335 1 2010 335 1 2011 335 1 2012 335 1 2013 335 1 2014 335 1 2015 335 1 2016 335 1 2017 335 1 2018 335 1 2019 335 1 2008 383 1 2009 383 1 2010 383 1 2011 383 1 2012 383 1 2013 383 1 2014 383 1 2015 383 1 2016 383 1 2017 383 1 2018 383 1 2019 383 1 2015 441 1 2016 441 1 2017 441 1 2018 441 1 2019 441 1 2008 606 1 2009 606 1 end
Thank you!
Comment