options errorabend;

libname perm '../data';
libname ibes '../data/raw';
libname crsp '../data/raw';
libname comp '../data/raw';

%include "modules/Macro Repository.sas";


data guide; set ibes.det_guidance 
(keep = ticker pdicity measure curr anndats anntims mod_date mod_time prd_yr prd_mon eefymo val_1 val_2 mean_at_date usfirm);
where (measure = "EPS") and (curr = "USD") and (usfirm = 1) /*and (ticker in ('MSFT','HWP','SNO','AAPL')) */;
guide_period = intnx('month',mdy(prd_mon,1,prd_yr),0,'e');
year = year(anndats);
if val_2 eq . and val_1 ne . then do;
	point = 1;
	mgmt_fcst = val_1;
end;
if val_2 ne . and val_1 ne . then do;
	point = 0;
	mgmt_fcst = mean(val_1,val_2);
end;
drop curr measure usfirm;
format guide_period YYMMDDN8.;
if mean_at_date ne .; /* Maybe fill in these values using ibes.statsum at some later date*/
horizon = (guide_period - anndats)/365;
if pdicity = "ANN" then annual = 1;
else if pdicity = "QTR" then annual = 0;
if mgmt_fcst lt 0 and mgmt_fcst ne . then pred_loss = 1;
else if mgmt_fcst ne . then pred_loss = 0;
run;

*keep the latest guidance by date;
proc sort data = guide; by ticker pdicity guide_period anndats descending anntims mod_date mod_time;
run;

proc sort data = guide out = guide_b; by ticker pdicity guide_period anndats;
run;

proc sql;
	create table guide2 as select
	a.*, b.permno
	from guide_b a left join ibes.ibcrsphist b
	on a.ticker = b.ticker
	and (b.sdate-90 le a.anndats or b.sdate = .)
	and (a.anndats le b.edate+90 or b.edate = .);
quit;

proc sort data = guide2; by ticker pdicity guide_period anndats;
where permno ne .;
run;

/*
https://wrds-www.wharton.upenn.edu/pages/support/support-articles/crsp/stock/adjusted-prices-stock-splits-and-dividends/
*/
proc sql;
	create table guide3 as select
	a.*, abs(b.prc)/cfacpr as prc_adj_beg_yr, abs(b.prc) as prc_unadj_beg_yr, b.date
	from guide2 as a left join crsp.dsf b
	on a.permno = b.permno
	and a.guide_period-365 lt b.date lt a.guide_period-358
	having abs(b.prc) ge 1;
quit;

proc sort data = guide3 nodupkey; by ticker pdicity guide_period anndats date; /* There may be multiple permnos for a few tickers that cause duplicate observations by this sort*/
run;

data guide4; set guide3; by ticker pdicity guide_period anndats date;
if first.anndats;
drop date;
run;


proc sort data = guide4 out = temp_ret (keep = permno anndats) nodupkey; by permno anndats;
run;

proc sql;
	create table ret as select distinct
	a.*, b.date, b.ret
	from temp_ret as a left join crsp.dsf as b
	on a.permno = b.permno
	and intnx('weekday',a.anndats,-1) le b.date le intnx('weekday',a.anndats,1);
quit;

proc sql;
	create table ret2 as select distinct
	a.*, b.vwretd
	from ret as a left join crsp.dsi as b
	on a.date = b.date
	order by permno, anndats, date;
quit;

proc sql; 
	create table ret3 as select distinct
	permno, anndats, date, sum(ret-vwretd) as CAR_MF
	from ret2 group by permno, anndats
;
quit;

proc sql;
	create table guide4b as select distinct
	a.*, b.car_mf
	from guide4 as a left join ret3 as b
	on a.permno = b.permno
	and a.anndats = b.anndats;
quit;


proc sort data = guide4b; by ticker pdicity guide_period ;
data guide5; set guide4b; by ticker pdicity guide_period ;
mgmt_fcst_tm1 = lag(mgmt_fcst);
val_1_tm1 = lag(val_1);
val_2_tm1 = lag(val_2);
prc_adj_beg_yr_tm1 = lag(prc_adj_beg_yr);
prc_unadj_beg_yr_tm1 = lag(prc_unadj_beg_yr);
if first.guide_period then do;
	initial = 1;
	mgmt_fcst_tm1 = .;
	val_1_tm1 = .;
	val_2_tm1 = .;
	prc_adj_beg_yr_tm1 = .;
	prc_unadj_beg_yr_tm1 = .;
