Announcement

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

  • Calculating 5 Year Compound Annual Growth Rates (CAGR)

    Hello Stata-listers,

    I am trying to calculate 5 year compound growth rates of gdppc (ie [(End val/beg val) ^(1/totyears)]-1 ). After lots of hunting, I could not find the code that I am looking for. Eventually, I will also calculate 10, 15, 20 year spans, as well.

    I am using Stata 13.1 for Mac, the data is panel with typical gdp data (country, year, gdppc). I have a much longer timespan for this (1962 - present) for 160 countries, but for simplicity I am providing the past 14 years of data for one country.

    Please note that I do not use the string isocode, but rather ccode (encoded isocode, but for the sake of understanding, I presented the data with the country codes)

    My input data:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long code int year float gdpwtc
    "USA" 2000  45866.84
    "USA" 2001  45844.46
    "USA" 2002  46230.65
    "USA" 2003  47114.27
    "USA" 2004  48477.21
    "USA" 2005  49657.54
    "USA" 2006  50517.59
    "USA" 2007  50939.59
    "USA" 2008  50321.09
    "USA" 2009   48485.1
    "USA" 2010   49288.5
    "USA" 2011   49674.8
    "USA" 2012  50390.96
    "USA" 2013  50764.77
    "USA" 2014  51620.79
    end
    My code:
    Code:
     bys ccode: gen g5pwt = [(gdpwt/l5.gdpwtc)^(1/5)]-1
    My output:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str3 isocode int year float g5pwt
    "USA" 2000     .03046135
    "USA" 2001     .02516414
    "USA" 2002    .020465866
    "USA" 2003    .018027034
    "USA" 2004    .016984068
    "USA" 2005    .016008312
    "USA" 2006    .019603074
    "USA" 2007    .019588867
    "USA" 2008    .013256771
    "USA" 2009 .000032567954
    "USA" 2010   -.001490759
    "USA" 2011   -.003359077
    "USA" 2012  -.0021633469
    "USA" 2013    .001757208
    "USA" 2014    .012612472
    What I am trying to calculate is the compound growth rate for every five years. So I would like my data to end up looking like:

    "USA" 2000 .03046135
    "USA" 2001 .
    "USA" 2002 .
    "USA" 2003 .
    "USA" 2004 .
    "USA" 2005 .016008312
    "USA" 2006 .
    "USA" 2007 .
    "USA" 2008 .
    "USA" 2009 .
    "USA" 2010 -.001490759
    "USA" 2011 .
    "USA" 2012 .
    "USA" 2013 .
    "USA" 2014 .
    [/CODE]

    I am willing to settle for a dummy variable generation where the year ends with a 5 or 0, but not sure how to code that.

    Thank you for your help!

  • #2
    The code you show actually should not run at all, because the use of [] as a substitute for parentheses is not legal Stata syntax. But, interestingly, it does run. However, it cannot possibly produce the results you are showing because there need to be missing values for the results in the first 5 observations when you are using the L5 operator. Also, your -bys code:- will sort the data by country, but into random order within country, so your results are not reliable.

    The actual question in your post is how to get the results to appear only in years that are multiples of five.

    Code:
    clear
    input str10 code int year float gdpwtc
    "USA" 2000  45866.84
    "USA" 2001  45844.46
    "USA" 2002  46230.65
    "USA" 2003  47114.27
    "USA" 2004  48477.21
    "USA" 2005  49657.54
    "USA" 2006  50517.59
    "USA" 2007  50939.59
    "USA" 2008  50321.09
    "USA" 2009   48485.1
    "USA" 2010   49288.5
    "USA" 2011   49674.8
    "USA" 2012  50390.96
    "USA" 2013  50764.77
    "USA" 2014  51620.79
    end
    
    encode code, gen(ccode)
    xtset ccode year
    bys ccode (year): gen g5pwt = (gdpwtc/L5.gdpwtc)^(1/5) - 1 if mod(year, 5) == 0
    It differs from your desired results in that there is no result generated in 2000, as there cannot be in the absence of data prior to 2000.


    Comment

    Working...
    X