Hello Statalist members,

I want to calculate the difference between the last and first observations in my time series data. I am using Stata 16 for Mac. I have 25 years of data with one observation for each year for 2167 individuals which are nested within 55 groups. I ultimately want to create percent change over time. But first wanted to create a variable that calculates 2014 observation - 1990 observation.

I first tried the following code but it was definitely not the correct syntax.

gen = ((var if year==2014) - (var if year==1990))


Then I tried to use xtset id year and the times series operators but I believe this is only the difference between the last and previous observation ie. 1991-1990 or 1992-1991 or 1993-1992.

gen diff = D.var
label variable diff "Difference: 2014-1990

gen change = D.`var' / L.`var'
label variable change "Change: 2014-1990/1990"

Please share how to create a new variable that is the difference between the last and first observations.

Thank you for your time and insights.
Be well and safe.