Announcement

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

  • growth rate calculation

    Hello, I just started using stata in my university and this simple yet too hard for me right now problem is bothering me.
    I have to compute gdp growth rate but instead this time the data was sorted in unfamiliar format

    Code:
    country                after   gdp_capita
    "aaaaaa"                  0        2209
    "aaaaaa"                  1        3885
    "bbbbbb"                  0        3562
    "bbbbbb"                  1        4459
    "cccccc"                  0        2188
    It was a lot easier when data was sorted horizontally. However this time I have no clue.

    For example,

    Code:
    gen gdp_after = gdp_capita if after==1
    Code:
    gen gdp_before = gdp_capita if after==0
    I tried this but it created missing values

    Code:
     country                after   gdp_capita   gdp_after   gdp_before
    "aaaaaa"                  0        2209                      2209 
    "aaaaaa"                  1        3885         3885
    "bbbbbb"                  0        3562                      3562
    "bbbbbb"                  1        4459         4459
    "cccccc"                  0        2188
    Thus, I couldn't do

    Code:
    gen growth = (gdp_after - gdp_before)/gdp_before
    It will be very much appreciated, if someone knows how to fix this.

    Thank you

  • #2
    Welcome to Statalist.

    Perhaps this example will be useful to you.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str8 country byte after int gdp_capita
    "aaaaaa" 0 2209
    "aaaaaa" 1 3885
    "bbbbbb" 0 3562
    "bbbbbb" 1 4459
    end
    generate growth = .
    bysort country (after): replace growth = (gdp_capita-gdp_capita[_n-1])/gdp_capita[_n-1] if after
    list, noobs sepby(country)
    Code:
    . list, noobs sepby(country)
    
      +---------------------------------------+
      | country   after   gdp_ca~a     growth |
      |---------------------------------------|
      |  aaaaaa       0       2209          . |
      |  aaaaaa       1       3885   .7587144 |
      |---------------------------------------|
      |  bbbbbb       0       3562          . |
      |  bbbbbb       1       4459   .2518248 |
      +---------------------------------------+

    Comment


    • #3
      William's response is great because it does not require you to change between long and wide formats. However, if you decide you still would like to work with the data in a wide format, try:


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str3 country float(after gdp_capita)
      "aaa" 0 2209
      "aaa" 1 3885
      "bbb" 0 3562
      "bbb" 1 4459
      "ccc" 0 2188
      "ccc" 1 2321
      end
      
      reshape wide gdp_capita, i(country) j(after)

      Comment

      Working...
      X