I want to generate a new variable, say called l_diff, that starts producing values in the row where ts=198 and is equal to the difference between lnl when ts=198 MINUS lnl when ts=148, then the next value in the row ts=199 will be lnl when ts=199 MINUS lnl when ts=149, and the next will be lnl when ts=200 MINUS lnl when ts=150. I want values for this variable for every naic from ts=198 to ts=210 (minus the values from 148 to to 160, respectively.

A snapshot of my data:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input double naic float year double ts float lnl
311111 1997 148  8.919141
311111 1997 149 8.9250555
311111 1997 150  8.969796
311111 1997 151  8.963288
311111 2001 164  2.959587
311111 2001 165 2.9604504
311111 2001 166 2.9604676
311111 2001 167   2.96584
311111 2002 168  2.892111
311111 2002 169  2.884745
311111 2002 170   2.88989
311111 2002 171 2.9026296
311111 2003 172  2.887776
311111 2003 173 2.8903904
311111 2003 174  2.889964
311111 2003 175 2.8905015
311111 2004 176  2.873527
311111 2004 177 2.8673306
311111 2004 178   2.87117
311111 2004 179  2.866193
311111 2005 180  2.884447
311111 2005 181 2.8873115
311111 2005 182  2.886977
311111 2005 183 2.9019525
311111 2006 184  2.909066
311111 2006 185  2.915805
311111 2006 186 2.9308736
311111 2006 187  2.937114
311111 2007 188  2.921296
311111 2007 189 2.9146125
311111 2007 190  2.930447
311111 2007 191 2.9481864
311111 2008 192  2.943245
311111 2008 193 2.9402196
311111 2008 194  2.938862
311111 2008 195   2.94154
311111 2009 196  2.977857
311111 2009 197  2.986002
311111 2009 198  2.990519
311111 2009 199  3.001565
311111 2010 200  3.001814
311111 2010 201  3.018211
311111 2010 202  3.022309
311111 2010 203 3.0274894
311111 2011 204  3.024401
311111 2011 205  3.021709
311111 2011 206  3.017706
311111 2011 207   3.02736
311111 2012 208   3.04595
311111 2012 209  3.044983
311111 2012 210  3.071628
311119 1997 148  9.279991
311119 1997 149  9.285603
311119 1997 150  9.292596
311119 1997 151  9.276284
311119 1998 152  9.264766
311119 1998 153  9.283498
311119 1998 154  9.310336
311119 1998 155  9.281327
311119 1999 156  9.286807
311119 1999 157  9.296885
311119 1999 158  9.301308
311119 1999 159  9.315541
311119 2000 160  9.268389
311119 2000 161   9.27747
311119 2000 162  9.269489
311119 2000 163  9.256715
311119 2001 164 3.5261056
311119 2001 165 3.5248594
311119 2001 166  3.521427
311119 2001 167 3.5082664
311119 2002 168  3.492672
311119 2002 169  3.498415
311119 2002 170  3.486508
311119 2002 171 3.4880476
311119 2003 172  3.463452
311119 2003 173  3.457672
311119 2003 174  3.445289
311119 2003 175 3.4385145
311119 2004 176  3.460493
311119 2004 177  3.449257
311119 2004 178  3.444076
311119 2004 179  3.440984
311119 2005 180 3.4178905
311119 2005 181  3.419605
311119 2005 182 3.4192555
311119 2005 183   3.42248
311119 2006 184 3.4199865
311119 2006 185 3.4234915
311119 2006 186  3.418251
311119 2006 187 3.4231436
311119 2007 188  3.435277
311119 2007 189   3.44985
311119 2007 190  3.450432
311119 2007 191  3.460619
311119 2008 192 3.4688146
311119 2008 193  3.466756
311119 2008 194 3.4638906
311119 2008 195  3.458155
311119 2009 196 3.4552436
end