I have a panel data which I want to estimate the difference between the values in a variable and the first observation of that panel. the data is shown below
How do I create a variable that will subtract the variables in prd from the first value of the panel to appear like the table below
So the variable dif for firm 1 is generated by subtracting the prd in 2004 from the prd values of 2004-2008. thus making the first observation of each panel in he dif variable zero. The data is sorted by firm and year.
Code:
firm year prd 1 2004 0.6995373 1 2005 0.7082548 1 2007 0.7548516 1 2008 0.7217189 2 2004 0.637614 2 2005 0.666153 2 2006 0.6679261 2 2007 0.6596823 2 2008 0.6664945 2 2009 0.6481165 2 2010 0.6474085 2 2011 0.6173385 2 2012 0.6334211 2 2013 0.6399546 2 2014 0.6296806 2 2015 0.6324123 2 2016 0.6398941 2 2017 0.6287656 2 2018 0.6188431 2 2020 0.6216881 3 2005 0.6463507 3 2006 0.643418 3 2007 0.6430452 3 2008 0.6433605 3 2009 0.6420132 3 2012 0.6400115 3 2013 0.6173558 3 2014 0.6145701 3 2020 0.6805519 4 2005 0.6595924 4 2006 0.6612933 4 2018 0.6601894
So the variable dif for firm 1 is generated by subtracting the prd in 2004 from the prd values of 2004-2008. thus making the first observation of each panel in he dif variable zero. The data is sorted by firm and year.
Code:
firm year prd dif 1 2004 0.6995373 0 1 2005 0.7082548 0.0087175 1 2007 0.7548516 0.0553143 1 2008 0.7217189 0.0221816 2 2004 0.637614 0 2 2005 0.666153 0.028539 2 2006 0.6679261 0.0303121 2 2007 0.6596823 0.0220683 2 2008 0.6664945 0.0288805 2 2009 0.6481165 0.0105025 2 2010 0.6474085 0.0097945 2 2011 0.6173385 -0.0202755 2 2012 0.6334211 -0.0041929 2 2013 0.6399546 0.0023406 2 2014 0.6296806 -0.0079334 2 2015 0.6324123 -0.0052017 2 2016 0.6398941 0.0022801 2 2017 0.6287656 -0.0088484 2 2018 0.6188431 -0.0187709 2 2020 0.6216881 -0.0159259 3 2005 0.6463507 0 3 2006 0.643418 -0.0029327 3 2007 0.6430452 -0.0033055 3 2008 0.6433605 -0.0029902 3 2009 0.6420132 -0.0043375 3 2012 0.6400115 -0.0063392 3 2013 0.6173558 -0.0289949 3 2014 0.6145701 -0.0317806 3 2020 0.6805519 0.0342012 4 2005 0.6595924 0 4 2006 0.6612933 0.0017009 4 2018 0.6601894 0.000597
Comment