end;
else initial = 0;
news_adj = (mgmt_fcst - mean_at_date) / prc_adj_beg_yr;
news_unadj = (mgmt_fcst - mean_at_date) / prc_unadj_beg_yr;
rename anndats=mgmt_anndats mgmt_fcst = mgmt_fcst_raw;
mgmt_fcst_adj = (mgmt_fcst / prc_adj_beg_yr);
mgmt_fcst_unadj = (mgmt_fcst / prc_unadj_beg_yr);
mgmt_fcst_adj_tm1 = (mgmt_fcst_tm1 / prc_adj_beg_yr_tm1);
mgmt_fcst_unadj_tm1 = (mgmt_fcst_tm1 / prc_unadj_beg_yr_tm1);
if mgmt_fcst ne . and news_adj ne .; /* this doesn't remove any obs.*/
run;

*** Get bundled guidance by merging mgmt_anndats = anndats_act;
proc sort data = ibes.statsum_epsus out = EAs (keep = ticker anndats_act) nodupkey; by ticker anndats_act;
run;

proc sql;
	create table guide6 as select distinct
	a.*, b.anndats_act, (b.anndats_act ne .) as bundled
	from guide5 as a left join EAS as b
	on a.ticker = b.ticker
	and intnx('weekday',b.anndats_act,-1) le a.mgmt_anndats le intnx('weekday',b.anndats_act,1);
quit;


data statsum; set ibes.statsum_epsus;
error = medest - actual;
run;

proc sql;
	create table guide6b as select distinct
	a.*, b.statpers, b.fiscalp, b.fpi, b.medest, b.error, b.actual, b.stdev as stdest, b.numest
	from guide6 as a left join statsum as b
	on a.ticker = b.ticker
	and a.guide_period = b.fpedats
	and a.pdicity = b.fiscalp
	and intnx('month',a.mgmt_anndats,-3) le b.statpers le a.mgmt_anndats
	and measure = "EPS" 
	/*and USFIRM = 1 and CURCODE = "USD"*/
	order by ticker, pdicity, guide_period, mgmt_anndats, statpers 
	;
quit;


data guide6c; set guide6b; by ticker pdicity guide_period mgmt_anndats;
if last.mgmt_anndats;
dif = mgmt_anndats -statpers;
if dif < 35;
run;

/*proc means data = guide6c n mean std min p1 p25 p50 p75 p99 max;*/
/*var dif;*/
/*run;*/
/*
*Check frequency of bundled guidance;
proc sql;
	select prd_yr as year, mean(bundled) as Bundled_freq
	from guide2 group by prd_yr;
quit;
*70% in 2004 and 80% in 2018 with gradual increase each year during that period.;
*/

* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
* Analyst benchmark - Modified code from Tim Seidel 													   ;
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;

*add analyst data;
data i; set ibes.statsum_epsus 
	(keep = ticker statpers fpi measure usfirm numest lowest medest highest
		stdev actual anndats_act fpedats);
where /*(ticker in ('MSFT', 'HPQ', 'SNO','AAPL')) and*/ (measure eq 'EPS') and (fpi = '6') and (year(statpers) ge 1986);
drop measure usfirm;
year = year(fpedats);
run;


data ibesdata; set i;
if missing(stdev) then stdev=0;
actualdiff=actual-MEDEST;
range=highest-lowest;
run;

data ibesdata; set ibesdata;
* Difference between statistical period and announcement date;
days=ANNDATS_ACT-statpers;
run;

proc sort data=ibesdata; by ticker FPEDATS days;
run;

proc sql;
	create table follow as select
	ticker, year, max(numest) as follow
	from ibesdata group by ticker, year;

	create table ibesdata as select
	a.*, b.follow
	from ibesdata as a left join follow as b
	on a.ticker=b.ticker
	and a.year=b.year;
quit;

proc sort data = ibesdata nodupkey; by ticker statpers fpedats; run;

data ibesdata; set ibesdata;
if days le 0 then delete;
run;

