/********************************************************************* */
/*  Program        : 8.EGdecomposition.sas                                             */
/*  Description    : Decompose the expected investment growth (EG)      */
/*                   into the component predicted by macro                            */
/*                   uncertainty and the residual component, and                     */
/*                   examine the returns to the portfolios sorted by                  */
/*                   each component (Table 8 of the paper).                            */
/*                   1) For each firm, EG (exp_ia) is regressed on                   */
/*                   the macro uncertainty shock (d_pcahat) over the               */
/*                   full sample: the fitted value is eia_unc (the                        */
/*                   predicted component) and the residual is                           */
/*                   eia_res (the residual component).                                     */
/*                   2) All stocks are sorted into ten portfolios                         */
/*                   every month by eia_unc (Panel A) or eia_res                     */
/*                   (Panel B), and the value-weighted excess                          */
/*                   portfolio returns of the next month are merged                  */
/*                   with the pricing factors and saved as                                 */
/*                   Table8\predictedEG (Panel A) or                                      */
/*                   Table8\residualEG (Panel B).                                          */
/*                   3) The 10-1 return (LS) is tested with the                        */
/*                   average return and the alphas from the CAPM,               */
/*                   FF3, FF4, FF5, FF6, HXZ q, and SY models. The          */
/*                   GMM standard errors with the Bartlett kernel                 */
/*                   (bandwidth 7) correspond to the Newey-West                */
/*                   t-statistics with six-month lags. For the alpha                */
/*                   of an individual decile, replace LS with the                   */
/*                   decile return (_1, _2, ..., _10) in the proc                      */
/*                   model blocks.                                                              */
/*                   Both portfolio-return datasets are included in               */
/*                   the package, so Section 4 can be run directly               */
/*                   without re-running Sections 1-3.                                 */
/*                   The testing period is 1973:07-2018:06; the SY            */
/*                   model ends in 2016:12 (the m4 sample).                     */
/*                                                                                                     */
/*  Input : output\q5\ crsp_compustat4 (monthly firm-level data with  */
/*          exp_ia, from 0.Q5factorconstruction.sas)                             */
/*          output\ dispersion_all (uncertainty shocks, from                   */
/*          0.MakeDispersion.sas)                                                        */
/*          output\ crsp2 (CRSP monthly returns with the                     */
/*          value weights, from 0.Qfactorconstruction.sas)                   */
/*          output\ q_factor (from 0.Qfactorconstruction.sas)               */
/*          data\   factor (monthly FF factors and rf, from                    */
/*          0.DataDownload.sas)                                                         */
/*          Table4\ m4 (Stambaugh and Yuan (2017) monthly factors) */
/*  Output: Table8\ predictedEG (Panel A) / residualEG (Panel B)   */
/*          (monthly excess returns of the ten portfolios and the           */
/*          10-1 return with the FF and q factors)                                */
/********************************************************************* */

%let projpath = D:\SAS\TFP\Dispersion_replication;
libname mydisp "&projpath.\data";
libname myq "&projpath.\output";
libname myq5 "&projpath.\output\q5";
libname myout "&projpath.\output";
libname myt4 "&projpath.\Table4";
libname myt8 "&projpath.\Table8";

/*1.Merge the expected investment growth with the uncertainty shocks*/
proc sort data=myq5.crsp_compustat4 out=myout.crsp_compustat4_s; by permno date; run;
proc sql;
create table myout.Eia_unc as
select a.*, b.d_tfpstd_star, b.d_pcahat, b.d_pcares
from myout.crsp_compustat4_s as a, myout.dispersion_all as b
where a.fyear=b.cyear;
quit;

/*2.Full sample regression to decompose EG into Predicted EG by UNC and residuals*/
proc sort data=myout.Eia_unc;
by permno date;
run;
proc printto log=junk; run;
proc reg data=myout.Eia_unc edf outest=myout.Eia_unc_coeff noprint;
    by permno;
    model exp_ia=d_pcahat;
	output out=myout.Eia_unc2 p=eia_unc r=eia_res;
run;
proc printto; run;
proc sql;
create table myout.Eia_unc2 as
select a.*, b._EDF_
from myout.Eia_unc2 as a, myout.Eia_unc_coeff as b
where a.permno=b.permno and b._EDF_>=1;
quit;
proc sort data=myout.Eia_unc2;
by date;
run;

