/*分配服从(0,1)分布的随机数*/
data random; set sum_Ref; random=ranuni(1);
proc sort data= random; by qdate random; run;
/*计算Culm_Ref_MktCap*/
data culm; set random; by qdate random;
if first.qdate then Culm_Ref_MktCap=Ref_MktCap; else Culm_Ref_MktCap+Ref_MktCap; run;
proc sort data=culm; by qdate Culm_Ref_MktCap; run;
/*分配decildecile_random*/
data decile_random; set culm;
if Culm_Ref_MktCap <= TRefMC*1/10 then decile_random=1;
else if TRefMC*1/10 < Culm_Ref_MktCap <= TRefMC*2/10 then decile_random=2;
else if TRefMC*2/10 < Culm_Ref_MktCap <= TRefMC*3/10 then decile_random=3;
else if TRefMC*3/10 < Culm_Ref_MktCap <= TRefMC*4/10 then decile_random=4;
else if TRefMC*4/10 < Culm_Ref_MktCap <= TRefMC*5/10 then decile_random=5;
else if TRefMC*5/10 < Culm_Ref_MktCap <= TRefMC*6/10 then decile_random=6;
else if TRefMC*6/10 < Culm_Ref_MktCap <= TRefMC*7/10 then decile_random=7;
else if TRefMC*7/10 < Culm_Ref_MktCap <= TRefMC*8/10 then decile_random=8;
else if TRefMC*8/10 < Culm_Ref_MktCap <= TRefMC*9/10 then decile_random=9;
else decile_random=10;
by qdate Culm_Ref_MktCap;
run;
/*计算DM_random, DF_random*/
proc sql;
create table sum_random as
select *, sum(MktCap) as DM_random "Decile MktCap", sum(NetFlows) as DF_random "Decile NetFlows"
from decile_random
group by qdate, decile_random
order by qdate, decile_random;
quit;
/*计算DR_random*/
proc sql;
create table ret_random as
select *, sum((MktCap/DM_random)*qret) as DR_random "Decile return (MktCap value weighted)"
from sum_random
group by qdate, decile_random
order by qdate, decile_random;
quit;
/*计算DER_random DEF_random*/
proc sql;
create table attr_random as
select *, (DR_random-MktRet) as DER_random "Decile Excess Return (Decile return over actual MktRet part)",
((DF_random/DM_random)-(TF/TM)) as DEF_random "Decile Excess Flow (All Available Flows and MktCap)"
from ret_random
group by qdate, decile_random
order by qdate, decile_random;
quit;
/*汇总DM, DF, DR, DER, DEF*/
proc sql;
create table decile_describe_random as
select distinct decile_random, qdate, DM_random, DF_random, DR_random, DER_random, DEF_random
from attr_random
group by qdate, decile_random
order by qdate, decile_random;
quit;
/*在decile describe中加入TM TF*/
proc sql;
create table info_random as
select *, i.TM, i.TF
from decile_describe_random s
inner join data.Info i on s.qdate=i.qdate
group by s.qdate, s.decile_random
order by s.qdate, s.decile_random;
quit;