/********************************************************************* */
/*  Program        : 4.MakeBaseassets.sas                                                     */
/*  Description    : Construct the base assets used to build the                       */
/*                   mimicking uncertainty factors (Section 4.1 and                      */
/*                   Table 4 of the paper).                                                             */
/*                   The base assets are X = [BMH, BLL, BLM, SLM, BLH,       */
/*                   EG]: five of the eighteen size/investment/                               */
/*                   profitability portfolios from                                                    */
/*                   0.Qfactorconstruction.sas plus the EG factor from                  */
/*                   0.Q5factorconstruction.sas.                                                    */
/*                                                                                                                */
/*  Input : output\  SAS datasets from 0.Qfactorconstruction.sas:                  */
/*          portret2 (18 monthly portfolio returns)                                            */
/*          data\    SAS datasets from 0.DataDownload.sas: ff5                        */
/*          output\q5\ SAS datasets from 0.Q5factorconstruction.sas:               */
/*          EIA_factor (monthly EG), EIA_factory (annualized EG)                */
/*  Output: output\ SAS datasets:                                                                 */
/*          basis2  (monthly base assets; excess returns p1-p18 and                  */
/*                   the named base assets BMH BLL BLM SLM BLH EG)        */
/*          basisy2 (annualized base assets; excess returns p1y-p18y                 */
/*                   and the named base assets BMH BLL BLM SLM BLH EG)  */
/*                                                                      */
/*  Portfolio mapping (sorting labels use IAPORT S/M/L = low/medium/      */
/*  high investment and ROEport L/N/H = low/medium/high profitability):    */
/*    BMH = p6  (BMH), BLL = p8  (BSL), BLM = p9  (BSN),                   */
/*    SLM = p12 (SLN), BLH = p17 (BSH), EG  = EIA factor.                     */
/********************************************************************* */

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

/*1.Monthly base assets: 18 portfolio returns + EG factor*/
proc sql;
     create table myout.basis as
	 select a.date, b.var1, b.year, b.month,
	        a.smn as y1, a.ssl as y2, a.ssn as y3, a.blh as y4, a.bll as y5, a.bmh as y6,
            a.bmn as y7, a.bsl as y8, a.bsn as y9, a.slh as y10, a.sll as y11, a.sln as y12,
            a.smh as y13, a.sml as y14, a.bln as y15, a.bml as y16, a.bsh as y17, a.ssh as y18,
            a.smn-b.rf as p1, a.ssl-b.rf as p2, a.ssn-b.rf as p3, a.blh-b.rf as p4, a.bll-b.rf as p5, a.bmh-b.rf as p6,
            a.bmn-b.rf as p7, a.bsl-b.rf as p8, a.bsn-b.rf as p9, a.slh-b.rf as p10, a.sll-b.rf as p11, a.sln-b.rf as p12,
            a.smh-b.rf as p13, a.sml-b.rf as p14, a.bln-b.rf as p15, a.bml-b.rf as p16, a.bsh-b.rf as p17, a.ssh-b.rf as p18,
            b.rf, c.EIA as EIAy, d.EIA
	 from myq.portret2 as a, mydisp.ff5 as b, myq5.EIA_factory as c, myq5.EIA_factor as d
	 where year(a.date)=b.year=c.year=year(d.date)
     and month(a.date)=b.month=c.month=month(d.date)
     and 197201 <= b.var1 <= 201812;
quit;
proc sort data=myout.basis; by date; run;
/*Monthly base assets with the names used in the paper*/
data myout.basis2;
     set myout.basis;
	 BMH=p6;
	 BLL=p8;
	 BLM=p9;
	 SLM=p12;
	 BLH=p17;
	 EG=EIA;
run;

/*2.Annualize the base assets*/
proc printto log = junk; run;
proc expand data = myout.basis out=myout.basisy method=none;
    id var1;
    convert y1 = p1a  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert y2 = p2a  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert y3 = p3a  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert y4 = p4a  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert y5 = p5a  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert y6 = p6a  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert y7 = p7a  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert y8 = p8a  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert y9 = p9a  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert y10 = p10a  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert y11 = p11a  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert y12 = p12a  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert y13 = p13a  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert y14 = p14a  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert y15 = p15a  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert y16 = p16a  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert y17 = p17a  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert y18 = p18a  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert rf = rfy  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
quit;
proc printto; run;
/*Annualized excess returns*/
data myout.basisy(drop=y1 - y18 p1a p2a p3a p4a p5a p6a p7a p8a p9a p10a p11a p12a p13a p14a p15a p16a p17a p18a );
     set myout.basisy;
	 p1y=p1a - rfy;
	 p2y=p2a - rfy;
	 p3y=p3a - rfy;
	 p4y=p4a - rfy;
	 p5y=p5a - rfy;
	 p6y=p6a - rfy;
	 p7y=p7a - rfy;
	 p8y=p8a - rfy;
	 p9y=p9a - rfy;
	 p10y=p10a - rfy;
	 p11y=p11a - rfy;
	 p12y=p12a - rfy;
	 p13y=p13a - rfy;
	 p14y=p14a - rfy;
	 p15y=p15a - rfy;
	 p16y=p16a - rfy;
	 p17y=p17a - rfy;
	 p18y=p18a - rfy;
run;
/*Annualized base assets with the names used in the paper*/
data myout.basisy2;
     set myout.basisy;
	 BMH=p6y;
	 BLL=p8y;
	 BLM=p9y;
	 SLM=p12y;
	 BLH=p17y;
	 EG=EIAy;
run;
