Announcement

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

  • loop or other methods?

    Dear All, I have this dataset.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte country float year double I float K2007
    1 2007 47639371944.1504 1.027757e+12
    1 2008 47079741922.4759 1.027757e+12
    1 2009 23310776752.8605 1.027757e+12
    1 2010 23169892635.7175 1.027757e+12
    1 2011 25140632088.3109 1.027757e+12
    1 2012 26392333282.9276 1.027757e+12
    1 2013 24184560713.7457 1.027757e+12
    1 2014 18385377287.1616 1.027757e+12
    1 2015 16695019910.2777 1.027757e+12
    1 2016  20107616311.306 1.027757e+12
    1 2017 23337869852.3111 1.027757e+12
    1 2018  26664902464.842 1.027757e+12
    2 2007 6067174174.07463 276483178496
    2 2008 7784184466.76024 276483178496
    2 2009 9714662213.96143 276483178496
    2 2010 10608411137.8261 276483178496
    2 2011 11446475617.9555 276483178496
    2 2012 12774266788.7859 276483178496
    2 2013 14026144934.6544 276483178496
    2 2014 15551083505.0151 276483178496
    2 2015 17012885354.2527 276483178496
    2 2016 17710413653.7783 276483178496
    2 2017 21146233903.1325 276483178496
    2 2018 24973702239.7895 276483178496
    end
    label values country country
    label def country 1 "乌克兰", modify
    label def country 2 "乌兹别克斯坦", modify
    For each `country', I'd like to generate a variable `K' which is K_t=(1-0.06)*K_{t-1} + I_t. The value of K in year 2007 is K2007. Any suggestions? Thanks.
    Ho-Chuan (River) Huang
    Stata 17.0, MP(4)

  • #2
    I hope this either does what you seek, or at least it points you in a useful direction.
    Code:
    // let's measure these things in millions for readability
    replace I = I/1000000
    replace K2007 = K2007/1000000
    // set up to take advantage of time series operators
    xtset country year
    generate K = K2007 if year==2007
    replace K = (1-0.06) * L.K + I if year>2007
    format %9.1f I K2007 K
    list, noobs sepby(country)
    Code:
    . list, noobs sepby(country)
    
      +-------------------------------------------------------+
      |      country   year         I       K2007           K |
      |-------------------------------------------------------|
      |       乌克兰   2007   47639.4   1027757.0   1027757.0 |
      |       乌克兰   2008   47079.7   1027757.0   1013171.3 |
      |       乌克兰   2009   23310.8   1027757.0    975691.8 |
      |       乌克兰   2010   23169.9   1027757.0    940320.2 |
      |       乌克兰   2011   25140.6   1027757.0    909041.6 |
      |       乌克兰   2012   26392.3   1027757.0    880891.4 |
      |       乌克兰   2013   24184.6   1027757.0    852222.5 |
      |       乌克兰   2014   18385.4   1027757.0    819474.5 |
      |       乌克兰   2015   16695.0   1027757.0    787001.1 |
      |       乌克兰   2016   20107.6   1027757.0    759888.6 |
      |       乌克兰   2017   23337.9   1027757.0    737633.2 |
      |       乌克兰   2018   26664.9   1027757.0    720040.1 |
      |-------------------------------------------------------|
      | 乌兹别克斯坦   2007    6067.2    276483.2    276483.2 |
      | 乌兹别克斯坦   2008    7784.2    276483.2    267678.4 |
      | 乌兹别克斯坦   2009    9714.7    276483.2    261332.3 |
      | 乌兹别克斯坦   2010   10608.4    276483.2    256260.8 |
      | 乌兹别克斯坦   2011   11446.5    276483.2    252331.6 |
      | 乌兹别克斯坦   2012   12774.3    276483.2    249966.0 |
      | 乌兹别克斯坦   2013   14026.1    276483.2    248994.2 |
      | 乌兹别克斯坦   2014   15551.1    276483.2    249605.6 |
      | 乌兹别克斯坦   2015   17012.9    276483.2    251642.2 |
      | 乌兹别克斯坦   2016   17710.4    276483.2    254254.1 |
      | 乌兹别克斯坦   2017   21146.2    276483.2    260145.0 |
      | 乌兹别克斯坦   2018   24973.7    276483.2    269510.0 |
      +-------------------------------------------------------+

    Comment


    • #3
      Dear William, Thanks a lot. It works well.
      Ho-Chuan (River) Huang
      Stata 17.0, MP(4)

      Comment

      Working...
      X