filename myfile 'D:\Dropbox\Step4Log.log';
proc printto log=myfile;run;

libname database "D:\Dropbox\EPA Data\Code and Data\Data\Database";
libname data "D:\Dropbox\EPA Data\Code and Data\Data";

*Table 1;
*******************************************************************************;
*******************************************************************************;
*******************************************************************************;
data a;set data.sample;obs_match=1;run;
data edgar;set data.EPAEdgar_clean;
format edgar_date mmddyy10.;
edgar_date=intnx('month',date,0,'end');
run;
proc sort data=edgar nodupkey;by RequestID;run;
proc sql;
create table edgar as 
select a.*, b.obs_match,b.ind_code
from edgar as a left join a as b
on a.gvkey=b.ngvkey and a.edgar_date=b.date_monthly;
quit;
data edgar;set edgar;if obs_match=1;run;
data edgar;set edgar;
format FormGroup $15.;
Form=Form_Type;
if Form="10-K"|Form="10-K/A" then FormGroup="10-K";
if Form="8-K"|Form="8-K/A" then FormGroup="8-K";
if Form="10-Q"|Form="10-Q/A" then FormGroup="10-Q";
if Form="4"|Form="4/A"|Form="3"|Form="3/A"|Form="5"|Form="5/A" then FormGroup="Form 3/4/5";*Insider trading;
if Form="SC 13D"|Form="SC 13D/"|Form="SC 13D/A"|Form="SC 13G"|Form="SC 13G/"|Form="SC 13G/A" then FormGroup="SC 13";*Inst holding;
if Form="ARS" then FormGroup="ARS";*annual report to security holders;
if Form="424B3"|Form="424A"|Form="424B"|Form="424B1"|Form="424B2"|Form="424B4"|Form="424B5"|Form="424B7" then FormGroup="424";*Prospectus issued following offering;
if Form="11-K"|Form="11-K/A" then FormGroup="11-K";*Annual report of employee stock purchase;
if missing(FormGroup)=1 then FormGroup="OTHER";
run;
*******************************************************************************;
*Panel A: Distribution By year;
proc freq data=edgar;table RequestYear;run;
proc freq data=edgar;where FormGroup="10-K";table RequestYear;run;
proc freq data=edgar;where FormGroup="8-K";table RequestYear;run;
proc freq data=edgar;where FormGroup="10-Q";table RequestYear;run;
*******************************************************************************;
*Panel B: Distribution By filing type;
proc freq data=edgar order=freq;table FormGroup;run;
*******************************************************************************;
*Panel C: Distribution by FF49 industry;
proc freq data=edgar;table ind_code;run;
*******************************************************************************;
*Panel D: distribution by 8-k topics;
data a;set edgar;if form8k=1;run;*11681;
*Clean 8-K item information;
proc import datafile='D:\Dropbox\EPA Data\Code and Data\Data\Database\DirectEdgar_8K.csv' out=form dbms=csv replace;run;
*Items;
data form;set form;if DOCTYPE='8K'|DOCTYPE='8KA';run;
proc contents data=form;run;
data form;set form;
if missing(ITEM_1_01)=0 then item101=1;else item101=0;
if missing(ITEM_1_02)=0 then item102=1;else item102=0;
if missing(ITEM_1_03)=0 then item103=1;else item103=0;
if missing(ITEM_1_04)=0 then item104=1;else item104=0;
if missing(ITEM_2_01)=0 then item201=1;else item201=0;
if missing(ITEM_2_02)=0 then item202=1;else item202=0;
if missing(ITEM_2_03)=0 then item203=1;else item203=0;
if missing(ITEM_2_04)=0 then item204=1;else item204=0;
if missing(ITEM_2_05)=0 then item205=1;else item205=0;
if missing(ITEM_2_06)=0 then item206=1;else item206=0;
if missing(ITEM_3_01)=0 then item301=1;else item301=0;
if missing(ITEM_3_02)=0 then item302=1;else item302=0;
if missing(ITEM_3_03)=0 then item303=1;else item303=0;
if missing(ITEM_4_01)=0 then item401=1;else item401=0;
if missing(ITEM_4_02)=0 then item402=1;else item402=0;
if missing(ITEM_5_01)=0 then item501=1;else item501=0;
if missing(ITEM_5_02)=0 then item502=1;else item502=0;
if missing(ITEM_5_03)=0 then item503=1;else item503=0;
if missing(ITEM_5_04)=0 then item504=1;else item504=0;
if missing(ITEM_5_05)=0 then item505=1;else item505=0;
if missing(ITEM_5_06)=0 then item506=1;else item506=0;
if missing(ITEM_5_07)=0 then item507=1;else item507=0;
if missing(ITEM_5_08)=0 then item508=1;else item508=0;
if missing(ITEM_6_01)=0 then item601=1;else item601=0;
if missing(ITEM_6_02)=0 then item602=1;else item602=0;
if missing(ITEM_6_03)=0 then item603=1;else item603=0;
if missing(ITEM_6_04)=0 then item604=1;else item604=0;
if missing(ITEM_6_05)=0 then item605=1;else item605=0;
if missing(ITEM_6_06)=0 then item606=1;else item606=0;
if missing(ITEM_7_01)=0 then item701=1;else item701=0;
if missing(ITEM_8_01)=0 then item801=1;else item801=0;
if missing(ITEM_9_01)=0 then item901=1;else item901=0;
run;
data form;set form;
drop FILENAME WORDCOUNT HITS DOCTYPE FYEND SICCODE--ITEM_9_01;
run;
proc means data=form n mean;var item101--item901;run;
proc sql;
create table a as 
select a.*, b.*
from a as a left join form as b
on (a.cik=b.cik) and (a.Date_Filed=b.RDate);
quit;
proc sort data=a nodupkey;by RequestID;run;
proc means data=a n mean;var item101--item901;run;
*Classify by topic;
data a;set a;
if item101=1|item102=1 then topic1=1;else topic1=0; *Material contract;
if item201=1 then topic2=1;else topic2=0 ;*M&A;
if item202=1 then topic3=1;else topic3=0;*Earnings News;
if item203=1 then topic4=1;else topic4=0 ;*financial obligation;
if item205=1|item206=1 then topic5=1;else topic5=0;*Disposal and impairment;
if item301=1|item302=1|item303=1 then topic6=1;else topic6=0 ;*Securities and Trading Markets;
if item401=1|item402=1 then topic7=1;else topic7=0;*Acct change and restatments;
if item502=1|item503=1|item504=1|item505=1|item507=1 then topic8=1;else topic8=0;*Corporate Governance and Management;
if item701=1 then topic9=1;else topic9=0;*Regulation FD Disclosure;
if item103=1|item104=1|item801=1 then topic10=1;else topic10=0;*Major events;
if topic1=0 and topic2=0 and topic3=0 and topic4=0 and topic5=0 and topic6=0 and topic7=0 and topic8=0 and topic9=0 and topic10=0 then topic11=1;else topic11=0;*other;
run;
proc means data=a n sum mean;var topic1 topic2 topic3 topic4 topic5 topic6 topic7 topic8 topic9 topic10 topic11;run;



