/********************************************************************* */
/*  Program        : 4.MakeMimickingfactors.sas                                        */
/*  Description    : Construct the mimicking uncertainty portfolios by        */
/*                   projecting the uncertainty shocks on the base                      */
/*                   assets, using the full sample (Section 4.1 and                     */
/*                   Table 4 of the paper).                                                        */
/*                   The portfolio weights are estimated by regressing              */
/*                   each uncertainty shock (UNC = d_tfpstd_star,                    */
/*                   macro UNC = d_pcahat, micro UNC = d_pcares) on the     */
/*                   annualized base assets X = [BMH BLL BLM SLM BLH    */
/*                   EG] with the six-month reporting gap, and scaled               */
/*                   by the absolute sum of the coefficients.                                */
/*                   The monthly mimicking factors are the monthly                   */
/*                   base asset returns multiplied by the estimated                       */
/*                   weights:                                                                              */
/*                   disp = mimicking total uncertainty factor (UNC),                 */
/*                   disp_hat = mimicking macro uncertainty factor,                    */
/*                   disp_res = mimicking micro uncertainty factor.                     */
/*                                                                                                              */
/*  Input : data\   dispersion_all (from 0.MakeDispersion.sas:                     */
/*          d_tfpstd_star, d_pcahat, d_pcares by cyear)                                  */
/*          data\   factor (from 0.DataDownload.sas: monthly FF                   */
/*          factors and UMD)                                                                       */
/*          output\ basisy2, basis2 (from Table4\4.MakeBaseassets.sas)        */
/*          output\ q_factor (from 0.Qfactorconstruction.sas)                        */
/*          output\q5\ EIA_factor (from 0.Q5factorconstruction.sas)             */
/*          Table4\ m4 (Stambaugh and Yuan (2017) monthly factors,          */
/*          downloaded from Robert Stambaugh's website)                            */
/*  Output: output\ all_factor_m_pca  (monthly mimicking factors with      */
/*          the FF, q, and EG factors, 197307-201806)                                   */
/*          output\ all_factor_m_pca2 (monthly mimicking factors with          */
/*          the SY factors, 197307-201612)                                                    */
/********************************************************************* */

%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";

/*1.Estimate the portfolio weights with the six-month reporting gap*/
proc sql;
     create table myout.PCAdisp_basis as
	 select *
	 from myout.dispersion_all as a, myout.basisy2 as b
	 where a.cyear+1=b.year and b.month=6 and 1972 <= a.cyear <= 2016;
quit;
proc reg data= myout.PCAdisp_basis outest=myout.pca_projection1 edf tableout noprint;
        model d_tfpstd_star = BMH BLL BLM SLM BLH EG;
		model d_pcahat =  BMH BLL BLM SLM BLH EG;
		model d_pcares =  BMH BLL BLM SLM BLH EG;
quit;
data myout.pca_beta;
		set myout.pca_projection1;
		if _TYPE_='PARMS';
		keep _MODEL_  BMH BLL BLM SLM BLH EG;
run;
/*Scailing the portfolio weights*/
data myout.pca_beta; set myout.pca_beta;
sumbeta=abs(sum(BMH, BLL, BLM, SLM, BLH, EG));
run;
data myout.pca_beta2; set myout.pca_beta;
    bp1=BMH/sumbeta;
	bp2=BLL/sumbeta;
	bp3=BLM/sumbeta;
	bp4=SLM/sumbeta;
	bp5=BLH/sumbeta;
	bp6=EG/sumbeta;
	keep bp1-bp6 _MODEL_;
run;
proc print data=myout.pca_beta2; run;

/*2.Annual mimicking portfolios*/
proc sql;
     create table myout.pca_basis_june as
	 select *
	 from myout.dispersion_all as a, myout.basisy2 as b
	 where a.cyear=b.year-1 and b.month=6 and 1972 <= a.cyear <= 2016;
quit;
proc sql;
  create table myout.pca_projection2 as
  select a.*, b.*
  from myout.pca_basis_june as a, myout.pca_beta2 as b;
quit;
proc sort data=myout.pca_projection2; by cyear; run;
data myout.pca_mimickingfactor; set myout.pca_projection2;
DISPfactor= BMH*bp1+BLL*bp2+BLM*bp3+SLM*bp4+BLH*bp5+EG*bp6;
run;
data myout.pca_mimickingfactor2; set myout.pca_mimickingfactor;
keep var1 year month _MODEL_ d_tfpstd_star d_pcahat d_pcares DISPfactor;
run;
proc transpose data=myout.pca_mimickingfactor out=myout.pca_mimickingfactort(drop=_NAME_) ;
by var1;
ID _MODEL_;
var DISPfactor;
run;
/*UNC mimicking portfolio: disp*/
/*macro UNC mimicking portfolio: disp_hat*/
/*micro UNC mimicking portfolio: disp_res*/
proc sql;
     create table myout.pca_basis_factor as
	 select a.cyear, b.year, b.month, b.var1,
            a.d_tfpstd_star, a.d_pcahat, a.d_pcares, c.MODEL1 as disp, c.Model2 as disp_hat, c.Model3 as disp_res
	 from myout.dispersion_all as a, myout.basisy2 as b, myout.pca_mimickingfactort as c
	 where a.cyear=b.year-1 and b.month=6 and 1972 <= a.cyear <= 2016
     and b.var1=c.var1;
quit;
/*Estimate the correlation between UNC and mimicking portfolios*/
proc corr data=myout.pca_basis_factor;
var d_tfpstd_star d_pcahat d_pcares disp disp_hat disp_res;
run;

/*3.Monthly mimicking portfolios*/
proc sql;
  create table myout.pca_projection_m as
  select a.*, b.*
  from myout.basis2 as a, myout.pca_beta2 as b;
quit;
proc sort data=myout.pca_projection_m; by var1; run;
data myout.pca_mimickingfactor_m; set myout.pca_projection_m;
DISPfactor=  BMH*bp1+BLL*bp2+BLM*bp3+SLM*bp4+BLH*bp5+EG*bp6;
run;
data myout.pca_mimickingfactor2_m; set myout.pca_mimickingfactor_m;
keep var1 year month _MODEL_ DISPfactor;
run;
proc transpose data=myout.pca_mimickingfactor2_m out=myout.pca_mimickingfactort_m(drop=_NAME_) ;
by var1;
ID _MODEL_;
var DISPfactor;
run;
data myout.pca_mimickingfactort_m; set myout.pca_mimickingfactort_m;
Year = int(var1/100);
Month = var1-Year*100;
run;
/*Merge the monthly mimicking factors with the FF, q, and EG factors*/
proc sql;
     create table myout.all_factor_m_pca as
	 select a.var1, a.year, a.month,
            a.MODEL1 as disp, a.Model2 as disp_hat, a.Model3 as disp_res,
            b.SMB, b.mktrf, b.hml, b.rmw, b.cma, b.umd, b.rf, c.q_me, c.q_IA, c.q_roe, d.EIA
	 from myout.pca_mimickingfactort_m as a, mydisp.factor as b, myq.q_factor as c, myq5.EIA_factor as d
     where a.year=b.year=year(c.date)=d.year
     and a.month=b.month=month(c.date)=d.month and a.var1>=197307 and a.var1<=201806;
quit;
/*Merge the monthly mimicking factors with the SY factors*/
proc sql;
     create table myout.all_factor_m_pca2 as
	 select a.var1, a.year, a.month,
            a.MODEL1 as disp, a.Model2 as disp_hat, a.Model3 as disp_res,
            b.mktrf, b.smb, b.mgmt, b.perf, d.eia
	 from myout.pca_mimickingfactort_m as a, myt4.m4 as b, myq5.EIA_factor as d
     where a.year=b.year=d.year
     and a.month=b.month=d.month and a.var1>=197307 and a.var1<=201612;
quit;
/*Estimate the summary statistics of mimicking factors and other pricing factors*/
proc corr data=myout.all_factor_m_pca;
var mktrf HML CMA RMW SMB q_me q_IA q_ROE umd eia disp disp_hat disp_res;
run;
