**************************************************************************************************************;
**************************************************************************************************************;
**PURPOSE: CREATES DATASET FOR "MEASURING RISK INFORMATION" BY KEVIN SMITH AND ERIC SO;
**************************************************************************************************************;
**************************************************************************************************************;



**************************************************************************************************************;
**************************************************************************************************************;
**NOTE: THE FIRST PART OF THE CODE BELOW RELIES ON A REMOTE SUBMIT COMMAND ("RSUBMIT")...;
***		TO CONNECT TO THE WHARTON RESEARCH DATA SERVICES (WRDS) PLATFORM....;
***		THIS PORTION OF THE CODE ENDS ON THE COMMAND ENDRSUBMIT;
**************************************************************************************************************;
**************************************************************************************************************;

rsubmit;
***SET YOUR MYDIR FILE TO YOUR RESPECTIVE LOCATION ON WRDS***;
/*EXAMPLE: libname mydir '/scratch/';*/


**************************************************************************************************************;
**************************************************************************************************************;
**STEP 1: BRING IN QUARTERLY COMPUSTAT DATA;
**DESC: THIS SECTION CREATES A BASE SAMPLE OF COMPUSTAT QUARTERLY...;
**		EARNINGS ANNOUNCEMENT DATES WITH FUNDAMENTAL SIGNALS;
**************************************************************************************************************;
**************************************************************************************************************;
proc sql;
	create table ISDCON as
	select distinct substr(CUSIP,1,8) as CUSIP, gvkey as GVKEY, cik, rdq as CompDate, datadate as FQE,
	datafqtr, fyearq as fiscaly, fqtr as fiscalq,
	ceqq as BE,
	(prccq) as CompPrice,
	atq/cshoq as TA,
	cshoq as CSO,
	epspxq as EPS,
	(IBQ)/ATQ as ROA,
	(IBQ)/CEQQ as ROE,	
	(max(LTQ,0))/CEQQ as LEV,
	(REVTQ-COGSQ)/ATQ as GP,
	IBQ as NIB,
	oancfy as CF,
	SCFQ as SCF 						label = 'CASH FLOW Format',
	FOPTY as FOPT 						label = 'FUNDS FROM OPERATIONS',
	WCAPCY as WCAPC 					label = 'CHANGE IN WORKING CAPITAL'	
	from comp.fundq
	where (rdq gt '01JAN1990'd) &
	(rdq ne .) &
	(CUSIP ne '') & 
	(Consol='C' and Indfmt='INDL' and Datafmt='STD' and Popsrc='D')
	order by GVKEY;
run;

proc sql;
	create table ISDCON1  as
	select distinct A.*, B.SIC as DNUM
	from ISDCON A left outer join compa.namesq B
	on (A.GVKEY=B.GVKEY) 
	order by GVKEY, CompDate;
quit;
proc sort data=ISDCON1 out=ISDCON1 nodupkey; by GVKEY CompDate; run;

ods exclude all; 
proc expand data=ISDCON1 out=ISDCON2 method=none;

	convert EPS = LagEPS4 / transformout=(lag 4 );
	convert roa=cvol / transformout=(MOVSTD 8 trimleft 4);
   	convert CompDate= LagCompDate1 / transformout=(lag 1 );
   	convert BE= LagBE1 / transformout=(lag 1 );
   	convert TA= LagTA1 / transformout=(lag 1 );
	by GVKEY;
	id CompDate;
run;
ods exclude none; 

data ISDCON3;
	set ISDCON2;
	QAE=EPS-LagEPS4;
run;

proc expand data=ISDCON3 out=mydir.ISDCOMP method=none;

	convert QAE=QAE_STD / transformout=(MOVSTD 8 trim 4);

	by GVKEY;
	id CompDate;
run;

proc sort data=mydir.ISDCOMP nodupkey; by GVKEY CompDate; run;


**************************************************************************************************************;
**************************************************************************************************************;
**STEP 2: BRING IN IBES DATA;
**DESC: THIS SECTION CREATES A SAMPLE OF ANALYST-BASED EARNINGS SURPRISES FROM IBES...;
**		USING THE IBES SUMMARY STATISTICS FILE;
**************************************************************************************************************;
**************************************************************************************************************;
data IBESAEP(keep=Actual CUSIP Ticker Fiscalp Fpedats FPI MEANEST MEDEST STDEV ANNDATS_ACT ANNTIMS_ACT STATPERS NUMEST);
	retain CUSIP ANNDATS_ACT STATPERS FPEDATS;
	set ibes.statsum_epsus(where=(('01JAN1970'd le ANNDATS_ACT) and fpi='6' and cusip ne '00000000' and cusip ne '' and meanest ne . and measure eq 'EPS' and actual ne . and ANNDATS_ACT ne . and (statpers lt ANNDATS_ACT)));
run;
proc sort data=IBESAEP; by CUSIP ANNDATS_ACT statpers; run;
data IBESAEP1(rename=(ANNDATS_ACT=IbesDate ANNTIMS_ACT=IbesTime)); 
	set IBESAEP; 
	by CUSIP ANNDATS_ACT statpers; 
	if last.ANNDATS_ACT; 
	UFE=(Actual-Medest);
run;

proc sql;
	create table IBESAEP3 as
	select distinct A.*, B.STDEV as FSTDEV, B.StatPers as FStatPers
	from IBESAEP1 A left outer join ibes.statsum_epsus B
	on (A.CUSIP=B.CUSIP)
	where (B.FPI eq '6')
	& (intck('Month', B.StatPers, B.Fpedats) gt 0)
	& (B.StatPers le A.StatPers)
	group by A.CUSIP, A.IbesDate
	having B.statpers=max(B.statpers);
quit;

proc sort data=IBESAEP3 nodupkey; by CUSIP IbesDate; run;
proc expand data=IBESAEP3 out=mydir.ISDIBES method=none;
	convert  UFE= LagUFE1 / transformout=(lag 1 );
	convert  IbesDate= LagIbesDate1 / transformout=(lag 1 );
	by CUSIP;
	id IbesDate;
run;



**************************************************************************************************************;
**************************************************************************************************************;
**STEP 3: MERGE COMPUSTAT AND IBES DATA;
**DESC: THIS SECTION MERGES TOGETHER THE COMPUSTAT AND IBES FILES CREATED IN STEPS 1 AND 2 ABOVE...;
**		AND APPLIES THE TECHNIQUE FROM DELLAVIGNA AND POLLET (2009, JF) TO IDENTIFY THE ANNOUNCEMENT DATE;
**************************************************************************************************************;
**************************************************************************************************************;
proc sort data=mydir.ISDIBES out=ISDIBES nodupkey; by CUSIP IbesDate; run;
proc sort data=mydir.ISDCOMP out=ISDCOMP nodupkey; by CUSIP CompDate; run;
proc sql;
	create table ISDBoth as
	select distinct *
	from ISDCOMP a, ISDIBES b
	where (A.CUSIP=B.CUSIP) 
	& (-5 le intck('WEEKDAY', A.CompDate, B.IbesDate) le 5)
	order by A.CUSIP, A.CompDate;
quit;


data mydir.SSDEAP(drop=ActDate_Temp LagIbesDate1 IBESTIME  FPI MEDEST STATPERS FStatPers ACTUAL);
	attrib ActDate CompDate LagCompDate1 IbesDate format=YYMMDDn8.;
	retain GVKEY CUSIP ACTDATE CompDate IbesDate;
	
	set ISDBoth(where=(CompDate ne .));
	if IbesDate ne . and (IbesDate lt CompDate) then ACTDATE_temp=IbesDate;
		else ACTDATE_temp=CompDate;
	if (IbesTime ge '16:00:00't) then ActDate=intnx('WeekDay', Actdate_Temp, 1);
		else ActDate=ActDate_Temp;
	
	if numest eq . then COV=0;
		else COV=numest;
	if UFE ne . and CompPrice gt 0 then SURP=UFE/CompPrice;

run;

