|
|
沙发

楼主 |
发表于 2010-9-16 23:55:18
|
只看该作者
Re: 求助:一个比较复杂的数据处理问题
[code:1dptkipx]data a;
input r fp $ lp $ rel percent8.0;
cards;
1 A&B C 90%
2 A D 80%
3 A C 70%
4 A E 65%
5 A F 50%
6 B A 55%
7 B C 65%
8 B D 60%
9 B E 40%
10 C D 75%
11 B&C D 80%
;
data b;
input id p $;
cards;
1 A
1 B
2 B
2 C
2 D
;
run;
proc sort data=b;
by id p;
run;
data b1(drop=p);
set b;
by id p;
if first.id then do;
fa='';fb='';fc='';fd='';
retain fa fb fc fd ;
end;
if p='A' then fA=1;
else if p='B' then fB=1;
else if p='C' then fC=1;
else if p='D' then fD=1;
if last.id=1;
RUN;
proc sql;
create table a1 as
select *
from (select *,max(rel) as max_rel,(max(rel)=rel) as m_r
from a
where fp in (select p from b where id=1)
or (substr(fp,1,1) in (select p from b where substr(fp,2,1)='&' and id=1)
and substr(fp,3,1) in (select p from b where substr(fp,2,1)='&' and id=1))
group by lp)
order by max_rel desc,lp,rel desc;
quit;
data a2(drop=max_rel m_r rank); set a1; by descending max_rel lp descending rel ;
rank+m_r;
if rank<=3 and first.lp;
run;
data a3(drop=r fp lp rel); retain id 1;set a2; retain lc relc ld reld le rele;
if lp='C' then do; lc=lp; relc=rel; end;
if lp='D' then do; ld=lp; reld=rel; end;
if lp='E' then do; le=lp; rele=rel; end;
if _n_=3;
run;
proc sql;
create table final as
select b1.*,a3.*
from b1 left join a3
on b1.id=a3.id
where b1.id=a3.id;
quit;[/code:1dptkipx] |
|