Generate percent change between annual observations in Stata?

31,225

// Here's an example with another measure of growth:

clear
set obs 100
gen year = _n + 1959
gen expenditure = _n^(1/3) + runiform()
line expenditure year, yti("Synthetic data example")

// From Statalist:

bys year: g expendituregrowth=100*(expenditure[_n]-expenditure[_n-1])/expenditure[_n-1]

// Also:

gen expenditure_gr = (expenditure/expenditure[_n-1] - 1)*100 // growth rate for expenditure
gen expenditure_bl = 100*expenditure/expenditure[1] // baseline growth rate for expenditure; base 100 = 1960

line expenditure_gr year, yti("Growth rate")
line expenditure_bl year, yti("Growth rate (base 100 = 1960)")

// The computation of expenditure_gr is what I think you are looking for.

// If your data are well-formed, use Stata with time series and get the growth rate easily:

tsset year, delta(1)
cap drop expenditure_gr
gen expenditure_gr = D.expenditure / 100*L.expenditure
Share:
31,225
Ricardo Altamirano
Author by

Ricardo Altamirano

Originally from Nicaragua, educated in Edinburgh and the USA, and now living primarily in London. Useful questions and answers Stack Overflow Debugging CREATE TABLE statements Logging in Python Simple string formatting in Python Reference types in C# String compression in C# using Gzip Meta Stack Overflow Book recommendation questions Answering old questions with a solution in the comments IT Security Cryptographically secure random strings in PHP LaTeX Fitting a table on a page through rotation StackExchange Flair

Updated on July 09, 2022

Comments

  • Ricardo Altamirano
    Ricardo Altamirano almost 2 years

    How do I use the gen or egen commands to generate the percent change between observations for different years in Stata? For example, I have observations for 1990 through 2010, each with a different value for expenditures, and I'm trying to generate a new observation with the percent change from 1990-1991, 1991-1992, etc.

  • StasK
    StasK about 12 years
    The last line will only create the difference; to create the growth rate equivalent to your previous definition of exp_gr, it should be gen exp_gr = D.exp/L.exp*100. Also, exp is an absolutely terrible name for a variable: if you forget multiplication sign in exp*(stuff1+stuff2), you will spend an hour looking for this bug (once somebody notes that your numbers are extraordinarily large).
  • Ricardo Altamirano
    Ricardo Altamirano about 12 years
    Excellent, thank you. Sorry about taking so long to accept your answer, but it is very helpful.
  • Konrad
    Konrad over 9 years
    Hi, I cam across this post and was wondering whether the correct way of obtaining projection for the data set that is xtset would be bys panel date: gen variable_gr = D.variable/L.variable*100? Running the command without the bys prefix returned the not sorted error.
  • Fr.
    Fr. over 9 years
    Sounds good. You can sort the data prior to the computation, as a separate command.