proc sort data=ibesdata; by ticker FPEDATS days;
run;
data ibesdata2; set ibesdata; by ticker FPEDats days;
if first.FPEDats;
run;

data ibesdata2; set ibesdata2;
horizon=days/30;
run;

*create lag actual;
proc sort data=ibesdata2; by ticker FPEDATS;
run;

data ibesdata2; set ibesdata2; by ticker FPEDATS;
actual_tm1=lag(actual);
actualdiff_tm1=lag(actualdiff);
lagyear=lag(year);
if first.ticker then do; actual_tm1=.; actualdiff_tm1=.; lagyear=.; end;
run;

proc sort data=ibesdata2; by ticker FPEDATS;
run;


*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
*Merge to main dataset - add to main program;
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
proc sql; create table guide6d as select
	a.*, b.actualdiff, (b.actual < 0) as loss,
	b.actualdiff_tm1, (b.actualdiff/a.prc_adj_beg_yr) as ue_adj, (b.range/a.prc_adj_beg_yr) as dispersion_adj,
		(b.actualdiff/a.prc_unadj_beg_yr) as ue_unadj, (b.range/a.prc_unadj_beg_yr) as dispersion_unadj, b.follow, log(1+b.follow) as lnfollow
	from guide6c as a left join ibesdata2 as b
	on a.ticker = b.ticker
	and a.anndats_act = b.anndats_act
/* Shouldn't matter whether the forecast is quarterly or annual since the surpise is for q4 should be equal to the surprise for annual*/;	
	quit;

proc sort data=guide6d nodupkey; by ticker pdicity guide_period mgmt_anndats;
run;


* Pull 1 and 2-year ahead to get the forecast revision for guidance issued around the EA;
data eps; set ibes.det_epsus
(keep = ticker cusip anndats_act anndats anntims analys fpi measure value usfirm fpedats actual);						
where /*ticker in ('MSFT', 'HWP', 'SNO', 'AAPL') and*/ (2001 lt year(anndats)) and
		(fpi in ("1","2","6","7")) and (measure = "EPS") and (usfirm = 1);
if value ne .;
year = year(anndats);
rename  value = analyst_fcst;
drop measure usfirm;
if fpi in ("1","2") then pdicity = "ANN";
if fpi in ("6","7") then pdicity = "QTR";
run;

proc sort data = eps nodupkey; by ticker pdicity analys fpedats anndats;
run;

proc sql;
	create table eps2 as select distinct
	a.*, b.mgmt_anndats
	from eps as a left join guide6d as b
	on a.ticker = b.ticker
	and a.fpedats = b.guide_period
	and a.pdicity = b.pdicity;
quit;

data eps3; set eps2;
where mgmt_anndats ne . and anndats lt mgmt_anndats;
dif = mgmt_anndats - anndats;
if dif le 180;
run;

proc sort data = eps3; by ticker pdicity fpedats mgmt_anndats analys anndats anntims;
run;

data eps4; set eps3; by ticker pdicity fpedats mgmt_anndats analys;
if last.analys;
run;

proc means data = eps4 noprint; by ticker pdicity fpedats mgmt_anndats;
output out = summary std(analyst_fcst) = std_fcsts n(analyst_fcst) = count median(analyst_fcst) = med_an_fcst;
run;

proc sql;
	create table guide7 as select distinct
	a.*, b.med_an_fcst, b.std_fcsts, b.count as n_analyst_fcsts
	from guide6d as a left join summary as b
	on a.ticker = b.ticker
	and a.pdicity = b.pdicity
	and a.guide_period = b.fpedats
	and a.mgmt_anndats = b.mgmt_anndats;
quit;

proc sql;
	create table guide8 as select distinct
	a.*, b.actual as actual2
	from guide7 as a left join eps4 as b
	on a.ticker = b.ticker
	and a.pdicity = b.pdicity
	and a.guide_period = b.fpedats
	and a.mgmt_anndats = b.mgmt_anndats;
quit;

