/********************************************************************* */
/*  Program        : 0.JLNunc.sas                                                         */
/*  Description    : Construct the UNCJLN pricing factor (Table 3,      */
/*                   Panel A of the paper). Uncertainty betas are                  */
/*                   estimated as in Equation (1) of Bali, Brown, and           */
/*                   Tang (2017) with 60-month rolling regressions, and      */
/*                   the factor is constructed following Fama and                 */
/*                   French (1993) with a 2x3 size/beta sort.                         */
/*                                                                                                       */
/*  Input : data\ SAS datasets from 0.DataDownload.sas:                    */
/*          monthret_ff, compustat_funda2                                             */
/*          data\ MacroUncertaintyToCirculate.csv,                                */
/*                FinancialUncertaintyToCirculate.csv,                               */
/*                RealUncertaintyToCirculate.csv                                       */
/*                (Jurado, Ludvigson, and Ng (2015) uncertainty series,     */
/*                downloaded from Sydney Ludvigson's website)               */
/*          data\ liquidity_factor (Pastor-Stambaugh traded liquidity       */
/*                factor, downloaded from Robert Stambaugh's website)   */
/*  Output: output\jln\ SAS datasets (mac_factor2, mac_factor2y)       */
/*          output\portret102y2.dta (annualized UNCJLN factor)            */
/********************************************************************* */

%let projpath = D:\SAS\TFP\Dispersion_replication;
libname mydisp "&projpath.\data";
libname myq "&projpath.\output";
libname myq5 "&projpath.\output\q5";
libname myjln "&projpath.\output\jln";
/*CRSP data*/
data myjln.Monthret_ff(keep=permno date prc l_prc ret exret ME lme siccd lme exchcd shrcd mktrf hml umd rf );
     set mydisp.Monthret_ff;
     by permno;
     LME=lag(me);
     if first.permno then LME=me/(1+ret);
	 if (exchcd=1 or exchcd=2 or exchcd=3);
	 if (shrcd=10 or shrcd=11);
     if (siccd<=6000 or siccd>=6999);
	 L_prc=lag(prc);
     if first.permno then L_prc=.;
	 cyear=year(date);
    if prc>=1;
run;
/*Combine the q-factors and the EG factor*/
/*We use Q factors from q-library from Lu Zhang's website*/
/*Merge with Lu's q-factors as well as Pastor's liquidity factor*/
proc sql;
create table myjln.Monthret_ff as                 
select a.*, b.q_me, b.q_ia, b.q_roe, b.eia
from myjln.Monthret_ff as a, mydisp.Qfactors as b
where year(a.date)=b.year and month(a.date)=b.month;
quit;
/*Liquidity factor*/
PROC IMPORT datafile="&projpath.\data\Liquidity_factor.csv"
 	OUT=myjln.Liquidity_factor
 	DBMS=csv
 	REPLACE;
 	GETNAMES=Yes;
RUN;
data myjln.Liquidity_factor; set myjln.Liquidity_factor; 
year=floor(var1/100);
month=var1 - year*100; 
run;
proc sql;
create table myjln.Monthret_ff as                 
select a.*, b.Traded_Liq__LIQ_V_ as liq
from myjln.Monthret_ff as a, myjln.Liquidity_factor as b
where year(a.date)=b.year and month(a.date)=b.month;
quit;
/*Merge with JLN uncertainty series*/
PROC IMPORT datafile="&projpath.\data\MacroUncertaintyToCirculate.csv"
 	OUT=myjln.MacroUncertaintyToCirculate
 	DBMS=csv
 	REPLACE;
 	GETNAMES=Yes;
RUN;
data myjln.MacroUncertaintyToCirculate; set myjln.MacroUncertaintyToCirculate; 
year=year(date);
month=month(date);
run;
PROC IMPORT datafile="&projpath.\data\FinancialUncertaintyToCirculate.csv"
 	OUT=myjln.FinancialUncertaintyToCirculate
 	DBMS=csv
 	REPLACE;
 	GETNAMES=Yes;
RUN;
data myjln.FinancialUncertaintyToCirculate; set myjln.FinancialUncertaintyToCirculate; 
year=year(date);
month=month(date);
run;
PROC IMPORT datafile="&projpath.\data\RealUncertaintyToCirculate.csv"
 	OUT=myjln.RealUncertaintyToCirculate
 	DBMS=csv
 	REPLACE;
 	GETNAMES=Yes;
RUN;
data myjln.RealUncertaintyToCirculate; set myjln.RealUncertaintyToCirculate; 
year=year(date);
month=month(date);
run;
proc sql;
create table myjln.Monthret_all as                 
select a.*, b.h_1 as mac1, b.h_3 as mac3, b.h_12 as mac12,  c.h_1 as fin1, c.h_3 as fin3, c.h_12 as fin12,  d.h_1 as real1, d.h_3 as real3, d.h_12 as real12
from myjln.Monthret_ff as a, myjln.MacroUncertaintyToCirculate as b, myjln.FinancialUncertaintyToCirculate as c, myjln.RealUncertaintyToCirculate as d
where year(a.date)=b.year=c.year=d.year and month(a.date)=b.month=c.month=d.month;
quit;
data myjln.Monthret_all; set myjln.Monthret_all;
if exret ne .; run;
proc sql;
create table myjln.Monthret_rolling as
select a.*, intnx('month',a.date,-59,'b') as begin_date format YYMMDDN8.
from myjln.Monthret_all as a;
quit;
proc sql;
create table myjln.Monthret_rolling as
select a.*, intck('month',a.begin_date,a.date) as numdays
from myjln.Monthret_rolling as a;
quit;
proc sort data=myjln.Monthret_rolling;
by permno date;
run;
proc sql;
create table myjln.Monthret_rolling2 as
select b.permno, b.date, b.begin_date, b.numdays, a.date as rwdate, a.exret, a.mktrf, a.q_me, a.hml, a.umd, a.liq, a.q_ia, a.q_roe,
          a.mac1, a.mac3, a.mac12, a.fin1, a.fin3, a.fin12, a.real1, a.real3, a.real12