**************************************************************************************************************;
**************************************************************************************************************;
**STEP 4: CREATE LINK FILE BETWEEN COMPUSTAT AND CRSP
**DESC: THIS SECTION MERGES DATA FROM COMPUSTAT AND CRSP;
**************************************************************************************************************;
**************************************************************************************************************;
%let begdate = "01JAN1990"d;
%let enddate = "31DEC2019"d;
%let consol  = %str(and CONSOL in ('N' 'C' 'P'));  * all possible values of CONSOLidation level: C, N, R, P, D;
%let indfmt  = %str(and INDFMT in ('INDL'));        * all possible values of INDustry ForMaT: INDL, FS;
%let datafmt = %str(and DATAFMT in ('STD'));     * all possible values of DATA ForMaT: STD, SUMM_STD, PRE_AMENDS, PRE_AMENDSS;
%let popsrc  = %str(and POPSRC in ('D'));             * all possible values of POPulation SouRCe: D, I ; 
%let curcd   = %str(and CURCD in ('USD' 'CAD')); * all possible values of CURrenCy: USD, CAD;
%let costat  = %str(and COSTAT in ('A' 'I'));          * all possible values of COmpany STATus: A, I;
%let andlu = %str(and USEDFLAG = 1);
%let andlt = %str(and LINKTYPE in ('LC' 'LU' 'LX' 'LD' 'LS' 'LN')); 
%let andld = and (LINKDT <= datadate or LINKDT = .B) and (datadate <= LINKENDDT or LINKENDDT = .E);
%let keepvars = %upcase(gvkey datadate fyear fyr consol indfmt datafmt popsrc curcd costat);
%let dataitem = %upcase(cik tic at sale cusip);
%let fn_dc    = %upcase(at_fn at_dc sale_fn sale_dc);
%let hdrvars  = %upcase(sic fyrc conml city);   

%let inputfile=mydir.SSDEAP;
%let inputdate=ActDate;

proc sql;
    create table lnk as 
	select c.*, keys.&inputdate
        from &inputfile as keys, crspa.CCMXPF_LINKTABLE as c
        where keys.gvkey = c.gvkey
              & (year(&enddate)+1 >=year(LINKDT) or LINKDT=.B);
quit;
proc sql;
    create table temp1 as
        select distinct *
        from lnk as a, comp.funda (keep=&keepvars &dataitem) as b
        where (a.gvkey = b.gvkey) and 
	&begdate < a.&inputdate <= &enddate 
	&andld &andlu &andlt &consol &indfmt &datafmt &popsrc &curcd &costat;
quit;
proc sort data=&inputfile out=InputFile nodupkey; by GVKEY &inputdate; run;
proc sort data=temp1 nodupkey; by GVKEY &inputdate; run;
data Temp2(rename=(LPERMNO=PERMNO));
	merge temp1(in=a) InputFile(in=b);
	by GVKEY &inputdate;
	if a and b;
run;
proc sql;
	create table Temp3 as
	select distinct A.GVKEY, A.Permno, A.&inputdate,
	B.Permno IS NOT NULL as SNP
	from Temp2 A left outer join crspa.msp500list B
	on (A.Permno=B.Permno)
	& (B.start le A.&inputdate le B.ending)
	order by A.Permno, A.&inputdate;
quit;
proc sql;
	create table Temp4 as
	select distinct A.*, B.CAPN as NYSD
	from Temp3 A left outer join crsp.mport1 B
	on (A.Permno=B.Permno)
	& (Year(A.&inputdate)=(1+B.Year))
	order by A.Permno, A.&inputdate;
quit;
proc sort data=Temp4; by GVKEY &inputdate; run;
proc sort data=mydir.SSDEAP out=SSDEAP; by GVKEY &inputdate; run;

data mydir.SSBase;
	retain GVKEY PERMNO CUSIP &inputdate;
	merge SSDEAP(in=a) Temp4(in=b);
	by GVKEY &inputdate; 
	if a and b;
	SIC=floor(DNUM/100);
	NYSQ=floor(NYSD/2);
run;

**************************************************************************************************************;
**************************************************************************************************************;
**STEP 5: ADD OPTION DATA;
**DESC: THIS SECTION CREATES DATA BASED ON OPTION IMPLIED VOLATILITES;
**************************************************************************************************************;
**************************************************************************************************************;


proc datasets nolist; delete OptBase BSTYOD; quit;

ods exclude all; 

%macro myear(yr, pyr);
data BSTYOD;
	set optionm.stdopd&yr optionm.stdopd&pyr;
run;
proc sql;
	create table C_&yr as
	select distinct B.CUSIP, B.ActDate, &yr as DYR,
	C.Date, C.DAYS,
	C.impl_volatility as IV, 
	intck('WeekDay', B.ActDate, C.Date) as TD 
	from mydir.SSBase B, BSTYOD C, optionm.securd D
	where (C.SECID=D.SECID) & (D.CUSIP=B.CUSIP) &
	( (-10 le intck('WEEKDAY', B.ActDate, C.DATE) le 10) or (intck('WEEKDAY', C.DATE, B.ActDate) eq 32) ) &
	(C.CP_flag eq 'C') &
	(abs(C.impl_volatility) lt 10) &
	(C.DAYS eq 30 or C.DAYS eq 60 or C.DAYS eq 122 or C.DAYS eq 182 or C.DAYS eq 365)
	order by B.CUSIP, B.ActDate, C.Date, C.Days;
quit;
proc sort data=C_&yr nodupkey; by CUSIP ActDate Date Days; run;
proc append data=C_&yr base=OptBase force; run;
proc datasets nolist; delete C_&yr; quit;

%mend;
%myear(2019, 2018);
%myear(2018, 2017);
%myear(2017, 2016);
%myear(2016, 2015);
%myear(2015, 2014);
%myear(2014, 2013);
%myear(2013, 2012);
%myear(2012, 2011);
%myear(2011, 2010);
%myear(2010, 2009);
%myear(2009, 2008);
%myear(2008, 2007);
%myear(2007, 2006);
%myear(2006, 2005);
%myear(2005, 2004);
%myear(2004, 2003);
%myear(2003, 2002);
%myear(2002, 2001);
%myear(2001, 2000);
%myear(2000, 1999);
%myear(1999, 1998);
%myear(1998, 1997);
%myear(1997, 1996);

ods exclude none;     

proc sort data=OptBase; by CUSIP ACTDATE TD Days DYR; run;
data BSTSE1;
	set OptBase;
	by CUSIP ACTDATE TD Days DYR;
	if last.DAYS;
run;
%macro var(vname);
proc transpose data=BSTSE1 out=T_&vname(drop=_name_) prefix=&vname;
	id Days;
	var &vname;
	by CUSIP ACTDATE TD;
run;
%mend var;
%var(IV);

%macro var(vname);
proc transpose data=T_IV out=T_&vname(drop=_name_) prefix=&vname;
	id TD;
	var &vname;
	by CUSIP ACTDATE;
run;
%mend var;
%var(IV30);
%var(IV60);
%var(IV122);
%var(IV182);
%var(IV365);

data SSIV_Pre(where=(SIGQ_Pre1 ne . and SIGB_Pre1 ne .));
	set T_IV(where=(td eq -2));
	SIGQ_Pre1=(IV30-IV60)/(252/30 - 252/60);
	SIGB_Pre1=IV30-((SIGQ_Pre1)/30);
run;
data SSIV_Post(where=(SIGQ_Post1 ne . and SIGB_Post1 ne .));
	set T_IV(where=(td eq 1));
	SIGQ_Post1=(IV30-IV60)/(252/30 - 252/60);
	SIGB_Post1=IV30-((SIGQ_Post1)/30);
run;

data mydir.SSIV1;
	merge SSIV_Pre SSIV_Post T_IV30 T_IV60 T_IV122 T_IV182 T_IV365;
	by CUSIP ActDate;
run;



***CALCULATE CHANGE IN VIX***;
proc sql;
	create table mydir.SSVIXPre as
	select distinct A.Permno, A.ActDate,
	B.VIX as VIXPre
	from mydir.ssbase A, CBOE.CBOE B
	where (intck('WeekDay', B.DATE, A.ActDate) eq 2)
	order by A.PERMNO, A.ActDate;
