作者:伍贤厚_197 | 来源:互联网 | 2023-05-27 19:52
在Stata,我可以这样做:
bysort group_var: drop if _n > 6
只保留前六个观察各组内,通过指定group_var
.我如何在SAS中执行此操作?
我试过了:
proc sort data=indata out=sorted_data;
by group_var;
run;
data outdata;
set sorted_data;
by group_var;
if (_n_ > 6) then delete;
run;
但这会删除整个数据集中除了前六个观测值之外的所有观测值(总共只留下六个观测值).
1> DomPazz..:
您需要按组计算每个记录.
data outdata;
set sorted_data;
by group_var;
retain count;
if first.group_var then
count = 0;
count = count + 1;
if count > 6 then delete;
drop count;
run;