I have some SAS code that needs to reference some values in a previous row. The lag function is typically how one deals with that. The issue is that the value that I grab using lag then (possibly) gets changed and the change isn't reflected in the next row. I've provided a short example but basically think of 'x' as the starting value and 'y' as the ending value and I don't want any of the intervals to be overlapping so if the starting value is <= the previous ending value I want to shift the interval over and keep repeating this process.
Code: 
data test;
	input x y;
	datalines;
1 2
1 4
5 7
;

data new;
	set test;
	diff = y - x;
	lg = lag(y);
	if x < lg then x = lg + 1;
	* update x and y to be shifted;
	y = x + diff;
run;
My output gives:
Code: 
                                     Obs    x    y    diff    lg

                                      1     1    2      1      .
                                      2     3    6      3      2
                                      3     5    7      2      4
I would have expected/desired

Code: 
                                     Obs    x    y    diff    lg

                                      1     1    2      1      .
                                      2     3    6      3      2
                                      3     7    9      2      6
So basically in the final row the lagged value for y is reading the original unmodified value of 4 instead of the updated value of 6. I think this is due to lag being implemented as a queue but I don't know how to tell lag to update after I change the value. I'm open to any suggestions - I'm not married to using lag here if there is another way around the issue.