/*proc means data = guide8 n mean std min p1 p10 p25 p50 p75 p90 p99 max;*/
/*var actual actual2;*/
/*run;*/
* Calculate accuracy measures;
data guide9; set guide8;
mgmt_accuracy_adj = -1*abs(mgmt_fcst_raw - actual2)/prc_adj_beg_yr;
mgmt_accuracy_unadj = -1*abs(mgmt_fcst_raw - actual2)/prc_unadj_beg_yr;
* From consensus file;
an_accuracy_adj = -1*abs(error)/prc_adj_beg_yr;
an_accuracy_unadj = -1*abs(error)/prc_unadj_beg_yr;
* From outstanding forecasts from the detail file;
an_accuracy_adj2 = -1*abs(med_an_fcst - actual2)/prc_adj_beg_yr;
an_accuracy_unadj2 = -1*abs(med_an_fcst - actual2)/prc_unadj_beg_yr;

if mgmt_accuracy_adj = . or an_accuracy_adj = . then rel_accurate = .;
else if mgmt_accuracy_adj > an_accuracy_adj then rel_accurate = 1;
else rel_accurate = 0;

if mgmt_accuracy_adj = . or an_accuracy_adj2 = . then rel_accurate2 = .;
else if mgmt_accuracy_adj > an_accuracy_adj2 then rel_accurate2 = 1;
else rel_accurate2 = 0;

if bundled = 0 then do;
	ue_adj = 0;
	ue_unadj = 0;
end;
run;


proc sql;
	create table guide9b as select distinct
	a.*, sum(b.rel_accurate)/count(b.rel_accurate) as prior_rel_acc_3yrs, 
			sum(b.rel_accurate2)/count(b.rel_accurate2) as prior_rel_acc2_3yrs, 
				mean(b.mgmt_accuracy_adj) as prior_mgmt_acc_adj_3yrs,
					mean(b.mgmt_accuracy_unadj) as prior_mgmt_acc_unadj_3yrs,
						count(*) as n_fcsts_3yrs
	from guide9 as a left join guide9 as b
	on a.ticker = b.ticker
	and intnx('month',a.mgmt_anndats,-35) le b.mgmt_anndats lt a.mgmt_anndats-1
	group by a.ticker, a.pdicity, a.guide_period, a.mgmt_anndats;
quit;

proc sort data = guide9b nodupkey; by ticker pdicity guide_period mgmt_anndats;
run;


* Add compustat variables;
data y ; set comp.funda
(keep = gvkey cusip cik tic datadate indfmt datafmt popsrc consol curcd fyear fyr
		ib act epspx lct spi at re pi xint prcc_f csho ceq lt sale revt aqp rcp sstk prstkc dv dltis dltr dlcch oancf);
where /*tic in ('MSFT','PII','HPQ','AAPL') and*/ year(datadate)>1980
and indfmt='INDL' and datafmt='STD' and popsrc='D' and consol='C' and curcd='USD' and (at > 0);
mve = prcc_f*csho;
lnmve = log(mve);
mtb = mve / ceq;
BTM = CEQ / MVE;
merger = (AQP ne 0 and aqp ne .);
restructure = (RCP ne 0 and RCP ne .);
array spvars sstk prstkc dv dltis dltr dlcch;
do over spvars;
	if missing(spvars) then spvars = 0;
end;
financing = (SSTK-PRSTKC - DV + DLTIS - DLTR - DLCCH)/AT;
*if financing = . then financing = 0;
cikn = cik*1;
if (ib < 0 and ib ne .) then loss = 1; 
else if ib ne . then loss = 0;
drop indfmt datafmt popsrc consol curcd;
run;

proc sort data = y nodupkey; by gvkey datadate;
run;

proc sql;
	create table y2 as select distinct
	a.*, b.datadate as datadate_tm1, b.fyear as fyear_tm1, 
		b.REVT as REVT_TM1, (a.REVT/b.REVT - 1) as REVT_GRTH, b.SALE as SALE_TM1, (a.SALE/b.SALE - 1) as SALE_GRTH, 
		b.epspx as epspx_tm1, mean(a.at,b.at) as ave_assets
	from y as a left join y as b
	on a.gvkey = b.gvkey
	and a.fyear = b.fyear+1;
quit;

proc sql;
	create table y2 as select
	a.*, b.gsubind as gics
	from y2 as a left join comp.names as b
	on a.gvkey = b.gvkey
	and b.year1 le a.fyear le b.year2
	having gsubind ne "";
quit;

