Announcement

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

  • Panel Data Generating mean from two years

    Hi!

    I have a panel dataset and would like to generate a new variable including the mean value of the current year and the following year for my emission values.
    Does anyone have an idea which command I can use for this?

    Thanks a lot!

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str10 ds_code double(year ClimateTotalEmissions)
    "130062" 2010                  .
    "130062" 2011                  .
    "130062" 2012 1531518.2661224662
    "130062" 2013        1547830.295
    "130062" 2014        2477747.068
    "130062" 2015         2457843.15
    "130062" 2016          2724674.1
    "130062" 2017            1130185
    "130062" 2018        1612554.428
    "130062" 2019            4500000
    "130062" 2020            5176000
    "130298" 2004                  .
    "130298" 2005                  .
    "130298" 2006                  .
    "130298" 2007                  .
    "130298" 2008                  .
    "130298" 2009                  .
    "130298" 2010                  .
    "130298" 2011                  .
    "130298" 2012            1128000
    "130298" 2013            1140000
    "130298" 2014             909000
    "130298" 2015         640199.612
    "130298" 2016           656334.2
    "130298" 2017            1050000
    "130298" 2018             960000
    "130298" 2019             902000
    "130298" 2020             935000
    "13466Q" 2010                  .
    "13466Q" 2011                  .
    "13466Q" 2012           7218.959
    "13466Q" 2013                  .
    "13466Q" 2014           9403.199
    "13466Q" 2015          12161.928
    "13466Q" 2016            15115.7
    "13466Q" 2017              46319
    "13466Q" 2018          64592.272
    "13466Q" 2019          82965.247
    "13466Q" 2020          98896.958
    "13471D" 2010                  .
    "13471D" 2011                  .
    "13471D" 2012  7855.531615821991
    "13471D" 2013             7939.2
    "13471D" 2014             7939.2
    "13471D" 2015            8520.96
    "13471D" 2016            17270.4
    "13471D" 2017              21101
    "13471D" 2018          23211.365
    "13471D" 2019          50642.978
    "13471D" 2020              45832
    end

  • #2
    Code:
    bysort ds_code (year): gen wanted = (ClimateTotalEmissions + ClimateTotalEmissions[_n+1]) / 2

    Comment


    • #3
      Originally posted by Øyvind Snilsberg View Post
      Code:
      bysort ds_code (year): gen wanted = (ClimateTotalEmissions + ClimateTotalEmissions[_n+1]) / 2
      Thanks a lot worked perfectly also for some further adaptions!

      Comment

      Working...
      X