quit;

proc sql;
	create table mydir.SSVIXPost as
	select distinct A.Permno, A.ActDate,
	B.VIX as VIXPost
	from mydir.ssbase A, CBOE.CBOE B
	where (intck('WeekDay', B.DATE, A.ActDate) eq -2)
	order by A.PERMNO, A.ActDate;
quit;




**************************************************************************************************************;
**************************************************************************************************************;
**STEP 6: BRING IN DATA ON RETURNS, LIQUIDITY, AND TRADING VOLUMES FROM CRSP;
**DESC: THIS SECTION CREATES DATA ON MARKET OUTCOMES;
**************************************************************************************************************;
**************************************************************************************************************;


proc sql;
	create table mydir.SSV1 as
	select distinct A.Permno, A.ActDate,
	std(B.Ret-C.EWRETD) as VMA_257_5,
	std(B.Ret) as VRW_257_5,
	mean(B.VOL/B.SHROUT) as TO_257_5
	from mydir.SSBase A, crsp.dsf B, crsp.dsi C
	where (A.Permno=B.Permno)
	& (B.Date=C.Date)
	& (-257 le intck('WeekDay', A.ActDate, B.DATE) le -5)
	group by A.PERMNO, A.ActDate;
quit;

proc sql;
	create table mydir.SSV2 as
	select distinct A.Permno, A.ActDate,
	std(B.Ret-C.EWRETD) as VMA_5_257,
	std(B.Ret) as VRW_5_257,
	mean(B.VOL/B.SHROUT) as TO_5_257
	from mydir.SSBase A, crsp.dsf B, crsp.dsi C
	where (A.Permno=B.Permno)
	& (B.Date=C.Date)
	& (5 le intck('WeekDay', A.ActDate, B.DATE) le 257)
	group by A.PERMNO, A.ActDate;
quit;

proc sql;
	create table mydir.SSV3 as
	select distinct A.Permno, A.ActDate,
	std(B.Ret-C.EWRETD) as VMA_5_26,
	std(B.Ret) as VRW_5_26,
	mean(B.VOL/B.SHROUT) as TO_5_26,
	mean( (abs(B.Ret)/(abs(B.PRC)*(B.VOL/1000000))) ) as ALR_5_26,
	sum(B.Vol*((B.Ask-B.Bid)/(0.5*B.Ask+0.5*B.Bid)))/sum(B.Vol) as BAS_5_26
	from mydir.SSBase A, crsp.dsf B, crsp.dsi C
	where (A.Permno=B.Permno)
	& (B.Date=C.Date)
	& (5 le intck('WeekDay', A.ActDate, B.DATE) le 26)
	group by A.PERMNO, A.ActDate;
quit;

proc sql;
	create table mydir.SSV4 as
	select distinct A.Permno, A.ActDate,
	std(B.Ret-C.EWRETD) as VMA_27_257,
	std(B.Ret) as VRW_27_257,
	mean(B.VOL/B.SHROUT) as TO_27_257
	from mydir.SSBase A, crsp.dsf B, crsp.dsi C
	where (A.Permno=B.Permno)
	& (B.Date=C.Date)
	& (27 le intck('WeekDay', A.ActDate, B.DATE) le 257)
	group by A.PERMNO, A.ActDate;
quit;

proc sql;
	create table mydir.SSV5 as
	select distinct A.Permno, A.ActDate,
	100*(exp(sum(log(B.RET+1)))-exp(sum(log(C.EWRETD+1)))) as EANRET,
	100*(exp(sum(log(B.RET+1)))-1) as EANRR,
	mean(B.VOL/B.SHROUT) as EANTO,
	mean(D.VIX) as EAVIX
	from mydir.SSBase A, crsp.dsf B, crsp.dsi C, CBOE.CBOE D
	where (A.Permno=B.Permno)
	& (B.Date=C.Date=D.Date)
	& (-1 le intck('WeekDay', A.ActDate, B.DATE) le 1)
	group by A.PERMNO, A.ActDate;
quit;


proc sql;
	create table mydir.SSV6 as
	select distinct A.Permno, A.ActDate,
	std(B.Ret-C.EWRETD) as VMA_27_127,
	std(B.Ret) as VRW_27_127,
	mean(B.VOL/B.SHROUT) as TO_27_127
	from mydir.SSBase A, crsp.dsf B, crsp.dsi C
	where (A.Permno=B.Permno)
	& (B.Date=C.Date)
	& (27 le intck('WeekDay', A.ActDate, B.DATE) le 127)
	group by A.PERMNO, A.ActDate;
quit;

proc sql;
	create table mydir.SSV7 as
	select distinct A.Permno, A.ActDate,
	std(B.Ret-C.EWRETD) as VMA_26_5,
	std(B.Ret) as VRW_26_5,
	mean(B.VOL/B.SHROUT) as TO_26_5,
	mean( (abs(B.Ret)/(abs(B.PRC)*(B.VOL/1000000))) ) as ALR_26_5,
	sum(B.Vol*((B.Ask-B.Bid)/(0.5*B.Ask+0.5*B.Bid)))/sum(B.Vol) as BAS_26_5
	from mydir.SSBase A, crsp.dsf B, crsp.dsi C
	where (A.Permno=B.Permno)
	& (B.Date=C.Date)
	& (-26 le intck('WeekDay', A.ActDate, B.DATE) le -5)
	group by A.PERMNO, A.ActDate;
quit;


proc sql;
	create table mydir.SSV8 as
	select distinct A.Permno, A.ActDate,
	std(B.Ret-C.EWRETD) as VMA_127_5,
	std(B.Ret) as VRW_127_5
	from mydir.SSBase A, crsp.dsf B, crsp.dsi C
	where (A.Permno=B.Permno)
	& (B.Date=C.Date)
	& (-127 le intck('WeekDay', A.ActDate, B.DATE) le -5)
	group by A.PERMNO, A.ActDate;
quit;

proc sql;
	create table mydir.SSV9 as
	select distinct A.Permno, A.ActDate,
	std(B.Ret-C.EWRETD) as VMA_5_127,
	std(B.Ret) as VRW_5_127
	from mydir.SSBase A, crsp.dsf B, crsp.dsi C
	where (A.Permno=B.Permno)
	& (B.Date=C.Date)
	& (5 le intck('WeekDay', A.ActDate, B.DATE) le 127)
	group by A.PERMNO, A.ActDate;
quit;





**************************************************************************************************************;
**************************************************************************************************************;
**STEP 7: BRING IN DATA ON IDIOSYNCRATIC VOLATILITY AND BETAS;
**DESC: THIS SECTION CREATES TWO DIFFERENT MEASURES OF IDIOSYNCRATIC VOLATILITY AND BETAS;
** 		CORRESPONDING TO TWO DIFFERENT TIME WINDOWS RELATIVE TO EARNINGS ANNOUNCEMENT DATES;
**************************************************************************************************************;
**************************************************************************************************************;


proc sort data=ff.factors_daily out=factors_daily; by date; run;
data factors_daily; 
	set factors_daily; 
	mktrfL1=lag1(mktrf);
	mktrfL2=lag2(mktrf);
	mktrfL3=lag3(mktrf);
run;

proc sql;
	create table FFreturns as
	select A.PERMNO, C.ActDate,  
	A.RET as RR, E.rf as RF,
	A.RET-E.rf as RRF, 
	E.mktrf as MKTRF,
	E.mktrfL1, E.mktrfL2, E.mktrfL3,
	E.smb as SMB,
	E.hml as HML,
	E.umd as UMD,
	intck('WEEKDAY', C.ACTDATE, A.DATE) as TD
	from crsp.dsf A, mydir.ssbase C, factors_daily E
	where (A.PERMNO=C.PERMNO) & (A.Date=E.Date) &
	(-257 le intck('WEEKDAY', C.ACTDATE, A.DATE) le 257);
quit;

proc sort data=FFreturns; by PERMNO ActDate; run;



ods exclude all; 
proc sort data=FFreturns(where=(-26 le TD lt -5)) out=DailyVolPre; by PERMNO ActDate; run;