data y3; set y2;
earn = ib / ave_assets;
accruals = (ib - oancf)/ave_assets;
roa = ib/ave_assets;
specItems = spi/ave_assets;
run;

proc sql;
	create table y4 as select distinct
	a.*, std(b.earn) as earn_vol
	from y3 as a left join y3 as b
	on a.gvkey = b.gvkey
	and intnx('year',a.datadate,-15) le b.datadate le a.datadate
	group by a.gvkey, a.datadate;
quit;

proc sql;
	create table temp_pers as select distinct
	a.gvkey, a.datadate, b.datadate as date, b.epspx, b.epspx_tm1
	from y4 as a left join y4 as b
	on a.gvkey = b.gvkey
	and intnx('year',a.datadate,-15) le b.datadate le a.datadate;
quit;

proc sort data = temp_pers nodupkey; by gvkey datadate date;
run;

proc printto log="../logs/proc_reg_junk.log" ; run;
proc reg data = temp_pers outest=fcst_est tableout noprint edf; by gvkey datadate;
model epspx = epspx_tm1;
run; quit;
proc printto; run;

data pers; set fcst_est (keep = gvkey datadate _type_ intercept epspx_tm1 _p_ _EDF_ _RSQ_);
where _type_ = "PARMS";
NOBS = _P_+_EDF_;
drop _p_ _edf_ _type_ ;
if nobs ge 3;
run;

proc sql;
	create table y5 as select distinct
	a.*, b.epspx_tm1 as earn_persist
	from y4 as a left join pers as b
	on a.gvkey = b.gvkey
	and a.datadate = b.datadate;
quit;

proc sql; create table y6 as select distinct
	a.*, b.lpermno as permno
    from y5 as a left join crsp.ccmxpf_linktable
	(where=(linktype in ('LU', 'LC', 'LD', 'LF', 'LN', 'LO', 'LS', 'LX'))) as b
    on (a.gvkey = b.gvkey) 
	and (b.linkdt <= a.datadate or b.linkdt = .B) 
	and (a.datadate <= b.linkenddt or b.linkenddt = .E);
 quit;

data y6; set y6;
where gvkey ne "" and permno ne .;
run;
*Get Historical SIC code from CRSP;
proc sql; create table y6b as select distinct
	a.*, b.siccd, b.exchcd, shrcd
	from y6 as a left join crsp.mse as b
	on (a.permno eq b.permno)
	and b.date le a.datadate le b.NAMEENDT
	order by gvkey, datadate;
quit;

proc sort data = y6b nodupkey; by gvkey datadate;
run;


proc sql;
	create table y6c as select distinct
	a.*, (a.datadate - min(b.datadate)) / 365 as firm_age
	from y6b as a left join comp.funda as b
	on a.gvkey = b.gvkey
	and b.indfmt='INDL' and b.datafmt='STD' and b.popsrc='D' and b.consol='C' and b.curcd='USD' and (b.at > 0)
	group by a.gvkey;
quit;

proc sort data = y6c nodupkey; by gvkey datadate;
run;

proc sort data = y6c out = temp_ret (keep = permno datadate) nodupkey; by permno datadate;
run;

proc sql;
	create table ret as select distinct
	a.*, b.date, b.ret, b.vol*b.cfacshr as vol_adj, b.shrout*b.cfacshr as shrout_adj
	from temp_ret as a left join crsp.dsf as b
	on a.permno = b.permno
	and intnx('month',a.datadate,-11) le b.date le a.datadate;
quit;

proc sql;
	create table ret2 as select distinct
	a.*, b.vwretd
	from ret as a left join crsp.dsi as b
	on a.date = b.date
	order by permno, datadate, date;
quit;

proc sql; create table ret3 as select distinct
	permno, datadate, exp(sum(log(1+ret))) - exp(sum(log(1+vwretd))) as BHAR_YR,
		std(ret) as std_ret_yr, sum(vol_adj)/shrout_adj as turn_yr, count(*) as N_ret
	from ret2 group by permno, datadate
	order by permno, datadate
;
quit;

proc sql;
	create table y6d as select distinct
	a.*, b.bhar_yr, b.std_ret_yr, b.turn_yr
	from y6c as a left join ret3 as b
	on a.permno = b.permno
	and a.datadate = b.datadate;
