Hello. I am working with a panel dataset as characterized below.


Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input double naics float(year emp_fraction)
311230 2005 .0669
311230 2006 .0621
311230 2007 .0726
311230 2008 .0792
311421 2005 .2174
311421 2006 .2288
311421 2007 .2195
311421 2008 .2275
311421 2009 .2184
311421 2010 .2137
311423 2005 .3615
311423 2006 .3594
311423 2007 .3398
311423 2009 .3151
311423 2010 .3165

end


I would like to generate a variable growthrate that is equal to the value of emp_fraction for year n, naics code i, divided by the value of emp_fraction for year (n-1), naics code i. In some cases there will be missing entries like naics = 311423 & year = 2008, so I want to be able to take account of that too.

For example, the first several entries should look like this (ideally the result would be an additional column, not a separate dataframe):

naics year emp_fraction growthrate
311230 2005 0.0669 .
311230 2006 0.0621 0.928
311230 2007 0.0726 1.169
311230 2008 0.0792 1.091
311421 2005 0.2174 .
311421 2006 0.2288 1.052
311421 2007 0.2195 0.959
311421 2008 0.2275 1.036
311421 2009 0.2184 0.960
311421 2010 0.2137 0.978
311423 2005 0.3615 .
311423 2006 0.3594 0.994
311423 2007 0.3398 0.945
311423 2009 0.3151 0.963
311423 2010 0.3165 1.004

Because 2005 is the first year, it has no growthrate entry since there is not previous data. All the remaining growthrate values are simple division problems, for example the second observation's growthrate comes from (emp_fraction(311230, 2006) / emp_fraction(311230, 2005)).

The exception in this sample is the growthrate for (311423, 2009) which is = (emp_fraction(311423, 2009) / emp_fraction(311423, 2007))^(1/(2009-2007)).

My first idea is that I could use the -runby- command to limit analysis to groupings by naics codes. But I am unsure how to call particular observations within each grouping and loop over them, since the years are not always consecutive. Also, I don't know how to prevent my forloop from iterating over the first year within each naics grouping.

If there is a resource that anyone could direct me to, or more situational advice, I would really appreciate it. Thank you and feel free to let me know if I should clarify anything else!