|
|
板凳

楼主 |
发表于 2010-5-3 10:17:22
|
只看该作者
Re: Error: No matching %marco with this %mend
那我干脆全贴上来?就怕太多了能解答的人没耐心看;
%macro cash(num_cycles=);
%do i=1 %to &num_cycles;
%let year_cycle=1950+&i;
%*Get monthly market return;
proc sql;
create table d as
select
d_ff.*,
(year(d_ff.date)*100+month(d_ff.date)) as month
from
d_ff
where
&year_cycle-4<=year(d_ff.date)<=&year_cycle;
%*Number of months;
proc sort data=d out=d_months (keep=month) nodupkeys;
by month;
data d_months;
set d_months;
i+1;
proc sort data=d_months;
by month;
%*Compute average return of all stocks as proxy for monthly market return;
proc sort data=d;
by month;
proc means data=d noprint;
by month;
output out=d_mkt_ret
mean(ret)=month_mkt_ret;
proc sort data=d_mkt_ret (keep=month month_mkt_ret);
by month;
data d;
merge
d_mkt_ret(keep=month month_mkt_ret)
d_months(keep=i month);
by month;
proc sort data=d nodupkeys;
by month;
%*Merge market return and stock return;
proc sql;
create table d_1 as
select
d_ff.permno,
d_ff.ret,
(year(d_ff.date)*100+month(d_ff.date)) as month
from
d_ff
where
&year_cycle-4<=year(d_ff.date)<=&year_cycle;
proc sort data=d_1;
by month;
data d_all;
merge
d_1(keep=permno month ret)
d(keep=month i month_mkt_ret);
by month;
%*Keep only securities with returns in all 12 months;
proc sort data=d_all;
by permno;
proc means data=d_all noprint;
var ret;
output out=d_all_stats_1 n=n_1;
by permno;
where
1<=i<=48;
proc means data=d_all noprint;
var ret;
output out=d_all_stats_2 n=n_2;
by permno;
where
48+1<=i<=48+12;
data d_all_1;
merge
d_all d_all_stats_1 d_all_stats_2;
by permno;
if
n_1=48 and n_2=12;
%*Compute betas for firms;
proc reg data=d_all_1 outest=d_all_reg (keep=intercept month_mkt_ret permno) noprint;
model ret=month_mkt_ret;
by permno;
where
1<=i<=48;
run;
data d_all_reg (rename=(intercept=alpha month_mkt_ret=beta));
set d_all_reg;
data d_all;
merge
d_all_1 d_all_reg;
by permno;
data d_all;
set d_all;
drop n_1 n_2 _type_ _freq_;
%*Define portfolio according to company's cash holding;
data d_cash_holding_1;
set d_cash_holding;
where
year(date)=&year_cycle;
proc sort data=d_cash_holding_1 (keep=permno cash_holding_prev_year) nodupkeys;
by permno;
proc sql;
create table d_cash as
select
d_all.permno,
d_all.ret,
d_all.beta,
d_all.month,
d_cash_holding_1.cash_holding_prev_year as cash,
(d_all.ret-d_all.alpha-d_all.beta*d_all.month_mkt_ret) as ab_ret
from
d_cash_holding_1 left join d_all
on
d_cash_holding_1.permno=d_all.permno
and
48+1<=i<=48+12
order by
permno,month;
data d_cash;
set d_cash;
if 0<cash<0.20
then
sample=1;
else
if
cash>=0.20
then
sample=2;
else
sample=0;
retain sample;
proc sort data=d_cash;
by sample month;
%if &i=1 %then
%do;
data worklib.d_cash_study;
set d_cash;
%end;
%else
%do;
proc append base=worklib.d_cash_study data=d_cash;
run;
%end;
%end;
run;
%mend cash; |
|