proc datasets nolist; delete AEPBasePre IdioVOLAllPre; quit;

%macro estim(dsetin, st_yr, end_yr);

	proc datasets nolist; delete AEPParm AEPRes; quit;

	%do year = %eval(&st_yr) %to %eval(&end_yr);

		proc glm data=&dsetin(where=(&year=year(ActDate)));
			model RRF=MKTRF SMB HML UMD;
			by PERMNO ActDate;
			ods output parameterestimates=AEPParm(keep=Permno ActDate Parameter Estimate);
		run;
		proc transpose data=AEPParm out=AEPParm2(drop=_name_ Intercept 
				rename=(MKTRF=MktBetaPre SMB=SMBBetaPre HML=HMLBetaPre UMD=UMDBetaPre));
				by PERMNO ActDate;
				var Estimate;
			id Parameter;
		run;		
		proc append data=AEPParm2 base=AEPBasePre force; quit;


		proc glm data=&dsetin(where=(&year=year(ActDate)));
			model RRF=MKTRF SMB HML UMD; *MKTRF MKTRFL1 MKTRFL2 MKTRFL3;
			by PERMNO ActDate;
			output out=AEPRes r=r;
		run;	
		data AEPResAll;
			set AEPRes;
			SQR=r**2;
			SRRF=RRF**2;
			SIV=(SQR)/(SRRF);
		run;
		proc summary data=AEPResAll nway;
			class PERMNO ACTDATE;
			output out=IdioVOL(drop=_type_ _freq_) mean(SQR SIV SRRF)=IdioVOLPre SIVPre SRRFPre;
		run;
		proc append data=IdioVOL(keep= PERMNO ActDate IdioVOLPre SIVPre SRRFPre) base=IdioVOLAllPre force; quit;
				
	%end;
%mend estim;
%estim(DailyVolPre, 1996,2019);







proc sort data=FFreturns(where=(5 le TD lt 26)) out=DailyVolPost; by PERMNO ActDate; run;

proc datasets nolist; delete AEPBasePost IdioVOLAllPost; quit;

%macro estim(dsetin, st_yr, end_yr);

	proc datasets nolist; delete AEPParm AEPRes; quit;

	%do year = %eval(&st_yr) %to %eval(&end_yr);

		proc glm data=&dsetin(where=(&year=year(ActDate)));
			model RRF=MKTRF SMB HML UMD;
			by PERMNO ActDate;
			ods output parameterestimates=AEPParm(keep=Permno ActDate Parameter Estimate);
		run;
		proc transpose data=AEPParm out=AEPParm2(drop=_name_ Intercept 
				rename=(MKTRF=MktBetaPost SMB=SMBBetaPost HML=HMLBetaPost UMD=UMDBetaPost));
				by PERMNO ActDate;
				var Estimate;
			id Parameter;
		run;		
		proc append data=AEPParm2 base=AEPBasePost force; quit;
		
		
		proc glm data=&dsetin(where=(&year=year(ActDate)));
			model RRF=MKTRF SMB HML UMD;
			by PERMNO ActDate;
			output out=AEPRes r=r;
		run;	
		data AEPResAll;
			set AEPRes;
			SQR=r**2;
			SRRF=RRF**2;
			SIV=(SQR)/(SRRF);
		run;
		proc summary data=AEPResAll nway;
			class PERMNO ACTDATE;
			output out=IdioVOL(drop=_type_ _freq_) mean(SQR SIV SRRF)=IdioVOLPost SIVPost SRRFPost;
		run;
		proc append data=IdioVOL(keep= PERMNO ActDate IdioVOLPost SIVPost SRRFPost) base=IdioVOLAllPost force; quit;
				
	%end;
%mend estim;
%estim(DailyVolPost, 1996,2019);

ods exclude none; 


proc sort data=AEPBasePre; by Permno ActDate; run;
proc sort data=AEPBasePost; by Permno ActDate; run;
proc sort data=IdioVOLAllPre; by Permno ActDate; run;
proc sort data=IdioVOLAllPost; by Permno ActDate; run;

data mydir.BetaIVOL;
	merge AEPBasePre AEPBasePost IdioVOLAllPre IdioVOLAllPost;
	by Permno ActDate;
run;





ods exclude all; 
proc sort data=FFreturns(where=(-127 le TD lt -5)) out=DailyVolPre; by PERMNO ActDate; run;

proc datasets nolist; delete AEPBasePre2 IdioVOLAllPre2; quit;

%macro estim(dsetin, st_yr, end_yr);

	proc datasets nolist; delete AEPParm AEPRes; quit;

	%do year = %eval(&st_yr) %to %eval(&end_yr);

		proc glm data=&dsetin(where=(&year=year(ActDate)));
			model RRF=MKTRF SMB HML UMD;
			by PERMNO ActDate;
			ods output parameterestimates=AEPParm(keep=Permno ActDate Parameter Estimate);
		run;
		proc transpose data=AEPParm out=AEPParm2(drop=_name_ Intercept 
				rename=(MKTRF=MktBetaPre2 SMB=SMBBetaPre2 HML=HMLBetaPre2 UMD=UMDBetaPre2));
				by PERMNO ActDate;
				var Estimate;
			id Parameter;
		run;		
		proc append data=AEPParm2 base=AEPBasePre2 force; quit;
		
		
		proc glm data=&dsetin(where=(&year=year(ActDate)));
			model RRF=MKTRF SMB HML UMD; 
			by PERMNO ActDate;
			output out=AEPRes r=r;
		run;	
		data AEPResAll;
			set AEPRes;
			SQR=r**2;
			SIV=(SQR)/(RRF**2);
		run;
		proc summary data=AEPResAll nway;
			class PERMNO ACTDATE;
			output out=IdioVOL(drop=_type_ _freq_) mean(SQR SIV)=IdioVOLPre2 SIVPre2;
		run;
		proc append data=IdioVOL(keep= PERMNO ActDate IdioVOLPre2 SIVPre2) base=IdioVOLAllPre2 force; quit;
				
	%end;
%mend estim;
%estim(DailyVolPre, 1996,2019);






proc sort data=FFreturns(where=(5 le TD lt 127)) out=DailyVolPost; by PERMNO ActDate; run;

proc datasets nolist; delete AEPBasePost2 IdioVOLAllPost2; quit;

%macro estim(dsetin, st_yr, end_yr);

	proc datasets nolist; delete AEPParm AEPRes; quit;

	%do year = %eval(&st_yr) %to %eval(&end_yr);

		proc glm data=&dsetin(where=(&year=year(ActDate)));
			model RRF=MKTRF SMB HML UMD;
			by PERMNO ActDate;
			ods output parameterestimates=AEPParm(keep=Permno ActDate Parameter Estimate);
		run;
		proc transpose data=AEPParm out=AEPParm2(drop=_name_ Intercept 
				rename=(MKTRF=MktBetaPost2 SMB=SMBBetaPost2 HML=HMLBetaPost2 UMD=UMDBetaPost2));
				by PERMNO ActDate;
				var Estimate;
			id Parameter;
		run;		
		proc append data=AEPParm2 base=AEPBasePost2 force; quit;
		
		
		proc glm data=&dsetin(where=(&year=year(ActDate)));
			model RRF=MKTRF SMB HML UMD; 
			by PERMNO ActDate;
			output out=AEPRes r=r;
		run;	
		data AEPResAll;
			set AEPRes;
			SQR=r**2;
			SIV=(SQR)/(RRF**2);
		run;
		proc summary data=AEPResAll nway;
			class PERMNO ACTDATE;
			output out=IdioVOL(drop=_type_ _freq_) mean(SQR SIV)=IdioVOLPost2 SIVPost2;
		run;
		proc append data=IdioVOL(keep= PERMNO ActDate IdioVOLPost2 SIVPost2) base=IdioVOLAllPost2 force; quit;
				
	%end;
%mend estim;
%estim(DailyVolPost, 1996,2019);
ods exclude none; 


proc sort data=AEPBasePre2; by Permno ActDate; run;
proc sort data=AEPBasePost2; by Permno ActDate; run;
proc sort data=IdioVOLAllPre2; by Permno ActDate; run;
proc sort data=IdioVOLAllPost2; by Permno ActDate; run;



