我已经将数据排序为
ID value1 value2
1 A 1
1 A 2
1 A 1
2 A 2
2 B 1
3 A 1
3 B 1
3 B 1
我想将此数据更改为一个新数据集,其中我只有ID的数据,其最后一个value1是BIe,它应如下所示:
ID value1 value2
2 A 2
2 B 1
3 A 1
3 B 1
3 B 1
我试过了
data want;
set have;
by ID;
if last.value1 = 'B' then output;
run;
但这没有用。有人可以帮我吗?提前致谢!
称为DOW +顺序循环的技术使您能够:
原始数据必须按组变量排序。
data want;
do _n_ = 1 by 1 until (last.id);
set have;
by id;
end;
%* _n_ is group size at this point;
_want_group = value1 = 'B'; %* state variable -- is B in last row?;
do _n_ = 1 to _n_; %* process the group again, using a different SET buffer;
set have; %* same data set as in the do/until;
%* no by required because loop limit _n_ is group size;
if _want_group then
OUTPUT; %* output all rows of group as desired;
end;
drop _want_group;
run;