自己摸索了一段程序,供参考
附件中的数据表,我定义的一个a、b两个变量,可以看作你的月份和各公司名(看作已经按公司价值从大到小排序)
程序的结果如果非10 的整数倍,在前面几个分类中多一条观测,比如某月有26个公司,按前六个分类每个分类3个公司,后四个每个分类2个;如果为10 的整数倍则都是同一的观测数
程序如下:
data a;
set a;
by a b;
if first.a then a1=1;
else a1+1;
run;
proc sql;
create table b as
select *,int(max(a1)/10) as a2,mod(max(a1),10) as a3
from a group by a;
quit;
proc sort data=b;
by a b;
run;
data c;
set b;
a4=(a2+1)*a3;
if a1<=a4 then cls=compress(a||'1');
else cls=compress(a||'2');
run;
data c;
set c;
by cls b;
if first.cls then c1=1;
else c1+1;
run;
data d;
set c;
by cls;
if first.cls then a5=1;
else do;
if substr(cls,2,1)='1' and mod(c1,(a2+1))=1 then a5+1;
if substr(cls,2,2)='2' and mod(c1,a2)=1 then a5+1;
end;
run;
data d;
set d;
cl=compress(cls||a5);
keep a b cl;
run;
|