data mydir.BetaIVOL2;
	merge AEPBasePre2 AEPBasePost2 IdioVOLAllPre2 IdioVOLAllPost2;
	by Permno ActDate;
run;

**************************************************************************************************************;
**************************************************************************************************************;
**STEP 8: BRING IN DATA ON OPTION TRADING VOLUME;
**DESC: THIS SECTION CREATES DATA USED IN LIQUIDITY SUBSAMPLE TESTS;
**************************************************************************************************************;
**************************************************************************************************************;

proc datasets nolist; delete JSOPT JSIV; quit;

ods exclude all;

%macro myear(yr);
proc sql;
	create table C_&yr as
	select distinct A.Permno, A.CUSIP, C.DATE,
	sum(C.Volume) as OPVOL,
	sum(case when C.cp_flag eq 'C' then C.Volume else . end) as CVOL,
	sum(case when C.cp_flag eq 'P' then C.Volume else . end) as PVOL,
	
	sum(C.Volume * (.5*C.best_offer+.5*C.best_bid) ) as OPDVOL,
	sum(case when C.cp_flag eq 'C' then (C.Volume * (.5*C.best_offer+.5*C.best_bid))  else . end) as CDVOL,
	sum(case when C.cp_flag eq 'P' then (C.Volume * (.5*C.best_offer+.5*C.best_bid))  else . end) as PDVOL,
		
	sum(abs((C.Delta*A.PRC)/(.5*C.best_offer+.5*C.best_bid))*C.open_interest)/sum(C.open_interest) as LM,
	sum(C.impl_volatility*C.Volume)/sum(C.Volume) as IV
	from crsp.dsf A, optionm.opprcd&yr C, optionm.securd D
	where (C.SECID=D.SECID) & (A.Cusip=D.Cusip) & (C.date=A.Date)
	& (C.Volume gt 0) & (C.ss_flag eq '0') & (0 lt C.impl_volatility lt 9) & (0 lt C.Vega lt 99)
	group by A.Permno, C.DATE;
quit;

proc sort data=C_&yr nodupkey; by Permno Date; run;
proc append data=C_&yr base=JSOPT force; run;
proc datasets nolist; delete C_&yr; quit;


%mend;
%myear(1996);
%myear(1997);
%myear(1998);
%myear(1999);
%myear(2000);
%myear(2001);
%myear(2002);
%myear(2003);
%myear(2004);
%myear(2005);
%myear(2006);
%myear(2007);
%myear(2008);
%myear(2009);
%myear(2010);
%myear(2011);
%myear(2012);
%myear(2013);
%myear(2014);
%myear(2015);
%myear(2016);
%myear(2017);
%myear(2018);
%myear(2019);

ods exclude none;   

proc sort data=JSOPT out=mydir.JSOPT; by permno date; run;
proc sql;
	create table mydir.AllVol as
	select distinct A.Permno, A.CUSIP, A.Date, A.VOL as EDVOL,
	max(B.OPVOL,0) as OPVOL, max(B.CVOL, 0) as CVOL, max(B.PVOL,0) as PVOL,
	max(B.OPDVOL,0) as OPDVOL, max(B.CDVOL, 0) as CDVOL, max(B.PDVOL,0) as PDVOL,
	case when EDVOL gt 0 and OPVOL gt 0 then 100*((OPVOL*100)/EDVOL) else . end as OS
	from crsp.dsf A left outer join mydir.JSOPT B
	on (A.PERMNO=B.PERMNO) 
	& (A.Date=B.Date) 
	& (A.HEXCD in (1,2,3))
	& (A.VOL gt 0)
	& ('01JAN1996'd le A.Date le '31DEC2019'd)
	order by A.PERMNO, A.Date;
quit;
run; 

proc sql;
	create table mydir.SSOPVOL as
	select distinct B.Permno, B.ActDate,
	mean(A.OPVOL) as MOPVOL,
	mean(A.CVOL) as MCVOL,
	mean(A.PVOL) as MPVOL,
	mean(A.OS) as MOSVOL,
	mean(A.OPDVOL) as MOPDVOL,
	mean(A.CDVOL) as MCDVOL,
	mean(A.PDVOL) as MPDVOL,
	sum(case when A.OPVOL gt 0 then 1 else 0 end) as OTD,
	sum(case when A.CVOL gt 0 then 1 else 0 end) as OCTD,
	sum(case when A.PVOL gt 0 then 1 else 0 end) as OPTD
	from mydir.AllVol A, mydir.SSBase B
	where (A.Permno=B.Permno) &
	(-26 le intck('WEEKDAY', B.ActDate, A.DATE) le -5)
	group by B.Permno, B.ActDate;
quit;



**************************************************************************************************************;
**************************************************************************************************************;
**STEP 9: MERGE DATA TOGETHER;
**DESC: THIS SECTION CREATES A FINAL DATASET FOR EXPORT;
**************************************************************************************************************;
**************************************************************************************************************;


proc sort data=mydir.SSIV1 nodupkey; by CUSIP ActDate; run;
proc sort data=mydir.SSBase nodupkey; by CUSIP ActDate; run;

data mydir.SSIVv1;
	merge mydir.SSBase(in=a) mydir.SSIV1(in=b);
	by CUSIP ActDate; 
	if a and b;
run;
proc sort data=mydir.SSIVv1; by Permno ActDate; run;
proc sort data=mydir.BetaIVOL nodupkey; by Permno ActDate; run;
proc sort data=mydir.BetaIVOL2 nodupkey; by Permno ActDate; run;
proc sort data=mydir.SSVIXPre nodupkey; by Permno ActDate; run;
proc sort data=mydir.SSVIXPost nodupkey; by Permno ActDate; run;
proc sort data=mydir.SSOPVOL nodupkey; by Permno ActDate; run;