from myjln.Monthret_rolling as a, myjln.Monthret_rolling as b
where a.permno = b.permno
and intck("month",a.date,b.date) between 0 and b.numdays 
group by b.permno, b.date
having count(b.permno)>24;
quit;
proc sort data=myjln.Monthret_rolling2;
by permno date rwdate;
run;
* Estimate factor exposures;
proc printto log=junk; run;
proc reg data=myjln.Monthret_rolling2 edf outest=myjln.beta_mac noprint;
    by permno date;
    model exret= mac1 mktrf q_me hml umd liq q_ia q_roe ;
run;
proc printto; run;
proc sql;
create table myjln.PFpool_mac
as select a.permno, a.date , a.mac1 as beta_mac1,b.date as portdate, b.lme, b.exret, b.exchcd
from myjln.beta_mac as a, myjln.monthret_ff as b
where a.permno=b.permno
and intnx('month',b.date,0,'E')= intnx('month',a.date,0,'E')
and a.mac1 ne . 
and a._EDF_ >=28
order by date; 
quit;
/*Keep firms with nonmissing book equity and assets and exclude financials.*/
/*compustat_funda2 (from 0.DataDownload.sas) replaces the former controls dataset.*/
proc sql;
create table myjln.PFpool_mac2
as select a.*
from myjln.PFpool_mac as a, mydisp.compustat_funda2 as b
where a.permno=b.lpermno
and year(a.date)= year(b.datadate)
and (b.sic<=6000 or b.sic>=6999)
and b.be ne .
and b.at ne .
order by date; 
quit;
proc means data=myjln.PFpool_mac2;
var beta_mac1 ;
run;
proc univariate data=myjln.PFpool_mac2 noprint;
where exchcd=1;
var beta_mac1;
by date; 
output out=myjln.factorbp_mac  pctlpts=30 70 pctlpre=mac;
run;
proc univariate data=myjln.PFpool_mac2 noprint;
where exchcd=1;
var lme;
by date; 
output out=myjln.nyse_me  pctlpts=50 pctlpre=ME;
run;
/* Use Breakpoints to classify stock only at end of all June's */
proc sql;
create table myjln.PFpool_mac2 as
select a.*, b.mac30, b.mac70, c.me50
from myjln.PFpool_mac2 as a, myjln.factorbp_mac as b, myjln.nyse_me as c
where a.date=b.date=c.date;
quit;
data myjln.PF_sorting;
     set myjln.PFpool_mac2;
     if lme <= ME50                                                                  then SIZEport = 'S';
     else if lme > ME50                                                             then SIZEport = 'B';
     if beta_mac1 <= mac30                                                      then macport  = 'L';
     else if beta_mac1 >  mac30 and beta_mac1 <= mac70      then macport  = 'M';
     else if beta_mac1  > mac70                                                then macport  = 'H';
if missing(sizeport)=0 and missing(macport)=0 then nonmissport=1;
else nonmissport=0;
keep permno date SIZEport macport nonmissport;
run;
proc sql;
create table myjln.PF_return as
select a.date, a.permno, a.exret, a.ret, a.lme, b.date as portdate format date9., b.SIZEport, b.macport, b.nonmissport
from myjln.monthret_ff as a, myjln.PF_sorting as b
where a.permno=b.permno 
and intnx('month',a.date,0,'E')=
    intnx('month',b.date,1,'E')
and a.exret ne .
and (a.shrcd=10 or a.shrcd=11)
order by date , SIZEport, macport;
quit;
proc univariate noprint data=myjln.PF_return;
    var ret;
    by date SIZEport macport;
    weight lme;
    output out = myjln.portret mean = vwret n = N;
run;
proc transpose data=myjln.portret(keep=date SIZEport macport vwret) out=myjln.portret2 (drop=_name_ _label_);
by date;
ID SIZEport macport;
Var vwret;
run;
data myjln.mac_factor;
     set myjln.portret2;
	 High=(BH+SH)/2;
	 Low=(BL+SL)/2;
	 mac=High - Low;
run;
data myjln.mac_factor2; set myjln.mac_factor;
if date > '01Jul1972'd; 
if date < '01Jul2018'd; run;
proc means data=myjln.mac_factor2;
run;
/*Annualize mac factor*/
proc printto log = junk; run;
proc expand data = myjln.mac_factor2 out=myjln.mac_factor2y method=none;
    id date;
    convert BH = BHy  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert BL = BLy  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert BM = BMy  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert SH = SHy  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert SL = SLy  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
	convert SM = SMy  / transformin=(+1) transformout=(MOVPROD  12 -1 trimleft  11);
quit;
proc printto; run;
data myjln.mac_factor2y; set myjln.mac_factor2y;
     Highy=(BHy+SHy)/2;
	 Lowy=(BLy+SLy)/2;
	 ls=Highy - Lowy;
	 If Month(date)=6; 
run;
proc means data=myjln.mac_factor2y;
run;
proc export data=myjln.mac_factor2y outfile="&projpath.\output\portret102y2.dta" replace;
run;