/*3.Construct the portfolios and save the portfolio returns*/
/*The default sorts by eia_unc and saves Table8\predictedEG (Panel A).*/
/*For Panel B, replace eia_unc with eia_res in the two sorting steps below*/
/*and save the last table as myt8.residualEG.*/
proc univariate data=myout.Eia_unc2 noprint;
where shrcd in (10,11) and exchcd in (1,2,3);
var eia_unc;
by date;
output out=myout.nyse_eia  pctlpts=10 20 30 40 50 60 70 80 90 pctlpre=Eia;
run;
/* Use Breakpoints to classify stock only at end of all June's */
proc sql;
create table myout.Eia10 as
select a.permno, a.date, a.me, a.lme, a.wi, a.d1_ia, a.cym, a.exp_ia, a.f1_ia,
       b.eia10, b.eia20, b.eia30, b.eia40, b.eia50, b.eia60, b.eia70, b.eia80, b.eia90,
	   a.eia_unc, a.eia_res
from myout.Eia_unc2 as a, myout.nyse_eia as b
where a.date=b.date;
quit;
data myout.PF_decile;
     set myout.Eia10;
     If eia90 <= eia_unc                 then EIAport = '10';
     If eia_unc<  eia10                  then EIAport = '1';
     If eia10 <= eia_unc < eia20         then EIAport = '2';
     If eia20 <= eia_unc < eia30         then EIAport = '3';
     If eia30 <= eia_unc < eia40         then EIAport = '4';
     If eia40 <= eia_unc < eia50         then EIAport = '5';
     If eia50 <= eia_unc < eia60         then EIAport = '6';
     If eia60 <= eia_unc < eia70         then EIAport = '7';
     If eia70 <= eia_unc < eia80         then EIAport = '8';
     If eia80 <= eia_unc < eia90         then EIAport = '9';
if missing(EIAport)=0 then nonmissport=1;
else nonmissport=0;
keep permno date eia_unc EIAport nonmissport;
run;
/*Next month value-weighted portfolio returns*/
proc sql;
create table myout.PF_return10 as
select a.date, a.permno, a.ret, a.wi, b.date as portdate format date9., b.EIAport, b.nonmissport
from myq.crsp2 as a, myout.PF_decile as b
where a.permno=b.permno
and intnx('month',a.date,0,'E')=
    intnx('month',b.date,1,'E')
order by date , EIAport;
quit;
proc univariate noprint data=myout.PF_return10;
    var ret;
    by date EIAport;
    weight wi;
    output out = myout.portret10 mean = vwret n = N;
run;
/*Excess returns*/
proc sql;
    create table myout.portret10 as
    select a.date, a.EIAport, (a.vwret-b.rf) as vwdecileret
    from myout.portret10 as a, mydisp.factor as b
    where year(a.date) = b.year and month(a.date) = b.month;
quit;
data myout.portret10; set myout.portret10;
if date > '01Jul1973'd; run;
proc means data=myout.portret10 (keep=EIAport vwdecileret );
class EIAport;
run;
proc transpose data=myout.portret10(keep=date EIAport vwdecileret) out=myout.portret10t (drop=_name_ _label_);
by date;
ID EIAport;
Var vwdecileret;
run;
data myout.portret10t;
     set myout.portret10t;
	 LS=(_10 - _1);
run;
/*Merge with the pricing factors and save (predictedEG: eia_unc sort;*/
/*residualEG: eia_res sort)*/
proc sql;
     create table myt8.predictedEG as
	 select a.*,
            b.mktrf, b.smb, b.hml, b.rmw, b.cma, b.umd,
            c.q_me, c.q_IA, c.q_roe
	 from myout.portret10t as a, mydisp.factor as b, myq.q_factor as c
     where year(a.date)=b.year=year(c.date) and
           month(a.date)=b.month=month(c.date);
quit;

/*4.Average return and alphas of the 10-1 portfolio (LS)*/
/*This section runs directly from the included datasets: the default is*/
/*Table8\predictedEG (Panel A); replace predictedEG with residualEG for*/
/*Panel B. For the alphas of an individual decile, replace LS with*/
/*_1, ..., _10 in the proc model blocks.*/
data myout.portret10t_factor; set myt8.predictedEG; run;
proc sql;
     create table myout.portret10t_factor2 as
	 select a.date, a._1, a._2, a._3, a._4, a._5, a._6, a._7, a._8, a._9, a._10, a.LS,
            b.mktrf, b.smb, b.mgmt, b.perf
     from myt8.predictedEG as a, mydisp.m4 as b
     where year(a.date)=b.year and
           month(a.date)=b.month;