data mydir.smithsoJAR_20211102;
	merge mydir.SSIVv1(in=a) mydir.SSV1(in=b) mydir.SSV2 mydir.SSV3 mydir.SSV4 mydir.SSV5 mydir.SSV6 mydir.SSV7 mydir.SSV8 mydir.SSV9
	mydir.BetaIVOL mydir.BetaIVOL2 mydir.SSVIXPre mydir.SSVIXPost mydir.SSOPVOL;
	by Permno ActDate;
	if a and b;
	
	Period=(Year(ActDate)*10)+Qtr(ActDate);
	AYear=year(actdate);
	AMonth=month(actdate);
	AYearMonth=(AYear*100)+AMonth;
	
	SIGQP=( ((IV30_2/sqrt(252))**2)-((IV60_2/sqrt(252))**2) )/(1/30 - 1/60);


	if VMA_26_5 gt 0 and VMA_5_26 gt 0 then PPVMA=100*(VMA_5_26-VMA_26_5);	
	if VMA_5_26 gt 0 and VMA_27_257 gt 0 then DVMA=100*(VMA_5_26-VMA_27_127);
	if ALR_26_5 gt 0 and ALR_5_26 gt 0 then PPALR=log(ALR_5_26/ALR_26_5);
	if BAS_26_5 gt 0 and BAS_5_26 gt 0 then PPBAS=log(BAS_5_26/BAS_26_5);


	DeltaSIV=100*(SIVPost-SIVPre);
	if SIVPost gt 0 and SIVPre gt 0 then DSIV=log(SIVPost/SIVPre);

	DeltaIVOL=100*(IdioVOLPost-IdioVolPre);
	if IdioVOLPost gt 0 and IdioVolPre gt 0 then DIVOL=log(IdioVOLPost/IdioVolPre);

	DeltaMktBetaSq=(MktBetaPost)**2-(MktBetaPre)**2;
	DeltaSMBBetaSq=(SMBBetaPost)**2-(SMBBetaPre)**2;
	DeltaHMLBetaSq=(HMLBetaPost)**2-(HMLBetaPre)**2;
	DeltaUMDBetaSq=(UMDBetaPost)**2-(UMDBetaPre)**2;	
	
	DeltaIVOL2=100*(IdioVOLPost2-IdioVolPre2);
	if IdioVOLPost2 gt 0 and IdioVolPre2 gt 0 then DIVOL2=log(IdioVOLPost2/IdioVolPre2);
	if (MktBetaPost)**2 gt 0 and (MktBetaPre)**2 gt 0 then DeltaMktBetaSq2=log((MktBetaPost)**2/(MktBetaPre)**2);
	if (SMBBetaPost)**2 gt 0 and (SMBBetaPre)**2 gt 0 then DeltaSMBBetaSq2=log((SMBBetaPost)**2/(SMBBetaPre)**2);
	if (HMLBetaPost)**2 gt 0 and (HMLBetaPre)**2 gt 0 then DeltaHMLBetaSq2=log((HMLBetaPost)**2/(HMLBetaPre)**2);
	if (UMDBetaPost)**2 gt 0 and (UMDBetaPre)**2 gt 0 then DeltaUMDBetaSq2=log((UMDBetaPost)**2/(UMDBetaPre)**2);		
	
	DeltaVIX=(VIXPost-VIXPre);
	
	DeltaIV30=(30)*((mean(of IV302)/sqrt(252))**2 - (mean(of IV30_2)/sqrt(252))**2);
	RI30=(DeltaIV30 + SIGQP);
	DeltaIV60=(60-2)*((mean(of IV602)/sqrt(252))**2 - (mean(of IV60_2)/sqrt(252))**2);
	RI60=(DeltaIV60 + SIGQP);
	DeltaIV122=(122)*((mean(of IV1222)/sqrt(252))**2 - (mean(of IV122_2)/sqrt(252))**2);
	RI122=(DeltaIV122 + SIGQP);
	DeltaIV182=(182)*((mean(of IV1822)/sqrt(252))**2 - (mean(of IV182_2)/sqrt(252))**2);
	RI182=(DeltaIV182 + SIGQP);
	DeltaIV365=(365)*((mean(of IV3652)/sqrt(252))**2 - (mean(of IV365_2)/sqrt(252))**2);
	RI365=(DeltaIV365 + SIGQP);		
	
	ABRI30=abs(RI30);
	ABRI122=abs(RI122);
	ABRI182=abs(RI182);
	ABRI365=abs(RI365);
	
	DiffVol30=30*((SIGB_pre1/sqrt(252))**2);
	if DiffVol30 gt 0 then SARI30=(AbRI30/DiffVol30);
	if DiffVol30 gt 0 then SRI30=(RI30/DiffVol30);

	DiffVol182=182*((SIGB_pre1/sqrt(252))**2);
	if DiffVol182 gt 0 then SARI182=(AbRI182/DiffVol182);
	if DiffVol182 gt 0 then SRI182=(RI182/DiffVol182);
	
	FirstQtr=(fiscalq=1);
	MCAP=CompPrice*CSO;
	if MCAP gt 0 then SIZE=log(MCAP);
	BTM=BE/MCAP;
	DISP=STDEV/Compprice;
	if (1+BTM gt 0) then LBM=log(1+BTM);
	COV=log(1+max(numest,0));
	
run;




proc download data=mydir.smithsoJAR_20211022; run;
endrsubmit;




**************************************************************************************************************;
**************************************************************************************************************;
** ADDITIONAL NOTES;
**************************************************************************************************************;
**************************************************************************************************************;

/*
(1) THE CODE BELOW ILLUSTRATES HOW TO CONDUCT THE MAIN ANALYSES IN OUR PAPER. SOME ANALYSES ARE CONDUCTED USING SAS AND THE REMAINDER ARE DONE USING STATA.
*/

/*
(2) To implement the code below, researchers need to download a SAS macro routine to winsorize variables available here https://wrds-www.wharton.upenn.edu/pages/support/research-wrds/macros/wrds-macros-winsorize/
*/

/*
(3) To conduct the analysis involving ICCs, we merge in monthly ICC data from Lee-So-Wang (2021, RFS) "Evaluating Firm-Level Expected-Return Proxies: Implications for Estimating Treatment Effects. In the code below, the data file is called erp_public_210621, which is available from the authors. The variable GLS_an is the analyst-based implementation of the ICC measure from Gebhardt, Lee and Swaminathan (2001, JAR)***;
*/

/*
(4) To implement Table 7, we apply the code above to firms' 10K filing dates and uses textual measures provided on WRDS in the dataset wrdssec.wrds_nlpsa.
*/

/*
(5) To implement Table 8, we obtained investor sentiment data from the Jeffrey Wurgler's website: http://people.stern.nyu.edu/jwurgler/. 
*/

**************************************************************************************************************;
**************************************************************************************************************;
** THIS SECTION CONTAINS CODE TO PROCESS DATA AFTER DOWNLOADING THE FILE mydir.smithsoJAR_20211102 FROM WRDS;
**************************************************************************************************************;
**************************************************************************************************************;

***DOWNLOAD FILE "smithsoJAR_20211022.sas7bdat" FROM WRDS AND SAVE IN LOCAL DIRECTORY***;


data SS;
	set a.smithsoJAR_20211022;	
	if MOPVOL lt 25 or CompPrice lt 1 or RI30 eq . or RI182 eq . or DeltaIV30 eq . or SURP eq . or SIZE eq . or LBM eq . then delete;
run;


***CREATE STANDARDIZED VARIABLES***;
proc sort data=SS out=SS1; by Period; run;
proc standard data=SS1 out=SS2(keep=Permno ActDate Period RI30 RI182 DeltaIV30 DeltaIV182 VMA_5_26 VMA_27_127) mean=0 std=1;
	by Period;
	var RI30 RI182 DeltaIV30 DeltaIV182 VMA_5_26 VMA_27_127;
run;
proc sort data=SS2; by Permno ActDate; run;
proc sort data=SS1; by Permno ActDate; run;

data SS3;
	merge SS1(in=a) SS2(in=b rename=(RI30=RI30_St RI182=RI182_St DeltaIV30=DeltaIV30_St DeltaIV182=DeltaIV182_St VMA_5_26=VMA_5_26_St VMA_27_127=VMA_27_127_St));
	by Permno ActDate;
	if a;
	DDRI30182=RI30_ST-RI182_ST;
	DDIV30182=DeltaIV30_St-DeltaIV182_St;

	if VMA_5_26 gt 0 and VMA_27_127 gt 0 then DVMA_ST=(VMA_5_26_St-VMA_27_127_St);
run;


proc sql;
	create table SS4 as
	select distinct A.*,
	100*(((1+B.GLS_an)**(12))-1) as GLS1
	from SS3 A left outer join a.erp_public_210621 B
	on (A.GVKEY=B.GVKEY)
	& (1 eq intck('Month', A.ActDate, mdy(b.month, 1, b.year) ))
	order by A.Permno, A.ActDate;
quit;
proc sql;
	create table SS5 as
	select distinct A.*,
	100*(((1+B.GLS_an)**(12))-1) as GLS_1
	from SS4 A left outer join a.erp_public_210621 B
	on (A.GVKEY=B.GVKEY)
	& (1 eq intck('Month', mdy(b.month, 1, b.year), A.ActDate))
	order by A.Permno, A.ActDate;;
quit;

data SS6;
	set SS5;
	array testmiss(*) GLS1 GLS_1;
	do i=1 to dim(testmiss);
			if testmiss(i) lt 0 then testmiss(i)=.;
		end;
	drop i;
	DeltaGLS=(GLS1-GLS_1);
run;



*******CREATE RANKS OF KEY VARIABLES******;
proc sort data=SS6 out=SSOPT; by Period; run;
%macro var(vname);
proc univariate data =SSOPT(where=(&vname ne .)) noprint;
    by Period; var &vname;
    output out =S3 PCTLPTS =10 20 30 33 40 50 60 70 66 80 90 PCTLPRE = Cut PCTLNAME = _P10 _P20 _P30 _P33 _P40 _P50 _P60 _P66 _P70 _P80 _P90;
