Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using panel Id to generate difference between the observations of a variable

    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

    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
    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    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

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte firm int year float prd
    1 2004 .6995373
    1 2005 .7082548
    1 2007 .7548516
    1 2008 .7217189
    2 2004  .637614
    2 2005  .666153
    2 2006 .6679261
    2 2007 .6596823
    2 2008 .6664945
    2 2009 .6481165
    2 2010 .6474085
    2 2011 .6173385
    2 2012 .6334211
    2 2013 .6399546
    2 2014 .6296806
    2 2015 .6324123
    2 2016 .6398941
    2 2017 .6287656
    2 2018 .6188431
    2 2020 .6216881
    3 2005 .6463507
    3 2006  .643418
    3 2007 .6430452
    3 2008 .6433605
    3 2009 .6420132
    3 2012 .6400115
    3 2013 .6173558
    3 2014 .6145701
    3 2020 .6805519
    4 2005 .6595924
    4 2006 .6612933
    4 2018 .6601894
    end
    
    by firm (year), sort: gen dif = prd - prd[1]
    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      @Clyde Thank you. I didn't know about the dataex. I will do as you have suggested in my subsequent pots.

      Comment

      Working...
      X