Re: how to keep first observation to last observation including missings in between?

Perhaps not the most efficient way:First pass - assign row no to a variable (_n_) and delete all Obs with missing until the firs non missing. Use first. logic and a flag to keep track if you encountered the first non missing within the by group.Second - resort the data set descending based on the previously created row no variable.Third - repeat the logic from the first pass. Remember to use descending on the BY statement.

Re: how to keep first observation to last observation including missings in between?

data want;
do _n_=1 by 1 until(last.id);
set have;
by id;
if x>. then do;
if start=. then start=_n_;
finish=_n_;
end;
end;
do _n_=1 by 1 until(last.id);
set have;
by id;
if start<=_n_<=finish then output;
end;
drop start finish;
run;