Dear All,

I'm working on longitudinal data that look like the following:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input byte id double x
1 5.2
1 5.4
1   .
1 5.3
1   5
2 4.7
2   .
2   .
2 4.8
2 4.6
3   2
3 2.1
3 2.4
3   .
3 2.3
end
Starting from the command:

Code:
bys id: gen d_x=d.x
I am trying to first difference x by id, but using the last available observation in case of missing values.
For instance, with id==2, 4.8 should be first differenced with 4.7, being the last available observation, regardless of the number of missing values in between.

Many thanks for your help!