|
|
my script
本人过去是写sql,转写sas时间不长,下面这个方法主要是用sql来写的,时间紧促,备注就随便写一写,大家请指教:
/*数据输入*/
data tem;
input Card_Number $4. Charge;
cards;
A123 14.56
A123 15.23
A123 -14.56
A234 11.12
A234 11.12
A234 3.87
A234 11.12
A234 -11.12
A234 -11.12
A234 4.86
;run;
/*增加一列序列号,1,2,3,4……*/
data tt;
set tem;
id=_n_;
run;
/*
为了得到这样的分组序号效果
A234 11.12 1
A234 11.12 2
A234 11.12 3
A234 -11.12 1
A234 -11.12 2
………
*/
proc sql;
create table tt1 as
select a.*,
(select count(*) from tt where Card_Number=a.Card_Number and Charge=a.Charge and ID<=a.ID) as n
from tt a ;
quit;
/*把负数(还款)取出来*/
data t1;
set tt1;
money=abs(Charge);
where Charge<0;
run;
/*做left join增加两列,如果这两列都为0,则就是最终所要的数据*/
proc sql;
create table t2 as
select a.Card_Number,a.Charge,b.Charge as ref_charge1,c.Charge as ref_charge2
from tt1 a
left join t1 b on a.Charge=b.money and a.Card_Number=b.Card_Number and a.n=b.n and a.charge>=0
left join t1 c on a.Charge=c.Charge and a.Card_Number=c.Card_Number and a.n=c.n and a.charge<0
;
quit;
data t3 ;
set t2;
drop ref_charge1 ref_charge2;
where ref_charge1=. and ref_charge2=.;
run; |
|