run; 
data S3B(drop=Period RepYear RepQtr);  set S3; 	
	RepYear=floor(Period/10);
	RepQtr=Period-(10*RepYear);
	if RepQtr le 3 then Period2=(RepYear*10)+(RepQtr+1);
	else Period2=((RepYear+1)*10)+(1);
run;
proc sort data=S3B out=S3C(rename=(Period2=Period)); by Period2; run;
data SSOPT(drop=Cut_P10 Cut_P20 Cut_P30 Cut_P33 Cut_P40 Cut_P50 Cut_P60 Cut_P66 Cut_P70 Cut_P80 Cut_P90);
	merge SSOPT(in=a) S3C(in=b); by Period; if a;
	if (&vname lt Cut_P20) and Cut_P20 ne . then Q_&vname=0.00;
	else if (Cut_P20 le &vname lt Cut_P40) then Q_&vname=0.25;
	else if (Cut_P40 le &vname lt Cut_P60) then Q_&vname=0.50;
	else if (Cut_P60 le &vname lt Cut_P80) then Q_&vname=0.75;
	else if (&vname ge Cut_P80) and Cut_P80 ne .  then Q_&vname=1.00;
	else &vname=.;
	if (&vname lt Cut_P33) and Cut_P33 ne . then T_&vname=0.00;
	else if (Cut_P33 le &vname lt Cut_P66) then T_&vname=0.50;
	else if (&vname ge Cut_P66) and Cut_P66 ne .  then T_&vname=1.00;
	else T_&vname=.;
	if (&vname lt Cut_P10) and Cut_P10 ne . then D_&vname=0.00;
	else if (Cut_P10 le &vname lt Cut_P20) then D_&vname=(1/9);
	else if (Cut_P20 le &vname lt Cut_P30) then D_&vname=(2/9);
	else if (Cut_P30 le &vname lt Cut_P40) then D_&vname=(3/9);
	else if (Cut_P40 le &vname lt Cut_P50) then D_&vname=(4/9);
	else if (Cut_P50 le &vname lt Cut_P60) then D_&vname=(5/9);
	else if (Cut_P60 le &vname lt Cut_P70) then D_&vname=(6/9);
	else if (Cut_P70 le &vname lt Cut_P80) then D_&vname=(7/9);
	else if (Cut_P80 le &vname lt Cut_P90) then D_&vname=(8/9);
	else if (&vname ge Cut_P90) and Cut_P90 ne .  then D_&vname=1.00;
	else D_&vname=.;
	if &vname eq . then do;
		&vname=.;
		D_&vname=.;
		T_&vname=.;
	end;
run; 
%mend var;
%var(RI30); %var(RI122); %var(RI182); %var(RI365); 
%var(DDRI30182); %var(MOPDVOL); 


proc sort data=SSOPT(where=(Q_RI30 ne . )) out=SSOPT1; by period; run;
%winsor(dsetin=SSOPT1, dsetout=SSOPT1a, byvar=PERIOD, vars=
RI30 RI60 RI122 RI182 RI365 
ABRI30 ABRI122 ABRI182 ABRI365 SIGQP 
DVMA DVMA_ST PPVMA SURP 
DeltaIVOL DeltaMktBetaSq DeltaSMBBetaSq DeltaHMLBetaSq DeltaUMDBetaSq
DeltaGLS PPALR PPBAS
,  type=winsor, pctl=1 99);

data SSD(where=(Year(ActDate) le 2019));
	set SSOPT1a;
	CON=1;
run;


******EXPORT THE FINAL DATASET FOR LATER USE IN STATA******;
proc export data=SSD outfile= "~/projects/LEV/ssd.dta" replace; run;







**************************************************************************************************************;
**************************************************************************************************************;
** CODE FOR MAIN ANALYSIS;
**************************************************************************************************************;
**************************************************************************************************************;




******CREATE DESCRIPTIVE STATISTICS AND CORRELATIONS******;
proc summary data=SSD nway;
	class AYEAR;
	output out=SSDYear(drop=_type_ rename=(_freq_=OBS)) 
	mean(RI30 AbRI30 SARI30 RI182 AbRI182 SARI182)=;
run;

proc export data=_last_ outfile='~/projects/LEV/RawResults/TableDesc.xls' dbms=xls replace; run;


proc corr data=SSD(where=(Year(ActDate) le 2019)) outs=TableCorr(where=(_type_ eq 'CORR'));
	var RI30 SRI30 RI182 SRI182 DeltaIV30 SIZE LBM VMA_5_257 PPVMALT DISP;
run;

proc export data=TableCorr outfile='~/projects/LEV/RawResults/TableCorr.xlsx' dbms=xlsx replace; run;








******TEST LINK BETWEEN RI AND CHANGES IN RETURN VOLATILITY AND SQUARED FACTOR LOADINGS******;

proc datasets nolist; delete SSPort1; quit;
%macro bmac(bvar,  cond, dletter);
proc means data=SSD(where=(&cond)) nway noprint;
	class Period &bvar;
	output out=TB3aSum(drop=_type_  rename=(_freq_=OBS)) mean(PPVMA)=;
run;
proc transpose data=TB3aSum out=BSDTran(drop=_name_) prefix=N;
	var OBS; by Period; id &bvar;
data SSSumAll; merge TB3aSum(in=a) BSDTran(in=b); by Period; if N1 lt 5 or N0 lt 5 then delete; run; 	
%macro dvar(dname, dletter);
proc transpose data=SSSumAll out=BSDTran(drop=_name_);
	var &dname; by Period; id &bvar;
data HighLow2a_&dletter (where=(HighLow ne .)); set BSDTran(drop=period); HighLow=_1-_0; run;
proc means data=HighLow2a_&dletter nway noprint;
	output out=HighLow_&dletter(drop=_type_ _freq_) mean=; 
proc means data=HighLow2a_&dletter nway noprint;
	output out=BSDTran_&dletter(drop=_type_ _freq_) t=; 
run;
data Both_&dletter; retain vname; set HighLow_&dletter BSDTran_&dletter; vname="&dname"; run;
%mend dvar; %dvar(PPVMA, a);  
data SSum; retain COND VNAME; set Both_a;
  COND="&cond"; run;
proc append data=SSum base=SSPort1 force; quit;
%mend bmac;
%bmac(Q_RI30, (ri30 ne .), a);
proc export data=SSPort1 outfile='~/projects/LEV/RawResults/PortDVOL1.xlsx' dbms=xlsx replace; run;




******TEST LINK BETWEEN RI AND HORIZON OF VOLATILITY******;
proc datasets nolist; delete SSPort2; quit;
%macro bmac(bvar, wvar, cond, dletter);
proc means data=SSD(where=(&cond)) nway noprint;
	class Period &bvar;
	weight &wvar;
	output out=dvmasum(drop=_type_  rename=(_freq_=OBS)) mean(RI30 RI182 DVMA DVMA_ST)=;
run;
proc transpose data=dvmasum out=BSDTran(drop=_name_) prefix=N;
	var OBS; by Period; id &bvar;
data SSSumAll; merge dvmasum(in=a) BSDTran(in=b); by Period; if N1 lt 5 or N0 lt 5 then delete; run; 	
%macro dvar(dname, dletter);
proc transpose data=SSSumAll out=BSDTran(drop=_name_);
	var &dname; by Period; id &bvar;
data HighLow2a_&dletter (where=(HighLow ne .)); set BSDTran; HighLow=_1-_0; run;
proc means data=HighLow2a_&dletter nway noprint;
	output out=HighLow_&dletter(drop=_type_ _freq_Period) mean=; 
proc means data=HighLow2a_&dletter nway noprint;
	output out=BSDTran_&dletter(drop=_type_ _freq_Period) t=; 
run;
data Both_&dletter; retain vname HighLow; set HighLow_&dletter BSDTran_&dletter; vname="&dname"; run;
%mend dvar; %dvar(RI30,a); %dvar(RI182,b); %dvar(DVMA,c);  %dvar(DVMA_ST,d);
data SSum; retain BVAR WVAR VNAME; set  Both_a Both_b Both_c Both_d; 
  BVAR="&bvar"; WVAR="&wvar"; run;