quit;

proc sort data = y6d nodupkey; by gvkey datadate;
run;


proc sql;
	create table guide10 as select distinct
	a.*, b.gvkey, b.datadate, b.siccd, b.firm_age, b.sale_grth, b.financing, b.btm, b.mve, b.lnmve, b.accruals, b.specitems, 
		b.earn_persist, b.earn_vol, b.bhar_yr, b.std_ret_yr, b.turn_yr, b.gics
	from guide9b as a left join y6d as b
	on a.permno = b.permno
	and intnx('month',a.mgmt_anndats,-13) le b.datadate le mgmt_anndats;
quit;

proc sort data = guide10 nodupkey; by ticker pdicity guide_period mgmt_anndats datadate;
run;

data guide11; set guide10; by ticker pdicity guide_period mgmt_anndats;
if last.mgmt_anndats;
run;


data perm.guidance; set guide11;
run;


data guide11; set perm.guidance;
where 2006 le year le 2019;
run;
*97,303 forecasts;
* Engagement data from sirius;
proc sort data = perm.firm_qtr_bearish out=bearish nodupkey; by permno mostimportantdateutc;
where permno ne .;
run;

data bearish2; set bearish; by permno;
retain count;
if first.permno then count = 1;
else count+1;
run;

proc sql;
	create table guide12 as select distinct
	a.*, b.mostimportantdateutc, 
		b.firm_qtr_bearish_ind_rmean as bearish_engage, b.bearish_in_roll as bearish_engage_2, b.max_bearish_priority_roll as bearish_priority, b.prin_sir, 
		b.pct_bearish as pct_bearish,

		(b.n_analysts - b.n_bearish) as n_non_bearish, b.n_analysts, b.n_in, b.n_out,
		b.n_bearish_in, b.n_bearish_out, b.n_bearish, b.n_analysts_on_call, 
			b.avg_questions_bearish, b.avg_answers_bearish, b.avg_n_answers, 
			b.avg_n_questions
	from guide11 as a left join bearish2 as b
	on a.permno = b.permno
	and intnx('month',a.mgmt_anndats,-12) le b.mostimportantdateutc lt intnx('weekday',a.mgmt_anndats,-1)
	order by ticker, pdicity, guide_period, mgmt_anndats, mostimportantdateutc;
quit;

data guide12b; set guide12; by ticker pdicity guide_period mgmt_anndats;
if last.mgmt_anndats;
run;

proc sql;
	create table guide12c as select distinct
	*, count(*) as N_Fcsts_day
	from guide12b group by ticker, mgmt_anndats;
quit;

data mf_disclosure; set perm.mf_disclosure;
mf_disc = guide*ann_guide_freq*precision;
mf_disc_roll = guider*ann_guide_freq_roll*precision_roll;
run;

proc sql;
	create table guide12d as select
	a.*, b.*
	from guide12c as a left join mf_disclosure as b
	on a.ticker = b.ticker
	and a.datadate = b.fpedats;
quit;


data guide12d; set guide12d;
   end_last_q = intnx('qtr', datadate, -1, 'end');
   format end_last_q yymmddn8.;
run;



proc sort data = guide12d nodupkey; by ticker pdicity guide_period mgmt_anndats;
run;

data guide12e; set guide12d;
if 2006 le year ;
run;

data guide12f; set guide12e;
where (rel_accurate ne .) and (prior_rel_acc_3yrs ne .) 
	and (bearish_engage ne .) 
;
run;

data guide13; set guide12f;
if mgmt_anndats < guide_period;
if news_adj ne 0;
run;

data guide14; set guide13;
if ue_adj > 0.0001 then pos_surp = 1;
else if ue_adj ne . then pos_surp = 0;
if ue_adj < -0.0001 then neg_surp = 1;
else if ue_adj ne . then neg_surp = 0;
if -0.0001 < ue_adj < 0.0001 then neut_surp = 1;
else if ue_adj ne . then neut_surp = 0;
sic2 = substr(left(siccd),1,2);
run;

%let outfile = mf_mkt_resp;
data perm.&outfile; set guide14;
run;

proc export data = guide14
	outfile = "../data/&outfile..dta"
	dbms = dta
	replace;
run;
