liuliuqiu 发表于 2014-6-27 16:45 
对,要根据hs的不同做处理。例如说:
hs code year1 year2 number
1 1234 2004 2 ...
你试试这样行吗?
data sample;
input hs code year1 year2;
unicode=strip(hs)||"-"||strip(code);
datalines;
1 1234 2004 2004
1 2345 2004 2004
1 3456 2004 2004
1 3456 2005 2004
1 4567 2004 2004
1 4567 2005 2004
1 5678 2004 2004
1 5678 2005 2004
1 5678 2006 2004
2 2478 2005 2005
2 1234 2005 2005
2 1234 2006 2005
;
proc sql;
create table sam1 as
select hs , code , max(year1) as year
from sample
group by hs , code
order by hs , code
;
create table sam2 as
select hs , year, count(distinct code) as num
from sam1
group by hs , year
order by hs , year
;
quit;
data sam21;
set sam2;
by hs year;
order=_n_;
lorder=_n_+1;
run;
data sam22;
merge sam21(in=a drop=lorder) sam21(drop=order year rename=(lorder=order num=lastnum));
by hs order;
retain cumnum;
if a;
if missing(lastnum) then lastnum=0;
if first.hs then cumnum=0;
cumnum+lastnum;
run;
proc sql;
create table sample1 as
select *
from sample a , sam22 b
where a.year1=b.year and a.hs=b.hs
;
quit;
data sample2;
set sample1;
if year1=year2 then number=0;
else if year1>year2 then number=cumnum;
run;
proc sort data=sample2;
by hs code;
run;