*Full sample descriptives;
*******************************************************************************;
*******************************************************************************;
*Table 2A: Summary statistics full sample;
data a;set data.sample;run;
proc means data=a  n mean std min p1 p10 median p90 p99 max nolabels;
var edgar total log_total 
log_resolve_3yr log_mve btm cash tangibility lev zscore_qtl roa sg filings log_filings
dum_discover dum_investigate dum_resolve dum_review facase highcc highp
dum_prepare dum_inspect dum_postinspect fr_inspect
dum_preparerule dum_publishrule dum_postrule finrule;
run;
proc corr data=a spearman;
var edgar total log_total log_resolve_3yr log_mve btm cash tangibility lev zscore_qtl roa sg filings log_filings;
run;




*Table 9A Summary statistics;
************************************************************************************************************************;
************************************************************************************************************************;
************************************************************************************************************************;
proc means data=data.t2case n mean std maxdec=3;var total log_total liquid1 liquid2 solv1 solv2 solv3 prof1 prof2 total log_total;run;
proc means data=data.t2insp n mean std maxdec=3;var total log_total liquid1 liquid2 solv1 solv2 solv3 prof1 prof2 total log_total;run;
proc means data=data.t2rule n mean std maxdec=3;var total log_total liquid1 liquid2 solv1 solv2 solv3 prof1 prof2 total log_total;run;


