@Yuna11 wrote: Hi Tom, I just edited my question a little bit, hopefully it is more clear now. The data is sorted by time and each group will have a 1 and only one "1" either at middle of somewhere or at the end. And I want to flag the rows before 1.
That is even easier.
data want;
set have;
by group ;
if first.group then y='Y';
if lag(x)=1 and not first.group then y='N';
retain y;
run;
The reason there are two IF statements instead of just an ELSE is because you need to execute the LAG() function for every observation for it to work properly. The LAG() function remembers the values that are passed into it, so if you skip executing it for some observations then those values of X never make into the list of values that can be returned by the LAG() function.
You could also skip the LAG() function and instead insert an OUTPUT statement so you can change the value of Y after it has already been written to the output dataset.
data want;
set have;
by group ;
if first.group then y='Y';
retain y;
output;
if x=1 then y='N';
run;
... View more