quit;
proc model data = myout.portret10t_factor;
parms b0 ;
      LS = b0;
fit LS /gmm kernel = (bart,7,0) vardef=n;
instruments/intonly;
run;
proc model data = myout.portret10t_factor;
parms b0 b1 ;
      LS = b0+b1*mktrf;
fit LS /gmm kernel = (bart,7,0) vardef=n;
instruments  mktrf;
run;
proc model data = myout.portret10t_factor;
parms b0 b1 b2 b3 ;
      LS = b0+b1*mktrf+b2*smb+b3*hml;
fit LS /gmm kernel = (bart,7,0) vardef=n;
instruments  mktrf smb hml;
run;
proc model data = myout.portret10t_factor;
parms b0 b1 b2 b3 b4;
      LS = b0+b1*mktrf+b2*smb+b3*hml+b4*umd;
fit LS /gmm kernel = (bart,7,0) vardef=n;
instruments  mktrf smb hml umd;
run;
proc model data = myout.portret10t_factor;
parms b0 b1 b2 b3 b4 b5;
      LS = b0+b1*mktrf+b2*smb+b3*hml+b4*cma+b5*rmw;
fit LS /gmm kernel = (bart,7,0) vardef=n;
instruments  mktrf smb hml cma rmw;
run;
proc model data = myout.portret10t_factor;
parms b0 b1 b2 b3 b4 b5 b6;
      LS = b0+b1*mktrf+b2*smb+b3*hml+b4*cma+b5*rmw+b6*umd;
fit LS /gmm kernel = (bart,7,0) vardef=n;
instruments  mktrf smb hml cma rmw umd;
run;
proc model data = myout.portret10t_factor;
parms b0 b1 b2 b3 b4 ;
      LS = b0+b1*mktrf+b2*q_me+b3*q_IA+b4*q_roe;
fit LS /gmm kernel = (bart,7,0) vardef=n;
instruments  mktrf q_me q_IA q_roe;
run;
proc model data = myout.portret10t_factor2;
parms b0 b1 b2 b3 b4 ;
      LS = b0+b1*mktrf+b2*smb+b3*mgmt+b4*perf;
fit LS /gmm kernel = (bart,7,0) vardef=n;
instruments  mktrf smb mgmt perf;
run;

/*Panel B*/
proc univariate data=myout.Eia_unc2 noprint;
where shrcd in (10,11) and exchcd in (1,2,3);
var eia_res;
by date;
output out=myout.nyse_eia  pctlpts=10 20 30 40 50 60 70 80 90 pctlpre=Eia;
run;
/* Use Breakpoints to classify stock only at end of all June's */
proc sql;
create table myout.Eia10 as
select a.permno, a.date, a.me, a.lme, a.wi, a.d1_ia, a.cym, a.exp_ia, a.f1_ia,
       b.eia10, b.eia20, b.eia30, b.eia40, b.eia50, b.eia60, b.eia70, b.eia80, b.eia90,
	   a.eia_unc, a.eia_res
from myout.Eia_unc2 as a, myout.nyse_eia as b
where a.date=b.date;
quit;
data myout.PF_decile;
     set myout.Eia10;
     If eia90 <= eia_res                 then EIAport = '10';
     If eia_res<  eia10                  then EIAport = '1';
     If eia10 <= eia_res < eia20         then EIAport = '2';
     If eia20 <= eia_res < eia30         then EIAport = '3';
     If eia30 <= eia_res < eia40         then EIAport = '4';
     If eia40 <= eia_res < eia50         then EIAport = '5';
     If eia50 <= eia_res < eia60         then EIAport = '6';
     If eia60 <= eia_res < eia70         then EIAport = '7';
     If eia70 <= eia_res < eia80         then EIAport = '8';
     If eia80 <= eia_res < eia90         then EIAport = '9';
if missing(EIAport)=0 then nonmissport=1;
else nonmissport=0;
keep permno date eia_res EIAport nonmissport;
run;
/*Next month value-weighted portfolio returns*/
proc sql;
create table myout.PF_return10 as
select a.date, a.permno, a.ret, a.wi, b.date as portdate format date9., b.EIAport, b.nonmissport
from myq.crsp2 as a, myout.PF_decile as b
where a.permno=b.permno
and intnx('month',a.date,0,'E')=
    intnx('month',b.date,1,'E')
