/*
0.MakeDispersion.sas

Constructs the annual uncertainty measures of the paper (Section 2.2-2.3):
  - Total uncertainty (UNC): the cross-sectional standard deviation of TFP
    growth (dtfp_std_star), computed from the full firm-year panel (TFP.csv).
  - Macro uncertainty (UNC^ma): the cross-sectional standard deviation of
    systematic TFP growth (dpca_hat).
  - Micro uncertainty (UNC^mi): the cross-sectional standard deviation of
    idiosyncratic TFP growth (dpca_res).
  Uncertainty shocks are the first differences: d_tfpstd_star, d_pcahat, d_pcares, respectively.

Input : output\TFP.csv               (from 0.TFPestimation.do)
        output\TFP_decomposition.csv (from 0.DecomposeTFPgrowth.do)
Output: output\Dispersions.csv
        (cyear, dtfp_std, dpca_hat, dpca_res, d_tfpstd, d_pcahat, d_pcares,
         dtfp_std_star, d_tfpstd_star)
Note that dtfp_std and  d_tfpstd are the cross-sectional standard deviation of TFP
growth by applying the mininum number of TFP growth observations (11) to use PCA.
*/

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

/*1.Total uncertainty from the full panel (no PCA sample filter): _star*/
PROC IMPORT datafile="&projpath.\output\TFP.csv"
 	OUT=mydisp.tfp
 	DBMS=csv
 	REPLACE;
 	GETNAMES=Yes;
RUN;
proc sort data=mydisp.tfp;
    by cyear;
run;
proc univariate data=mydisp.tfp noprint;
     var d_tfppool;
     by cyear;
     output out=mydisp.tfp_std std=dtfp_std;
run;
proc sql;
     create table mydisp.dispersions as
     select a.*, (a.dtfp_std - b.dtfp_std) as d_tfpstd
     from mydisp.tfp_std as a, mydisp.tfp_std as b
     where a.cyear=b.cyear+1;
quit;

/*2.Systematic TFP (pca_hat), and residual TFP (pca_res)*/
PROC IMPORT datafile="&projpath.\output\TFP_decomposition.csv"
 	OUT=mydisp.PCA
 	DBMS=csv
 	REPLACE;
 	GETNAMES=Yes;
RUN;
proc sort data=mydisp.PCA;
     by gvkey cyear;
run;
/*Gvkey format change: Character to Number*/
data mydisp.PCA;
     set mydisp.PCA;
     gvkey1=put(gvkey,z6.);
     drop gvkey;
     rename gvkey1=gvkey;
run;
data mydisp.PCA;
     retain gvkey;
     set mydisp.PCA;
run;
proc sort data=mydisp.PCA;
    by cyear;
run;
proc univariate data=mydisp.PCA noprint;
     var tfp_pool pca_hat pca_res;
     by cyear;
     output out=mydisp.pca_std std=dtfp_std dpca_hat  dpca_res;
run;
proc sql;
     create table mydisp.PCA_dispersions as
     select a.*, (a.dtfp_std - b.dtfp_std) as d_tfpstd, (a.dpca_hat - b.dpca_hat) as d_pcahat,
	                  (a.dpca_res - b.dpca_res) as d_pcares
     from mydisp.pca_std as a, mydisp.pca_std as b
     where a.cyear=b.cyear+1;
quit;

/*3.Combine: attach the unfiltered total uncertainty (_star) to the macro and
   micro uncertainty series. The paper uses the _star series as the total
   uncertainty (UNC).*/
proc sql;
     create table mydisp.dispersion_all as
     select a.*, b.dtfp_std as dtfp_std_star, b.d_tfpstd as d_tfpstd_star
     from mydisp.PCA_dispersions as a, mydisp.dispersions as b
     where a.cyear=b.cyear;
quit;
proc export data=mydisp.dispersion_all outfile="&projpath.\output\Dispersions.csv" dbms=csv replace;
run;
