Hello,

I am working in stata 15 on windows 10 and I want to create a variable that is the difference between two periods. I have a non balance panel data set and I need to construct a variable that takes the value of the year 2000 for each id of the panel and differentiate it with the first value for year in each id. I have tried the following command:

Code:
xtset sma year
gen difflowMI = flowMI - L20.flowMI
but it does not do what I need as this is an unbalanced panel. For the sample of the data below I am looking something like: for sma==0 the variable should be flowsIM[2000]-flowsIM[1980] but for sma==240 it should be: flowsIM[2000]-flowsIM[1985]. For sma==70 the new variable should be a missing values as there is no information for 2000

Here is a sample of my data:
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float sma int year double flowsIM
  0 1980 118688.51266666665
  0 1981 115826.66666666667
  0 1982 110825.38112500001
  0 1983      109079.327875
  0 1984          94683.763
  0 1994 247745.58026000002
  0 1995   570261.418275625
  0 1996 1077448.3120197915
  0 1997 1062628.5238560415
  0 1998 1034820.6353443749
  0 1999 1088978.8031808336
  0 2000 1029940.6983304167
  0 2001   1012742.38483875
  0 2002 1042281.6182437498
  0 2003 1101260.2323106248
  0 2004 1121398.1989604167
  0 2005     1068705.605865
 70 2003  4819.450093636363
 70 2004        2527.177718
 80 1994 1869.4138050000004
 80 1995      4503.37840375
 80 1996 14769.334221666664
 80 1997  7466.258235833332
 80 1998 16010.998976250003
 80 1999  9530.395127272726
 80 2000 12942.642832727275
 80 2001        8467.057925
 80 2002 10275.203990909089
 80 2003        11203.56672
160 1980  572.0867916666666
160 1981  670.9879999999999
160 1982          528.28425
160 1983  525.5827916666668
160 1984            390.577
160 1985          239.01425
160 1986  699.9463750000001
160 1987 199.88491666666667
160 1988 139.58383333333333
160 1989  575.5377083333333
160 1990  818.3181666666669
160 1991  586.6253333333334
160 1992            891.617
160 1994 2571.3339190909096
160 1995 5854.3192768750005
160 1996       10476.606345
160 1997  16284.35516818182
160 1998 12939.549484166666
160 1999  9585.002019000001
160 2000 12379.049771666665
160 2001 11890.231902500002
160 2002 13391.656133636365
160 2003        30974.11786
200 1985 401.14374999999995
200 1986         455.008125
200 1987  310.6906666666667
200 1988 261.25095833333336
200 1989  513.0155416666666
200 1990  422.8329166666667
200 1991 473.52795833333334
200 1992            386.302
200 1994  2544.032880454545
200 1995  4292.066666250001
200 1996  9768.289357083335
200 1997 12189.856720416668
200 1998 13331.678712500003
200 1999 18192.537379166664
200 2000 13989.214159166664
200 2001 16300.074200000001
200 2002  18284.60858333333
200 2003 19795.276029999997
240 1985          366.72275
240 1986 263.89454166666667
240 1987  80.10308333333334
240 1988  96.04229166666666
240 1989  558.3115833333334
240 1990         431.345125
240 1991          308.51375
240 1992            195.882
240 1994       1467.4414355
240 1995  3824.309747857143
240 1996  8727.322945416667
240 1997  8380.615447777778
240 1998        5808.703564
240 1999       7466.6514225
240 2000  7564.011780833333
240 2001 12069.743124545454
240 2002  7714.674273333334
240 2003          81.370445
280 1985 1450.6152499999998
280 1986 1588.9762916666666
280 1987 1166.5726666666667
280 1988         947.627375
280 1989 1954.7679583333331
280 1990        1747.793125
280 1991 1395.7192916666666
280 1992 492.93958333333336
280 1993  419.7317083333333
280 1994  1028.294377142857
280 1995  570.0832674999999
300 1985          243.37675
end
Thank you so much in advance for any suggestions.

Andres