order by date , EIAport;
quit;
proc univariate noprint data=myout.PF_return10;
    var ret;
    by date EIAport;
    weight wi;
    output out = myout.portret10 mean = vwret n = N;
run;
/*Excess returns*/
proc sql;
    create table myout.portret10 as
    select a.date, a.EIAport, (a.vwret-b.rf) as vwdecileret
    from myout.portret10 as a, mydisp.factor as b
    where year(a.date) = b.year and month(a.date) = b.month;
quit;
data myout.portret10; set myout.portret10;
if date > '01Jul1973'd; run;
proc means data=myout.portret10 (keep=EIAport vwdecileret );
class EIAport;
run;
proc transpose data=myout.portret10(keep=date EIAport vwdecileret) out=myout.portret10t (drop=_name_ _label_);
by date;
ID EIAport;
Var vwdecileret;
run;
data myout.portret10t;
     set myout.portret10t;
	 LS=(_10 - _1);
run;
/*Merge with the pricing factors and save (predictedEG: eia_unc sort;*/
/*residualEG: eia_res sort)*/
proc sql;
     create table myt8.residualEG as
	 select a.*,
            b.mktrf, b.smb, b.hml, b.rmw, b.cma, b.umd,
            c.q_me, c.q_IA, c.q_roe
	 from myout.portret10t as a, mydisp.factor as b, myq.q_factor as c
     where year(a.date)=b.year=year(c.date) and
           month(a.date)=b.month=month(c.date);
quit;

/*4.Average return and alphas of the 10-1 portfolio (LS)*/
data myout.portret10t_factor; set myt8.residualEG; run;
proc sql;
     create table myout.portret10t_factor2 as
	 select a.date, a._1, a._2, a._3, a._4, a._5, a._6, a._7, a._8, a._9, a._10, a.LS,
            b.mktrf, b.smb, b.mgmt, b.perf
     from myt8.residualEG as a, mydisp.m4 as b
     where year(a.date)=b.year and
           month(a.date)=b.month;
quit;
proc model data = myout.portret10t_factor;
parms b0 ;
      LS = b0;
fit LS /gmm kernel = (bart,7,0) vardef=n;
instruments/intonly;
run;
proc model data = myout.portret10t_factor;
parms b0 b1 ;
      LS = b0+b1*mktrf;
fit LS /gmm kernel = (bart,7,0) vardef=n;
instruments  mktrf;
run;
proc model data = myout.portret10t_factor;
parms b0 b1 b2 b3 ;
      LS = b0+b1*mktrf+b2*smb+b3*hml;
fit LS /gmm kernel = (bart,7,0) vardef=n;
instruments  mktrf smb hml;
run;
proc model data = myout.portret10t_factor;
parms b0 b1 b2 b3 b4;
      LS = b0+b1*mktrf+b2*smb+b3*hml+b4*umd;
fit LS /gmm kernel = (bart,7,0) vardef=n;
instruments  mktrf smb hml umd;
run;
proc model data = myout.portret10t_factor;
parms b0 b1 b2 b3 b4 b5;
      LS = b0+b1*mktrf+b2*smb+b3*hml+b4*cma+b5*rmw;
fit LS /gmm kernel = (bart,7,0) vardef=n;
instruments  mktrf smb hml cma rmw;
run;
proc model data = myout.portret10t_factor;
parms b0 b1 b2 b3 b4 b5 b6;
      LS = b0+b1*mktrf+b2*smb+b3*hml+b4*cma+b5*rmw+b6*umd;
fit LS /gmm kernel = (bart,7,0) vardef=n;
instruments  mktrf smb hml cma rmw umd;
run;
proc model data = myout.portret10t_factor;
parms b0 b1 b2 b3 b4 ;
      LS = b0+b1*mktrf+b2*q_me+b3*q_IA+b4*q_roe;
fit LS /gmm kernel = (bart,7,0) vardef=n;
instruments  mktrf q_me q_IA q_roe;
run;
proc model data = myout.portret10t_factor2;
parms b0 b1 b2 b3 b4 ;
      LS = b0+b1*mktrf+b2*smb+b3*mgmt+b4*perf;
fit LS /gmm kernel = (bart,7,0) vardef=n;
instruments  mktrf smb mgmt perf;
run;
