/********************************************************************* */
/*  Program        : 1.VIXcorrelation.sas                                                 */
/*  Description    : Compute the correlation between the VIX and the     */
/*                   uncertainty shocks (Table 1 and Section 1 of the              */
/*                   paper).                                                                            */
/*                   The daily change of the VXO index (divided by               */
/*                   100) is averaged over the end of June (26-30) of  year    */
/*                   (vxo_mean). The annual VIX changes are matched         */
/*                   with the annualized pricing factors and the                      */
/*                   uncertainty shocks with the same timing                         */
/*                   convention as the uncertainty shocks                               */
/*                                                                                                        */
/*  Input : Table1\ vxo.xlsx (daily VXO index, 1986-2018)                   */
/*          data\ annualfactors (from Table1\1.MakeAnnualfactors.sas:    */
/*          annualized factors and uncertainty shocks)                              */
/*  Output: Table1\ allfactor_dvix (matched annual dataset)                  */
/********************************************************************* */

%let projpath = D:\SAS\TFP\Dispersion_replication;
libname mydisp "&projpath.\data";
libname myrob "&projpath.\Table1";

/*1. Import the daily VXO index*/
PROC IMPORT datafile="&projpath.\Table1\vxo.xlsx"
 	OUT=myrob.vxo
 	DBMS=xlsx
 	REPLACE;
 	GETNAMES=Yes;
RUN;
proc sort data=myrob.vxo;
     by A;
run;
data myrob.vxo2;
     set myrob.vxo;
	 vxo2 = input(vxo, 8.);
     year=year(A);
     month=month(A);
     day=day(A);
	 dvix=dif(vxo2)/100;
run;
/*2. Average the daily VIX changes over June 26-30 of each year*/
/*Note that the results are not sensitive to the specific date of June (e.g., the last trading day),
average of June, or December*/
proc sql;
     create table myrob.vxo2 as
     select *
     from myrob.vxo2 where month=6 and 26<=day<=30;
quit;
proc univariate data=myrob.vxo2 noprint;
     var dvix;
     by year;
     output out=myrob.vxo_annual mean=vxo_mean;
run;
/*3. Match with the annualized factors and the uncertainty shocks*/
proc sql;
     create table myrob.allfactor_dvix as
	 select a.*, b.vxo_mean, b.year as vix_year
	 from mydisp.annualfactors as a, myrob.vxo_annual as b
     where a.year=b.year+2
	 and a.year>=1989 and a.year<=2018;
quit;
proc corr data=myrob.allfactor_dvix;
var vxo_mean mktrfy HMLy CMAy RMWy SMBy q_mey q_IAy q_ROEy UMDy EIA d_tfpstd_star d_pcahat d_pcares;
run;
