Stata: calculating growth rates for observations with same ID -


i want calculate growth rates in stata observations having same id. data looks in simplified way:

id    year     b   c   d   e   f 10    2010   2   4   9   8   4   2 10    2011   3   5   4   6   5   4 220   2010   1   6   11  14  2   5 220   2011   6   2   12  10  5   4     334   2010   4   5   4   6   1   4 334   2011   5   5   4   4   3   2 

now want calculate each id growth rates variables a-f 2010 2011:

for e.g id 10 , variable a be: (3-2)/2, variable b: (5-4)/4 etc. , store results in new variables (e.g. growth_a, growth_b etc).

since have on 120k observations , around 300 variables, there efficient way (loop)?

my code looks following (simplified):

local variables "a b c d e f" foreach x in local variables {  bys id: g `x'_gr = (`x'[_n]-`x'[_n-1])/`x'[_n-1] } 

fyi: variables a-f numeric.

but stata says: 'local not found' , not sure whether code correct. have sort year first?

the specific error in

local variables "a b c d e f" foreach x in local variables {      bys id: g `x'_gr = (`x'[_n]-`x'[_n-1])/`x'[_n-1] } 

is error in syntax of foreach, here expects syntax foreach x of local variables, given prior use of local macro. keyword in, foreach takes word local literally , here looks variable name: hence error message. basic foreach syntax: see help.

this code problematic further reasons.

  1. sorting on id not guarantee correct sort order, here time order year, each distinct id. if observations jumbled within id, results garbage.

  2. the code assumes time values present; otherwise time gap between observations might unequal.

a cleaner way growth rates

tsset id year  foreach x in b c d e f {      gen `x'_gr = d.`x'/l.`x'  }  

once have tsset (or xtset) time series operators can used without fear: correct sorting automatic , operators smart gaps in data (e.g. jumps 1982 1984 in yearly data).

for more variables loop

foreach x of var <whatever> {      gen `x'_gr = d.`x'/l.`x'  }  

where <whatever> general (numeric) varlist.

edit: question has changed since first posting , interest declared in calculating growth rates 2010 2011, implication in example years present. more general code above naturally still work calculating growth rates.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -