/********************************************************************* */
/*  Program        : 4.MakeMimickingfactors_extending.sas               */
/*  Description    : Construct the mimicking uncertainty factors with   */
/*                   the extending-window projection (Section 4.1 and   */
/*                   Table 4, Panels B and D of the paper).             */
/*                   The portfolio weights are re-estimated at the end  */
/*                   of every year y = 1995, ..., 2016 by regressing    */
/*                   each uncertainty shock (UNC = d_tfpstd_star,       */
/*                   macro UNC = d_pcahat, micro UNC = d_pcares) over   */
/*                   the expanding sample 1972-y on the annualized      */
/*                   base assets X = [p2y (SSL), p7y (BMN), p12y (SLN), */
/*                   p17y (BSH), EG] with the six-month reporting gap,  */
/*                   scaled by the absolute sum of the coefficients.    */
/*                   For each estimation year y, FMR3(y) splices the    */
/*                   in-sample fitted mimicking factors of the first    */
/*                   window (1973:07-1997:06, weights through 1995)     */
/*                   with the out-of-sample mimicking factors of the    */
/*                   later windows ((i+1):07-(i+2):06 for weights       */
/*                   through year i), so it covers 1973:07-(y+2):06.    */
/*                   MODEL1 = mimicking total uncertainty factor,       */
/*                   MODEL2 = mimicking macro uncertainty factor        */
/*                   (disp_hat), MODEL3 = mimicking micro uncertainty   */
/*                   factor (disp_res).                                 */
/*  Portfolio mapping (sorting labels use IAPORT S/M/L = low/medium/    */
/*  high investment and ROEport L/N/H = low/medium/high profitability): */
/*    SLL = p2, BMM = p7, SLM = p12, BLH = p17, EG  = EIA factor.     */
/*                                                                                                           */
/*NOTE that this one macro program generates the extending windows */
/*for each factor model (augmented factor model) one-by-one.             */
/*First, users can choose the different pricing factors & mimicking factors*/
/*Second, run 4.FMRegression_extendingwindow.m in Matlab               */
/*                                                                                                          */    
/*  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)     */
/*  Output: output\ FMR1996 .csv - FMR2016 .csv                         */
/*          (annually re-estimated mimicking factors with the base      */
/*          factor model, read by 4.FMRegression_extending.m; the       */
/*          exported file names contain a trailing blank before         */
/*          ".csv")                                                     */
/********************************************************************* */

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

/*1.Estimate the portfolio weights and the mimicking factors for each extending window*/
%macro extending_FM(start);
       %local i;
	   %do i= &start %to 2016;
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 <= &i;
quit;
proc reg data= myout.PCAdisp_basis outest=myout.pca_projection1 edf tableout noprint;
		model d_tfpstd_star =  p17y p7y  p12y p2y eiay;
		model d_pcahat =   p17y  p7y  p12y p2y  eiay;
		model d_pcares =   p17y p7y  p12y p2y eiay;
quit;
data myout.pca_beta;
		set myout.pca_projection1;
		if _TYPE_='PARMS';
		keep _MODEL_   p17y p7y  p12y p2y eiay;
run;
/*Scailing the portfolio weights*/
data myout.pca_beta; set myout.pca_beta;
sumbeta=abs(sum( p17y, p7y,p12y, p2y, eiay));
run;
data myout.pca_beta2; set myout.pca_beta;

	bp7=p7y/sumbeta;
	bp17=p17y/sumbeta;
	bp12=p12y/sumbeta;
	bp2=p2y/sumbeta;
	bp19=eiay/sumbeta;

	keep bp1-bp19 _MODEL_;
run;
/*Monthly mimicking factors of the window*/
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=  p7*bp7+p17*bp17+p12*bp12+p2*bp2+
                   eia*bp19
          ;
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;
/*Out-of-sample mimicking factors of the window: (i+1):07 - (i+2):06*/
data myout.FM&i; set myout.pca_mimickingfactort_m;
     Year = int(var1/100);
     Month = var1-Year*100;
	 if var1 >= (&i+1)*100+07;
	 if var1 <= (&i+2)*100+06;
     run;
/*Full-history mimicking factors of the window: 1973:07 - (i+2):06*/
	 data myout.FMR2&i; set myout.pca_mimickingfactort_m;
     Year = int(var1/100);
     Month = var1-Year*100;
	 if var1 >= 197307;
	 if var1 <= (&i+2)*100+06;
     run;
	 %end;
%mend extending_FM;
%extending_FM(1995);

/*2.Splice the extending-window mimicking factors*/
/*The in-sample factors of the first window (through 1997:06) are followed by*/
/*the out-of-sample factors of the later windows.*/
%macro all_fm2;
       %local i;
	   %do i= 1996 %to 2016;
       proc append base=myout.FMR21995 data=myout.FM&i;
       run;
	   %end;
%mend all_fm2;
%all_fm2;
%macro all_fm3;
       %local i;
	   %do i= 1996 %to 2016;
       data myout.FMR3&i; set myout.FMR21995;
       if var1 >= 197307;
	   if var1 <= (&i+2)*100+06;
       run;
	   %end;
%mend all_fm3;
%all_fm3;

/*3.Merge with the base factor model and export one file per estimation year*/
/*HMX q factor+macro unc*/
%macro all_fmr;
       %local i;
	   %do i= 1996 %to 2016;
       proc sql;
       create table myout.FMR&i as
	   select a.var1,
       /*The base factor model of the extending-window Fama-MacBeth regressions is chosen here.*/
       /*The default is the HXZ q-factors and the mimicking macro uncertainty factor*/
       /*(MODEL2 as disp_hat). For the mimicking micro uncertainty factor, use*/
       /*a.MODEL3 as disp_res instead of a.MODEL2 as disp_hat.*/
	   /*Example) HXZ q-factor model with macro unc mimicking factor*/
       b.mktrf, c.q_me, c.q_ia, c.q_roe, a.MODEL2 as disp_hat, b.rf
	   from myout.FMR3&i 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<=(&i+2)*100+06;
       quit;
       proc export data=myout.FMR&i outfile="&projpath.\output\FMR&i .csv" dbms = csv replace;
       run;
	   %end;
%mend all_fmr;
%all_fmr;
%macro all_fmr;
       %local i;
	   %do i= 1996 %to 2015;
       proc sql;
       create table myout.FMR&i as
	   select a.var1,
       /*The base factor model of the extending-window Fama-MacBeth regressions is chosen here.*/
       /*The default is the HXZ q-factors and the mimicking macro uncertainty factor*/
       /*(MODEL2 as disp_hat). For the mimicking micro uncertainty factor, use*/
       /*a.MODEL3 as disp_res instead of a.MODEL2 as disp_hat.*/
	   /*Example) HXZ q-factor model with macro unc mimicking factor*/
       b.mktrf, c.smb, c.mgmt, c.perf, a.MODEL2 as disp_hat, b.rf
	   from myout.FMR3&i as a, mydisp.factor as b, mydisp.M4 as c
       where a.year=b.year=c.year
       and a.month=b.month=c.month and a.var1>=197307 and a.var1<=(&i+2)*100+06;
       quit;
       proc export data=myout.FMR&i outfile="&projpath.\output\FMR&i .csv" dbms = csv replace;
       run;
	   %end;
%mend all_fmr;
%all_fmr;
