Announcement

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

  • Percentage change( by year and from initial to last)

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float id str52 country int year double gdp_pc_current_local_currency
    1 "Ireland"  2000 28482.53982603686
    2 "Portugal" 2000 12484.70150044247
    1 "Ireland"  2001 31543.98210355635
    2 "Portugal" 2001  13107.3208371314
    1 "Ireland"  2002  34576.9564035324
    2 "Portugal" 2002 13688.72026274251
    1 "Ireland"  2003 36420.82951647195
    2 "Portugal" 2003 13974.64178801798
    1 "Ireland"  2004 38361.94819891201
    2 "Portugal" 2004 14533.91665532384
    1 "Ireland"  2005 40911.51403610747
    2 "Portugal" 2005 15104.97708821869
    1 "Ireland"  2006 43288.04043250746
    2 "Portugal" 2006 15799.67351207266
    1 "Ireland"  2007 44829.35669531446
    2 "Portugal" 2007 16643.11070397281
    1 "Ireland"  2008 41820.76620699118
    2 "Portugal" 2008 16941.61615210656
    1 "Ireland"  2009 37504.48419369953
    2 "Portugal" 2009  16601.4467678509
    1 "Ireland"  2010 36749.46355990093
    2 "Portugal" 2010 17017.69698574685
    1 "Ireland"  2011 37540.63025918302
    2 "Portugal" 2011 16686.29664430039
    1 "Ireland"  2012 38169.33806105968
    2 "Portugal" 2012 16015.26080653218
    1 "Ireland"  2013  38993.3942008073
    2 "Portugal" 2013 16282.34902046849
    1 "Ireland"  2014 41766.42749487949
    2 "Portugal" 2014  16640.5175740708
    1 "Ireland"  2015  55729.4398906668
    2 "Portugal" 2015 17359.31084112532
    1 "Ireland"  2016   57949.040393579
    2 "Portugal" 2016 17964.73200398394
    1 "Ireland"  2017 61523.87564587727
    2 "Portugal" 2017 18756.29563584314
    end
    format %ty year
    Q1 .I am trying to find % change in the variable gdp_pc_current_local_currency by year for each country. Tried the following code. Unfortunately, it yields missing values.
    Code:
    bys year id : gen growth_gdp_pc_current=100*(gdp_pc_current_local_currency[_n]-gdp_pc_current_local_currency[_n-1])/gdp_pc_current_local_currency[_n-1]
    Q2. For the same variable, how do I find % change from the initial year 2010 to the last year 2017 for each country?

  • #2
    Thank you for the good selection of example data.

    This code will do what you want, I think.
    Code:
    bys id (year): gen growth_gdp_pc_current=100*(gdp_pc_current_local_currency[_n]-gdp_pc_current_local_currency[_n-1])/gdp_pc_current_local_currency[_n-1]
    bys id (year): gen growth_gdp_pc_cumul  =100*(gdp_pc_current_local_currency[_N]-gdp_pc_current_local_currency[1])/gdp_pc_current_local_currency[1]
    You need to run your calculations separately for each id (country) and within each country the data must be sorted by year, and that is what
    Code:
    bysort id (year):
    does. Within each country, there are 18 observations, and gdp_pc_current_local_currency[_n]-gdp_pc_current_local_currency[_n-1] give the year to year differences.

    You had
    Code:
    bys id year
    which meant your calculation was run separately for each observation. But with only one observation at a time, gdp_pc_current_local_currency[_n-1] is always missing, and your result is always missing.

    In the second command, gdp_pc_current_local_currency[_N] is the last observation (2017) for the id and gdp_pc_current_local_currency[1] is the first observation (2000) for the id.

    Comment


    • #3
      William Lisowski : Many thanks for the code and explanation! The code is efficacious; got the result that I wanted. Thanks again!

      Comment

      Working...
      X