proc append data=SSum base=SSPort2 force; quit;
%mend bmac;
%bmac(Q_DDRI30182, CON, (ri30 ne .), a);
proc export data=SSPort2 outfile='~/projects/LEV/RawResults/PortHorizon.xlsx' dbms=xlsx replace; run;






*******************************EXPORT DATA TO STATA************************************;


proc export data=SSD outfile= "~/projects/LEV/ssd.dta" replace; run;


* * * * * * * * * * * * * * * * * * * ;
* * * * * STATA CODE BEGINS * * * * * ;
* * * * * * * * * * * * * * * * * * * ;

clear  all
use "~/projects/LEV/ssd.dta", clear
rename _all, lower

set more off, permanently
set matsize 11000
tabulate period, gen(yq)
tabulate fiscalq, gen(fq)



***************REGRESSIONS OF CHANGES IN VOLATILITY***************;

local varnames = "ppvma" 
foreach var of local varnames {

est clear

eststo: quietly reghdfe `var'  ri30 ,  absorb( permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe `var'  ri30  size lbm,  absorb( permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe `var'  ri30  size lbm surp,  absorb( permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe `var'  deltaiv30  size lbm surp,  absorb( permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe `var'  sigqp  deltaiv30  size lbm surp,  absorb(permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe `var'  ri30  deltaiv30  size lbm surp,  absorb( permno ayear ) cluster(ayear permno)
esttab, t(%8.2f) b(%8.3f) replace star(* 0.10 ** 0.05 *** 0.01) ar2 nogaps  scalar(F)
}
esttab using ~/projects/LEV/RawResults/ssracelevel.csv, t(%8.2f) b(%8.3f) replace star(* 0.10 ** 0.05 *** 0.01) ar2 nogaps  scalar(F)




***************REGRESSIONS OF CHANGES IN VOLATILITY BY SUBSAMPLES BASED ON OPTION DOLLAR VOLUME AND INCLUSION IN S&P500***************;

est clear
use "~/projects/LEV/ssd.dta", clear
rename _all, lower
keep if t_mopdvol == 0
eststo: quietly reghdfe ppvma  ri30    size lbm surp ,  absorb( permno ayear) cluster(ayear permno)
use "~/projects/LEV/ssd.dta", clear
rename _all, lower
keep if t_mopdvol == 0.5
eststo: quietly reghdfe ppvma  ri30    size lbm surp ,  absorb( permno ayear) cluster(ayear permno)
use "~/projects/LEV/ssd.dta", clear
rename _all, lower
keep if t_mopdvol == 1
eststo: quietly reghdfe ppvma  ri30    size lbm surp ,  absorb( permno ayear) cluster(ayear permno)
use "~/projects/LEV/ssd.dta", clear
rename _all, lower
keep if snp == 0
eststo: quietly reghdfe ppvma  ri30    size lbm surp ,  absorb( permno ayear) cluster(ayear permno)
esttab using ~/projects/LEV/RawResults/sspartition1.csv, t(%8.2f) b(%8.3f) replace star(* 0.10 ** 0.05 *** 0.01) ar2 nogaps 
use "~/projects/LEV/ssd.dta", clear
rename _all, lower
keep if snp == 1
eststo: quietly reghdfe ppvma  ri30    size lbm surp ,  absorb( permno ayear) cluster(ayear permno)
esttab, t(%8.2f) b(%8.3f) replace star(* 0.10 ** 0.05 *** 0.01) ar2 nogaps
esttab using ~/projects/LEV/RawResults/sssubsample.csv, t(%8.2f) b(%8.3f) replace star(* 0.10 ** 0.05 *** 0.01) ar2 nogaps  scalar(F)




***************REGRESSIONS OF CHANGES IN SQUARED FACTOR LOADINGS***************;
clear  all
use "~/projects/LEV/ssd.dta", clear
rename _all, lower

est clear
eststo: quietly reghdfe deltaivol ri30    size lbm surp,  absorb( permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe deltamktbetasq ri30    size lbm surp,  absorb( permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe deltasmbbetasq  ri30    size lbm surp,  absorb( permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe deltahmlbetasq  ri30    size lbm surp,  absorb( permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe deltaumdbetasq  ri30    size lbm surp,  absorb( permno ayear ) cluster(ayear permno)
esttab, t(%8.2f) b(%8.3f) replace star(* 0.10 ** 0.05 *** 0.01) ar2 nogaps  scalar(F)
esttab using ~/projects/LEV/RawResults/ssfactorloadings.csv, t(%8.2f) b(%8.3f) replace star(* 0.10 ** 0.05 *** 0.01) ar2 nogaps  scalar(F)



***************REGRESSIONS TO ILLUSTRATE METHODOLOGICAL IMPLICATIONS***************;
clear  all
use "~/projects/LEV/ssd.dta", clear
rename _all, lower

gen ayearbynysd = (ayear*10+nysd)

est clear
eststo: quietly reghdfe deltaiv30 cov   , absorb( permno ayearbynysd) cluster(ayearbynysd permno)
eststo: quietly reghdfe sigq_pre cov   , absorb( permno ayearbynysd) cluster(ayearbynysd permno)
eststo: quietly reghdfe ri30 cov   , absorb( permno ayearbynysd) cluster(ayearbynysd permno)
eststo: quietly reghdfe deltaiv30   cov surp  , absorb( permno ayearbynysd) cluster(ayearbynysd permno)
eststo: quietly reghdfe sigq_pre   cov surp , absorb( permno ayearbynysd) cluster(ayearbynysd permno)
eststo: quietly reghdfe ri30  cov  surp , absorb( permno ayearbynysd) cluster(ayearbynysd permno)
esttab, t(%8.2f) b(%8.3f) replace star(* 0.10 ** 0.05 *** 0.01) ar2 nogaps 
esttab using ~/projects/LEV/RawResults/ssmethod.csv, t(%8.2f) b(%8.3f) replace star(* 0.10 ** 0.05 *** 0.01) ar2 nogaps  scalar(F)



***************REGRESSIONS OF HORIZON OF VOLATILITY***************;
clear  all
use "~/projects/LEV/ssd.dta", clear
rename _all, lower

est clear
eststo: quietly reghdfe dvma  ddri30182  size lbm surp, absorb(permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe dvma  ddiv30182 size lbm surp , absorb(permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe dvma  ddri30182  ddiv30182 size lbm surp , absorb(permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe dvma_st  ddri30182  size lbm surp, absorb(permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe dvma_st  ddiv30182 size lbm surp , absorb(permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe dvma_st  ddri30182  ddiv30182 size lbm surp , absorb(permno ayear ) cluster(ayear permno)
esttab, t(%8.2f) b(%8.3f) replace star(* 0.10 ** 0.05 *** 0.01) ar2 nogaps
esttab using ~/projects/LEV/RawResults/sshorizon.csv, t(%8.2f) b(%8.3f) replace star(* 0.10 ** 0.05 *** 0.01) ar2 nogaps  scalar(F)




***************REGRESSIONS OF ICCS AND LIQUIDITY***************;

use "~/projects/LEV/ssd.dta", clear
rename _all, lower


est clear
eststo: quietly reghdfe deltagls  ri30    , absorb(permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe deltagls  ri30   deltaiv30 size lbm surp , absorb( permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe ppalr  ri30    , absorb(permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe ppalr  ri30  deltaiv30 size lbm surp ppvma, absorb( permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe ppbas  ri30    , absorb(permno ayear ) cluster(ayear permno)
eststo: quietly reghdfe ppbas  ri30  deltaiv30 size lbm surp ppvma, absorb( permno ayear ) cluster(ayear permno)
esttab, t(%8.2f) b(%8.3f) replace star(* 0.10 ** 0.05 *** 0.01) ar2 nogaps
esttab using ~/projects/LEV/RawResults/regiccliq.csv, t(%8.2f) b(%8.3f) replace star(* 0.10 ** 0.05 *** 0.01) ar2 nogaps 






* * * * * * * * * * * * * * * * * * * ;
* * * * * STATA CODE ENDS * * * * * ;
* * * * * * * * * * * * * * * * * * * ;





*/









