Skip to content

brycewang-stanford/Auto-Empirical-Research-Skills / skills/18-jusi-aalto-stata-accounting-research

stata-accounting-research

Unverified

STATA code pattern library for empirical archival accounting research. Provides tested syntax from 126 peer-reviewed JAR (Journal of Accounting Research) replication files (2017-2025). Use when the user asks procedural questions like "How do I implement [method]?" or "Show me code for [technique]" — including: entropy balancing, propensity score matching (PSM), difference-in-differences (DiD), regression discontinuity (RDD), instrumental variables (IV), event studies (CAR/BHAR), survival analysis, Fama-MacBeth regressions, bootstrap, quantile regression, reghdfe/xtreg/areg, clustering standard errors, fixed effects, esttab/outreg2 table formatting, winsorization, leads/lags. Users can specify their variables (e.g., treatment, outcomes, controls) and receive adapted syntax. NOTE: This skill provides code patterns from published papers, not research design advice.

FileKindSize
README-original.mdmarkdown2.9 KB
SKILL.mdskill md4.8 KB
references/JAR_55_bbcl.doother59.1 KB
references/JAR_55_chaplinksy-hanley-moon.doother46.3 KB
references/JAR_55_clm.doother95.1 KB
references/JAR_55_elw.doother58.0 KB
references/JAR_55_hlsw.doother104.1 KB
references/JAR_55_kks.doother365.5 KB
references/JAR_55_lzz.doother25.5 KB
references/JAR_55_madsen.doother307.5 KB
references/JAR_55_mr.doother41.5 KB
references/JAR_56_al.doother75.3 KB
references/JAR_56_bch.doother13.5 KB
references/JAR_56_blv.doother24.6 KB
references/JAR_56_csmw.doother27.6 KB
references/JAR_56_en.doother21.2 KB
references/JAR_56_granja.doother107.9 KB
references/JAR_56_htw.doother18.6 KB
references/JAR_56_kmt.doother5.6 KB
references/JAR_56_ksvw.doother8.0 KB
references/JAR_56_llz.doother3.1 KB
references/JAR_56_ls.doother147.5 KB
references/JAR_56_lv2018.doother123.7 KB
references/JAR_57_bht.doother24.4 KB
references/JAR_57_bl.doother22.6 KB
references/JAR_57_bw.doother263.2 KB
references/JAR_57_cct.doother347.1 KB
references/JAR_57_cgw.doother195.8 KB
references/JAR_57_hcp.doother38.0 KB
references/JAR_57_honigsberg.doother99.5 KB
references/JAR_57_lehmann.doother112.8 KB
references/JAR_57_lm.doother85.5 KB
references/JAR_58_bsz.doother36.9 KB
references/JAR_58_ckm.doother52.3 KB
references/JAR_58_eyring.doother58.1 KB
references/JAR_58_gj.doother187.5 KB
references/JAR_58_gsz.doother121.6 KB
references/JAR_58_htyz.doother30.9 KB
references/JAR_58_jansen.doother99.5 KB
references/JAR_58_joshi.doother43.0 KB
references/JAR_58_kvy.doother19.5 KB
references/JAR_58_msz.doother35.0 KB
references/JAR_58_rauter.doother433.2 KB
references/JAR_58_wy.doother64.5 KB
references/JAR_58_zz.doother125.5 KB
references/JAR_59_breuer.doother826.9 KB
references/JAR_59_ctv.doother383.7 KB
references/JAR_59_dess.doother78.4 KB
references/JAR_59_hmo.doother351.6 KB
references/JAR_60_abkp.doother129.9 KB
references/JAR_60_alv.doother35.4 KB
references/JAR_60_at.doother38.5 KB
references/JAR_60_barrios.doother119.6 KB
references/JAR_60_bbin.doother72.9 KB
references/JAR_60_bdem.doother11.6 KB
references/JAR_60_bl.doother43.5 KB
references/JAR_60_bmsw.doother7.7 KB
references/JAR_60_cc.doother20.1 KB
references/JAR_60_cpss.doother15.4 KB
references/JAR_60_cstv.doother175.3 KB
references/JAR_60_dg.doother19.9 KB
references/JAR_60_duguay.doother8.0 KB
references/JAR_60_fhl.doother71.3 KB
references/JAR_60_gls.doother37.0 KB
references/JAR_60_hmryz.doother28.6 KB
references/JAR_60_hst.doother18.3 KB
references/JAR_60_kt.doother10.4 KB
references/JAR_60_mliu.doother28.3 KB
references/JAR_60_nrwx.doother14.6 KB
references/JAR_60_ptwy.doother26.6 KB
references/JAR_60_skim.doother14.7 KB
references/JAR_61_bcdr.doother524.9 KB
references/JAR_61_bkr.doother51.6 KB
references/JAR_61_bls.doother79.2 KB
references/JAR_61_bo.doother29.7 KB
references/JAR_61_ccm.doother48.7 KB
references/JAR_61_cdss.doother3.9 KB
references/JAR_61_cfhll.doother47.1 KB
references/JAR_61_cfmp.doother12.7 KB
references/JAR_61_ckor.doother77.5 KB
references/JAR_61_cllw.doother11.2 KB
references/JAR_61_clm.doother11.0 KB
references/JAR_61_deller.doother368.9 KB
references/JAR_61_dlrtw.doother30.6 KB
references/JAR_61_dlz.doother13.6 KB
references/JAR_61_drs.doother15.8 KB
references/JAR_61_ds.doother197.5 KB
references/JAR_61_glt.doother1.9 KB
references/JAR_61_gyz.doother7.5 KB
references/JAR_61_ll.doother89.3 KB
references/JAR_61_lszz.doother15.9 KB
references/JAR_61_os.doother128.9 KB
references/JAR_61_zureich.doother22.2 KB
references/JAR_62_1_bdsy_banerjee_etal.doother6.6 KB
references/JAR_62_1_bhs_bouwens_etal.doother59.7 KB
references/JAR_62_1_gsz_gu_sun_zhou.doother46.6 KB
references/JAR_62_2_dgzz_defranco_etal.doother1.8 KB
references/JAR_62_2_gmp_grewal_etal.doother37.2 KB
references/JAR_62_2_xyz_xin_etal.doother25.3 KB
references/JAR_62_3_bht_bloomfield_etal.doother16.0 KB
references/JAR_62_3_ccz_cheynel_etal.doother23.3 KB
references/JAR_62_3_kim.doother2.6 KB
references/JAR_62_3_rr_raghunandan_ruchti.doother15.5 KB
references/JAR_62_4_bd_breuer_dehaan.doother15.3 KB
references/JAR_62_4_cc_cadman_carrizosa.doother20.1 KB
references/JAR_62_4_kmsv_kohler_etal.doother91.5 KB
references/JAR_62_4_mps_marra_etal.doother68.0 KB
references/JAR_62_4_raghunandan.doother50.0 KB
references/JAR_62_4_stv_sran_etal.doother41.1 KB
references/JAR_62_4_xu.doother49.4 KB
references/JAR_62_5_aov_abraham_etal.doother418.9 KB
references/JAR_62_5_gibbons.doother80.1 KB
references/JAR_62_5_jiang.doother114.3 KB
references/JAR_62_5_kstz_krueger_etal.doother26.7 KB
references/JAR_62_5_llz_li_lou_zhang.doother6.2 KB
references/JAR_62_5_lrst_lins_etal.doother66.2 KB
references/JAR_62_5_lswy_lin_etal.doother85.2 KB
references/JAR_62_5_lw_li_wang.doother16.6 KB
references/JAR_63_1_blmv_bastiaansen_etal.doother18.5 KB
references/JAR_63_1_cvw_chen_etal.doother61.2 KB
references/JAR_63_1_fgr_frankel_etal.doother30.3 KB
references/JAR_63_1_hrsx_huang_etal.doother35.5 KB
references/JAR_63_1_jia.doother12.0 KB
references/JAR_63_2_bcrw_bakke_etal.doother271.3 KB
references/JAR_63_2_dlr_desimone_etal.doother83.5 KB
references/JAR_63_2_mqtz_mao_etal.doother94.2 KB
references/JAR_63_2_npstv_nessa_etal.doother7.0 KB
references/JAR_63_3_breuer.doother51.4 KB
references/REFERENCES.mdmarkdown30.7 KB

references/JAR_55_lzz.do

View on GitHub
/* This program generates the main dependent variables used in Ljungqvist, Zhang and Zuo (2017) */

set more off

use fundq, clear /*  This dataset is directly downloaded from Compustat Fundamentals Quarterly */
drop if atq ==.
drop if atq<=0
drop if ceqq==.
drop if (oiadpq==. & oibdpq==. & oancfy==.)

count
gsort gvkey fyearq fqtr -atq
by gvkey fyearq fqtr: keep if _n==1

replace pstkq=0 if pstkq==.
replace dlcq = 0 if dlcq ==.
replace dlttq = 0 if dlttq ==.
replace mibtq =0 if mibtq ==.

gen noaq = ceqq + pstkq + dlttq + dlcq + mibtq
gen rnoaq = oiadpq/noaq
replace rnoaq =. if noaq <0

gen roaq = oiadpq/atq

capture program drop clean
program define clean
sort fyear
by fyear: egen p99 = pctile (`1'), p(99) 
by fyear: egen p1 = pctile (`1'), p(1) 
replace `1'=p99 if `1'>p99&`1'~=.
replace `1'=p1 if `1'<p1&`1'~=.
drop p99 p1
end

clean rnoaq
clean roaq

sort gvkey fyearq fqtr

by gvkey: gen lag4_fyearq = fyearq[_n-4]
gen diff4_fyearq = fyearq - lag4_fyearq

local vol "rnoaq roaq" 
foreach x of local vol {
	sort gvkey fyearq fqtr
	by gvkey: gen lag4_`x' = `x'[_n-4]
	replace lag4_`x'=. if diff4_fyearq > 1
	gen adj_`x' = `x' - lag4_`x'
	by gvkey: gen `x'_1 = adj_`x'[_n+1]
	by gvkey: gen `x'_2 = adj_`x'[_n+2]
	by gvkey: gen `x'_3 = adj_`x'[_n+3]
	by gvkey: gen `x'_4 = adj_`x'[_n+4]
	by gvkey: gen `x'_5 = adj_`x'[_n+5]
	by gvkey: gen `x'_6 = adj_`x'[_n+6]
	by gvkey: gen `x'_7 = adj_`x'[_n+7]
	by gvkey: gen `x'_8 = adj_`x'[_n+8]
	by gvkey: gen `x'_9 = adj_`x'[_n+9]
	by gvkey: gen `x'_10 = adj_`x'[_n+10]
	by gvkey: gen `x'_11 = adj_`x'[_n+11]
	egen std_`x' = rowsd(`x' `x'_1 `x'_2 `x'_3 `x'_4 `x'_5 `x'_6 `x'_7 `x'_8 `x'_9 `x'_10 `x'_11)
	egen std_`x'_n = rownonmiss(`x' `x'_1 `x'_2 `x'_3 `x'_4 `x'_5 `x'_6 `x'_7 `x'_8 `x'_9 `x'_10 `x'_11) 
	}

sort gvkey fyearq fqtr
by gvkey fyearq: keep if _n==1
count
sum std_*

/* We restrict to n>=4 quarters in the regression */
replace std_roaq =. if std_roaq_n < 4
replace std_rnoaq =. if std_rnoaq_n < 4

keep gvkey fyearq std_*
rename fyearq fyear
destring gvkey, force replace
save fundq_vol, replace
/* This program generates the dataset used in Ljungqvist, Zhang and Zuo (2017) */

set more off
use funda, clear /* This dataset is directly downloaded from Compustat Fundamentals Annual*/

/* Because of leads/lags needed, we do not restrict to 1989-2011 here */
drop if fyear < 1986
drop if fyear > 2013
format datadate %d

/* Initial screening */
des indfmt consol popsrc datafmt
keep if indfmt=="INDL"
keep if consol=="C"
keep if popsrc=="D"
keep if datafmt=="STD        "

/* Retain only US firms */
keep if curcd=="USD"
keep if curncd=="USD"
keep if fic=="USA"

/* Industry screening */
gen sic1= int(sich/1000)
gen sic2=int(sich/100)
drop if sic1==6
drop if sic2==49
drop if sic1==9

/* Additional screening */
drop if at==.
drop if at <=0
sort gvkey fyear
by gvkey fyear: keep if _n==1

/* Generate basic variables */
gen mkv=abs(prcc_f)*csho
gen ltbook_lev=dltt/at
gen mb=mkv/ceq
replace mb =. if ceq <0
replace mb=log(mb)

replace xrd=0 if xrd==.
replace sppe =0 if sppe==.
replace dpc=0 if dpc==.

gen capex=(capx-sppe)/at
gen rnd=xrd/at
gen cash=che/at
gen sur_cash=(oancf-dpc+xrd)/at
gen nol=1 if (tlcf > 0 & tlcf~=.)
replace nol=0 if nol==.

keep gvkey fyear fyr datadate cik at ltbook_lev mb sich sale capex rnd cash sur_cash nol tlcf pi  

gen year=year(datadate)
merge m:1 year using gdplev_annual /*gdplev_annual is the dataset containing the GDP deflator, available at http://www.bea.gov/national/xls/gdplev.xls */
drop if _merge==2
drop _merge

gen at09=at*ratio
gen lnat09=log(at09)
gen sale09=sale*ratio

destring gvkey, force replace
tsset gvkey fyear
gen lagsale09=l1.sale09
gen sgrowth=log(sale09/lagsale09)

merge m:1 gvkey using company /* company is the dataset containing the variable (headquarter) "state", directly downloaded from Compustat */
drop if _merge==2
drop _merge

merge 1:1 gvkey fyear using crsp_data_05012015 /* This dataset contains stock return data, computed using the SAS code "LZZ code stock return" */
drop if _merge==2
drop _merge

merge 1:1 gvkey fyear using fundq_vol /* fundq_vol contains vol. measures based on quarterly data, computed using the Stata code "LZZ code volatility" */
drop if _merge==2
drop _merge

merge m:1 gvkey using compustat_start /* Starting year in compustat for each firm */
drop if _merge==2
drop _merge

gen age=fyear - fyear_start + 1
replace age=log(age)

do "HQ corrections 1989-2011 (c) Alexander Ljungqvist, 2013" /*This do-file corrects Compustat's backfilled HQ states to actual historic HQ states for the period 1989-2011. Please contact Alexander Ljungqvist for access to this do-file */

do "Code tax changes - clean" /*This do-file codes which firms are affected by tax changes when. This do-file is based on manually collected data described in the paper and listed in Table A.1 of the Online Appendix */

merge m:1 state year using GDP_growth_rate /*GSP growth rate is the real annual growth rate in gross state product (GSP) using data obtained from the U.S. Bureau of Economic Analysis */
drop if _merge==2
drop _merge

merge m:1 state year using State_unemployment_rate_annual /*State unemployment rate is the state unemployment rate, obtained from the U.S. Bureau of Labor Statistics */
drop if _merge==2
drop _merge
rename value suer

drop if (state=="AB" | state=="BC" | state=="MB"| state=="NB"| state=="NF"| state=="NS"| state=="ON"| state=="PR"| state=="QC"| state=="SK"| state=="VI") /*removing non-US statas*/

tsset gvkey fyear
egen i_t=group(sich fyear)

tab state, gen(st)

*** Dependent variables

replace std_roaq=log(std_roaq)
replace std_rnoaq=log(std_rnoaq)

*** NETS weighted tax changes
di _N
sort gvkey datadate
merge gvkey datadate using "NETS weighted tax changes.dta",nokeep
/* We match Compustat firms by name to the National Establishment Time Series (NETS) database, which contains a comprehensive record of all business establishments in the U.S. since 1989 */
tab _m
replace wtaxchangeup_all=taxchangeup if _m==1 
replace wtaxchangedown_all=taxchangedown if _m==1 
drop _m

/* absoluate magnitude */
replace taxchangedown=abs(taxchangedown)
replace wtaxchangedown_all=abs(wtaxchangedown_all)
replace wtaxchangeup_all=. if taxchangeup==.
replace wtaxchangedown_all=. if taxchangedown==.

tsset gvkey fyear

global Xvars="age lnat09 mb ltbook_lev sur_cash nol sgrowth RET_12"
global Xvars_state=" gdpgr suer"

/* Winsorize */

capture program drop clean
program define clean
sort fyear
by fyear: egen p99 = pctile (`1'), p(99) 
by fyear: egen p1 = pctile (`1'), p(1) 
replace `1'=p99 if `1'>p99&`1'~=.
replace `1'=p1 if `1'<p1&`1'~=.
drop p99 p1
end

local clean_var "std_roaq std_rnoaq $Xvars $Xvars_state"

foreach x of local clean_var {
	clean `x'
}


tsset gvkey fyear

local diff_var "std_roaq std_rnoaq"

/*std_var was generated using three-year data t to t+2; For year t, we compare ROA vol. computed using (t to t+2) data to ROA vol. computed using (t-3 to t-1) data.*/

foreach x of local diff_var {
	gen diff_`x' = `x'- l3.`x'
	
}

sort gvkey fyear
tsset gvkey fyear

replace diff_std_roaq=. if (fyear < 1990 | fyear>2011)
replace diff_std_rnoaq=. if (fyear < 1990 | fyear>2011)

*** Main results

* lagged changes
quiet areg diff_std_roaq L.(taxchangeup taxchangedown) D.($Xvars_state) DL.($Xvars), cluster(state) absorb(i_t)
	outreg2 using table2.xls,   bdec(3) sdec(3) rdec(3)  replace
	test L.taxchangeup+L.taxchangedown=0
	
quiet areg diff_std_rnoaq L.(taxchangeup taxchangedown) D.($Xvars_state) DL.($Xvars), cluster(state) absorb(i_t)
	outreg2 using table2.xls,   bdec(3) sdec(3) rdec(3)  append
	test L.taxchangeup+L.taxchangedown=0

replace taxchangeup=. if (fyear < 1990 | fyear>2011)
replace wtaxchangeup_all=. if (fyear < 1990 | fyear>2011)

* contemporaneous changes
quiet areg diff_std_roaq taxchangeup taxchangedown D.($Xvars_state) DL.($Xvars), cluster(state) absorb(i_t)
	outreg2 using table2.xls,   bdec(3) sdec(3) rdec(3)  append
	test taxchangeup+taxchangedown=0

quiet areg diff_std_rnoaq taxchangeup taxchangedown D.($Xvars_state) DL.($Xvars), cluster(state) absorb(i_t)
	outreg2 using table2.xls,   bdec(3) sdec(3) rdec(3)  append
	test taxchangeup+taxchangedown=0

* Alternative tax change measures: NETS weighted tax changes

quiet areg diff_std_roaq wtaxchangeup_all wtaxchangedown_all D.($Xvars_state) DL.($Xvars), cluster(state) absorb(i_t)
	outreg2 using table2.xls,   bdec(3) sdec(3) rdec(3)  append
		test wtaxchangeup_all+wtaxchangedown_all=0

quiet areg diff_std_rnoaq wtaxchangeup_all wtaxchangedown_all D.($Xvars_state) DL.($Xvars), cluster(state) absorb(i_t)
	outreg2 using table2.xls,   bdec(3) sdec(3) rdec(3)  append
		test wtaxchangeup_all+wtaxchangedown_all=0
/* This program generates the main dependent variables used in Ljungqvist, Zhang and Zuo (2017) */

set more off

use fundq, clear /*  This dataset is directly downloaded from Compustat Fundamentals Quarterly */
drop if atq ==.
drop if atq<=0
drop if ceqq==.
drop if (oiadpq==. & oibdpq==. & oancfy==.)

count
gsort gvkey fyearq fqtr -atq
by gvkey fyearq fqtr: keep if _n==1

replace pstkq=0 if pstkq==.
replace dlcq = 0 if dlcq ==.
replace dlttq = 0 if dlttq ==.
replace mibtq =0 if mibtq ==.

gen noaq = ceqq + pstkq + dlttq + dlcq + mibtq
gen rnoaq = oiadpq/noaq
replace rnoaq =. if noaq <0

gen roaq = oiadpq/atq

capture program drop clean
program define clean
sort fyear
by fyear: egen p99 = pctile (`1'), p(99) 
by fyear: egen p1 = pctile (`1'), p(1) 
replace `1'=p99 if `1'>p99&`1'~=.
replace `1'=p1 if `1'<p1&`1'~=.
drop p99 p1
end

clean rnoaq
clean roaq

sort gvkey fyearq fqtr

by gvkey: gen lag4_fyearq = fyearq[_n-4]
gen diff4_fyearq = fyearq - lag4_fyearq

local vol "rnoaq roaq" 
foreach x of local vol {
	sort gvkey fyearq fqtr
	by gvkey: gen lag4_`x' = `x'[_n-4]
	replace lag4_`x'=. if diff4_fyearq > 1
	gen adj_`x' = `x' - lag4_`x'
	by gvkey: gen `x'_1 = adj_`x'[_n+1]
	by gvkey: gen `x'_2 = adj_`x'[_n+2]
	by gvkey: gen `x'_3 = adj_`x'[_n+3]
	by gvkey: gen `x'_4 = adj_`x'[_n+4]
	by gvkey: gen `x'_5 = adj_`x'[_n+5]
	by gvkey: gen `x'_6 = adj_`x'[_n+6]
	by gvkey: gen `x'_7 = adj_`x'[_n+7]
	by gvkey: gen `x'_8 = adj_`x'[_n+8]
	by gvkey: gen `x'_9 = adj_`x'[_n+9]
	by gvkey: gen `x'_10 = adj_`x'[_n+10]
	by gvkey: gen `x'_11 = adj_`x'[_n+11]
	egen std_`x' = rowsd(`x' `x'_1 `x'_2 `x'_3 `x'_4 `x'_5 `x'_6 `x'_7 `x'_8 `x'_9 `x'_10 `x'_11)
	egen std_`x'_n = rownonmiss(`x' `x'_1 `x'_2 `x'_3 `x'_4 `x'_5 `x'_6 `x'_7 `x'_8 `x'_9 `x'_10 `x'_11) 
	}

sort gvkey fyearq fqtr
by gvkey fyearq: keep if _n==1
count
sum std_*

/* We restrict to n>=4 quarters in the regression */
replace std_roaq =. if std_roaq_n < 4
replace std_rnoaq =. if std_rnoaq_n < 4

keep gvkey fyearq std_*
rename fyearq fyear
destring gvkey, force replace
save fundq_vol, replace
/* This program generates the dataset used in Ljungqvist, Zhang and Zuo (2017) */

set more off
use funda, clear /* This dataset is directly downloaded from Compustat Fundamentals Annual*/

/* Because of leads/lags needed, we do not restrict to 1989-2011 here */
drop if fyear < 1986
drop if fyear > 2013
format datadate %d

/* Initial screening */
des indfmt consol popsrc datafmt
keep if indfmt=="INDL"
keep if consol=="C"
keep if popsrc=="D"
keep if datafmt=="STD        "

/* Retain only US firms */
keep if curcd=="USD"
keep if curncd=="USD"
keep if fic=="USA"

/* Industry screening */
gen sic1= int(sich/1000)
gen sic2=int(sich/100)
drop if sic1==6
drop if sic2==49
drop if sic1==9

/* Additional screening */
drop if at==.
drop if at <=0
sort gvkey fyear
by gvkey fyear: keep if _n==1

/* Generate basic variables */
gen mkv=abs(prcc_f)*csho
gen ltbook_lev=dltt/at
gen mb=mkv/ceq
replace mb =. if ceq <0
replace mb=log(mb)

replace xrd=0 if xrd==.
replace sppe =0 if sppe==.
replace dpc=0 if dpc==.

gen capex=(capx-sppe)/at
gen rnd=xrd/at
gen cash=che/at
gen sur_cash=(oancf-dpc+xrd)/at
gen nol=1 if (tlcf > 0 & tlcf~=.)
replace nol=0 if nol==.

keep gvkey fyear fyr datadate cik at ltbook_lev mb sich sale capex rnd cash sur_cash nol tlcf pi  

gen year=year(datadate)
merge m:1 year using gdplev_annual /*gdplev_annual is the dataset containing the GDP deflator, available at http://www.bea.gov/national/xls/gdplev.xls */
drop if _merge==2
drop _merge

gen at09=at*ratio
gen lnat09=log(at09)
gen sale09=sale*ratio

destring gvkey, force replace
tsset gvkey fyear
gen lagsale09=l1.sale09
gen sgrowth=log(sale09/lagsale09)

merge m:1 gvkey using company /* company is the dataset containing the variable (headquarter) "state", directly downloaded from Compustat */
drop if _merge==2
drop _merge

merge 1:1 gvkey fyear using crsp_data_05012015 /* This dataset contains stock return data, computed using the SAS code "LZZ code stock return" */
drop if _merge==2
drop _merge

merge 1:1 gvkey fyear using fundq_vol /* fundq_vol contains vol. measures based on quarterly data, computed using the Stata code "LZZ code volatility" */
drop if _merge==2
drop _merge

merge m:1 gvkey using compustat_start /* Starting year in compustat for each firm */
drop if _merge==2
drop _merge

gen age=fyear - fyear_start + 1
replace age=log(age)

do "HQ corrections 1989-2011 (c) Alexander Ljungqvist, 2013" /*This do-file corrects Compustat's backfilled HQ states to actual historic HQ states for the period 1989-2011. Please contact Alexander Ljungqvist for access to this do-file */

do "Code tax changes - clean" /*This do-file codes which firms are affected by tax changes when. This do-file is based on manually collected data described in the paper and listed in Table A.1 of the Online Appendix */

merge m:1 state year using GDP_growth_rate /*GSP growth rate is the real annual growth rate in gross state product (GSP) using data obtained from the U.S. Bureau of Economic Analysis */
drop if _merge==2
drop _merge

merge m:1 state year using State_unemployment_rate_annual /*State unemployment rate is the state unemployment rate, obtained from the U.S. Bureau of Labor Statistics */
drop if _merge==2
drop _merge
rename value suer

drop if (state=="AB" | state=="BC" | state=="MB"| state=="NB"| state=="NF"| state=="NS"| state=="ON"| state=="PR"| state=="QC"| state=="SK"| state=="VI") /*removing non-US statas*/

tsset gvkey fyear
egen i_t=group(sich fyear)

tab state, gen(st)

*** Dependent variables

replace std_roaq=log(std_roaq)
replace std_rnoaq=log(std_rnoaq)

*** NETS weighted tax changes
di _N
sort gvkey datadate
merge gvkey datadate using "NETS weighted tax changes.dta",nokeep
/* We match Compustat firms by name to the National Establishment Time Series (NETS) database, which contains a comprehensive record of all business establishments in the U.S. since 1989 */
tab _m
replace wtaxchangeup_all=taxchangeup if _m==1 
replace wtaxchangedown_all=taxchangedown if _m==1 
drop _m

/* absoluate magnitude */
replace taxchangedown=abs(taxchangedown)
replace wtaxchangedown_all=abs(wtaxchangedown_all)
replace wtaxchangeup_all=. if taxchangeup==.
replace wtaxchangedown_all=. if taxchangedown==.

tsset gvkey fyear

global Xvars="age lnat09 mb ltbook_lev sur_cash nol sgrowth RET_12"
global Xvars_state=" gdpgr suer"

/* Winsorize */

capture program drop clean
program define clean
sort fyear
by fyear: egen p99 = pctile (`1'), p(99) 
by fyear: egen p1 = pctile (`1'), p(1) 
replace `1'=p99 if `1'>p99&`1'~=.
replace `1'=p1 if `1'<p1&`1'~=.
drop p99 p1
end

local clean_var "std_roaq std_rnoaq $Xvars $Xvars_state"

foreach x of local clean_var {
	clean `x'
}


tsset gvkey fyear

local diff_var "std_roaq std_rnoaq"

/*std_var was generated using three-year data t to t+2; For year t, we compare ROA vol. computed using (t to t+2) data to ROA vol. computed using (t-3 to t-1) data.*/

foreach x of local diff_var {
	gen diff_`x' = `x'- l3.`x'
	
}

sort gvkey fyear
tsset gvkey fyear

replace diff_std_roaq=. if (fyear < 1990 | fyear>2011)
replace diff_std_rnoaq=. if (fyear < 1990 | fyear>2011)

*** Main results

* lagged changes
quiet areg diff_std_roaq L.(taxchangeup taxchangedown) D.($Xvars_state) DL.($Xvars), cluster(state) absorb(i_t)
	outreg2 using table2.xls,   bdec(3) sdec(3) rdec(3)  replace
	test L.taxchangeup+L.taxchangedown=0
	
quiet areg diff_std_rnoaq L.(taxchangeup taxchangedown) D.($Xvars_state) DL.($Xvars), cluster(state) absorb(i_t)
	outreg2 using table2.xls,   bdec(3) sdec(3) rdec(3)  append
	test L.taxchangeup+L.taxchangedown=0

replace taxchangeup=. if (fyear < 1990 | fyear>2011)
replace wtaxchangeup_all=. if (fyear < 1990 | fyear>2011)

* contemporaneous changes
quiet areg diff_std_roaq taxchangeup taxchangedown D.($Xvars_state) DL.($Xvars), cluster(state) absorb(i_t)
	outreg2 using table2.xls,   bdec(3) sdec(3) rdec(3)  append
	test taxchangeup+taxchangedown=0

quiet areg diff_std_rnoaq taxchangeup taxchangedown D.($Xvars_state) DL.($Xvars), cluster(state) absorb(i_t)
	outreg2 using table2.xls,   bdec(3) sdec(3) rdec(3)  append
	test taxchangeup+taxchangedown=0

* Alternative tax change measures: NETS weighted tax changes

quiet areg diff_std_roaq wtaxchangeup_all wtaxchangedown_all D.($Xvars_state) DL.($Xvars), cluster(state) absorb(i_t)
	outreg2 using table2.xls,   bdec(3) sdec(3) rdec(3)  append
		test wtaxchangeup_all+wtaxchangedown_all=0

quiet areg diff_std_rnoaq wtaxchangeup_all wtaxchangedown_all D.($Xvars_state) DL.($Xvars), cluster(state) absorb(i_t)
	outreg2 using table2.xls,   bdec(3) sdec(3) rdec(3)  append
		test wtaxchangeup_all+wtaxchangedown_all=0
/* This program generates the main dependent variables used in Ljungqvist, Zhang and Zuo (2017) */

set more off

use fundq, clear /*  This dataset is directly downloaded from Compustat Fundamentals Quarterly */
drop if atq ==.
drop if atq<=0
drop if ceqq==.
drop if (oiadpq==. & oibdpq==. & oancfy==.)

count
gsort gvkey fyearq fqtr -atq
by gvkey fyearq fqtr: keep if _n==1

replace pstkq=0 if pstkq==.
replace dlcq = 0 if dlcq ==.
replace dlttq = 0 if dlttq ==.
replace mibtq =0 if mibtq ==.

gen noaq = ceqq + pstkq + dlttq + dlcq + mibtq
gen rnoaq = oiadpq/noaq
replace rnoaq =. if noaq <0

gen roaq = oiadpq/atq

capture program drop clean
program define clean
sort fyear
by fyear: egen p99 = pctile (`1'), p(99) 
by fyear: egen p1 = pctile (`1'), p(1) 
replace `1'=p99 if `1'>p99&`1'~=.
replace `1'=p1 if `1'<p1&`1'~=.
drop p99 p1
end

clean rnoaq
clean roaq

sort gvkey fyearq fqtr

by gvkey: gen lag4_fyearq = fyearq[_n-4]
gen diff4_fyearq = fyearq - lag4_fyearq

local vol "rnoaq roaq" 
foreach x of local vol {
	sort gvkey fyearq fqtr
	by gvkey: gen lag4_`x' = `x'[_n-4]
	replace lag4_`x'=. if diff4_fyearq > 1
	gen adj_`x' = `x' - lag4_`x'
	by gvkey: gen `x'_1 = adj_`x'[_n+1]
	by gvkey: gen `x'_2 = adj_`x'[_n+2]
	by gvkey: gen `x'_3 = adj_`x'[_n+3]
	by gvkey: gen `x'_4 = adj_`x'[_n+4]
	by gvkey: gen `x'_5 = adj_`x'[_n+5]
	by gvkey: gen `x'_6 = adj_`x'[_n+6]
	by gvkey: gen `x'_7 = adj_`x'[_n+7]
	by gvkey: gen `x'_8 = adj_`x'[_n+8]
	by gvkey: gen `x'_9 = adj_`x'[_n+9]
	by gvkey: gen `x'_10 = adj_`x'[_n+10]
	by gvkey: gen `x'_11 = adj_`x'[_n+11]
	egen std_`x' = rowsd(`x' `x'_1 `x'_2 `x'_3 `x'_4 `x'_5 `x'_6 `x'_7 `x'_8 `x'_9 `x'_10 `x'_11)
	egen std_`x'_n = rownonmiss(`x' `x'_1 `x'_2 `x'_3 `x'_4 `x'_5 `x'_6 `x'_7 `x'_8 `x'_9 `x'_10 `x'_11) 
	}

sort gvkey fyearq fqtr
by gvkey fyearq: keep if _n==1
count
sum std_*

/* We restrict to n>=4 quarters in the regression */
replace std_roaq =. if std_roaq_n < 4
replace std_rnoaq =. if std_rnoaq_n < 4

keep gvkey fyearq std_*
rename fyearq fyear
destring gvkey, force replace
save fundq_vol, replace

/* This program generates the dataset used in Ljungqvist, Zhang and Zuo (2017) */

set more off
use funda, clear /* This dataset is directly downloaded from Compustat Fundamentals Annual*/

/* Because of leads/lags needed, we do not restrict to 1989-2011 here */
drop if fyear < 1986
drop if fyear > 2013
format datadate %d

/* Initial screening */
des indfmt consol popsrc datafmt
keep if indfmt=="INDL"
keep if consol=="C"
keep if popsrc=="D"
keep if datafmt=="STD        "

/* Retain only US firms */
keep if curcd=="USD"
keep if curncd=="USD"
keep if fic=="USA"

/* Industry screening */
gen sic1= int(sich/1000)
gen sic2=int(sich/100)
drop if sic1==6
drop if sic2==49
drop if sic1==9

/* Additional screening */
drop if at==.
drop if at <=0
sort gvkey fyear
by gvkey fyear: keep if _n==1

/* Generate basic variables */
gen mkv=abs(prcc_f)*csho
gen ltbook_lev=dltt/at
gen mb=mkv/ceq
replace mb =. if ceq <0
replace mb=log(mb)

replace xrd=0 if xrd==.
replace sppe =0 if sppe==.
replace dpc=0 if dpc==.

gen capex=(capx-sppe)/at
gen rnd=xrd/at
gen cash=che/at
gen sur_cash=(oancf-dpc+xrd)/at
gen nol=1 if (tlcf > 0 & tlcf~=.)
replace nol=0 if nol==.

keep gvkey fyear fyr datadate cik at ltbook_lev mb sich sale capex rnd cash sur_cash nol tlcf pi  

gen year=year(datadate)
merge m:1 year using gdplev_annual /*gdplev_annual is the dataset containing the GDP deflator, available at http://www.bea.gov/national/xls/gdplev.xls */
drop if _merge==2
drop _merge

gen at09=at*ratio
gen lnat09=log(at09)
gen sale09=sale*ratio

destring gvkey, force replace
tsset gvkey fyear
gen lagsale09=l1.sale09
gen sgrowth=log(sale09/lagsale09)

merge m:1 gvkey using company /* company is the dataset containing the variable (headquarter) "state", directly downloaded from Compustat */
drop if _merge==2
drop _merge

merge 1:1 gvkey fyear using crsp_data_05012015 /* This dataset contains stock return data, computed using the SAS code "LZZ code stock return" */
drop if _merge==2
drop _merge

merge 1:1 gvkey fyear using fundq_vol /* fundq_vol contains vol. measures based on quarterly data, computed using the Stata code "LZZ code volatility" */
drop if _merge==2
drop _merge

merge m:1 gvkey using compustat_start /* Starting year in compustat for each firm */
drop if _merge==2
drop _merge

gen age=fyear - fyear_start + 1
replace age=log(age)

do "HQ corrections 1989-2011 (c) Alexander Ljungqvist, 2013" /*This do-file corrects Compustat's backfilled HQ states to actual historic HQ states for the period 1989-2011. Please contact Alexander Ljungqvist for access to this do-file */

do "Code tax changes - clean" /*This do-file codes which firms are affected by tax changes when. This do-file is based on manually collected data described in the paper and listed in Table A.1 of the Online Appendix */

merge m:1 state year using GDP_growth_rate /*GSP growth rate is the real annual growth rate in gross state product (GSP) using data obtained from the U.S. Bureau of Economic Analysis */
drop if _merge==2
drop _merge

merge m:1 state year using State_unemployment_rate_annual /*State unemployment rate is the state unemployment rate, obtained from the U.S. Bureau of Labor Statistics */
drop if _merge==2
drop _merge
rename value suer

drop if (state=="AB" | state=="BC" | state=="MB"| state=="NB"| state=="NF"| state=="NS"| state=="ON"| state=="PR"| state=="QC"| state=="SK"| state=="VI") /*removing non-US statas*/

tsset gvkey fyear
egen i_t=group(sich fyear)

tab state, gen(st)

*** Dependent variables

replace std_roaq=log(std_roaq)
replace std_rnoaq=log(std_rnoaq)

*** NETS weighted tax changes
di _N
sort gvkey datadate
merge gvkey datadate using "NETS weighted tax changes.dta",nokeep
/* We match Compustat firms by name to the National Establishment Time Series (NETS) database, which contains a comprehensive record of all business establishments in the U.S. since 1989 */
tab _m
replace wtaxchangeup_all=taxchangeup if _m==1 
replace wtaxchangedown_all=taxchangedown if _m==1 
drop _m

/* absoluate magnitude */
replace taxchangedown=abs(taxchangedown)
replace wtaxchangedown_all=abs(wtaxchangedown_all)
replace wtaxchangeup_all=. if taxchangeup==.
replace wtaxchangedown_all=. if taxchangedown==.

tsset gvkey fyear

global Xvars="age lnat09 mb ltbook_lev sur_cash nol sgrowth RET_12"
global Xvars_state=" gdpgr suer"

/* Winsorize */

capture program drop clean
program define clean
sort fyear
by fyear: egen p99 = pctile (`1'), p(99) 
by fyear: egen p1 = pctile (`1'), p(1) 
replace `1'=p99 if `1'>p99&`1'~=.
replace `1'=p1 if `1'<p1&`1'~=.
drop p99 p1
end

local clean_var "std_roaq std_rnoaq $Xvars $Xvars_state"

foreach x of local clean_var {
	clean `x'
}


tsset gvkey fyear

local diff_var "std_roaq std_rnoaq"

/*std_var was generated using three-year data t to t+2; For year t, we compare ROA vol. computed using (t to t+2) data to ROA vol. computed using (t-3 to t-1) data.*/

foreach x of local diff_var {
	gen diff_`x' = `x'- l3.`x'
	
}

sort gvkey fyear
tsset gvkey fyear

replace diff_std_roaq=. if (fyear < 1990 | fyear>2011)
replace diff_std_rnoaq=. if (fyear < 1990 | fyear>2011)

*** Main results

* lagged changes
quiet areg diff_std_roaq L.(taxchangeup taxchangedown) D.($Xvars_state) DL.($Xvars), cluster(state) absorb(i_t)
	outreg2 using table2.xls,   bdec(3) sdec(3) rdec(3)  replace
	test L.taxchangeup+L.taxchangedown=0
	
quiet areg diff_std_rnoaq L.(taxchangeup taxchangedown) D.($Xvars_state) DL.($Xvars), cluster(state) absorb(i_t)
	outreg2 using table2.xls,   bdec(3) sdec(3) rdec(3)  append
	test L.taxchangeup+L.taxchangedown=0

replace taxchangeup=. if (fyear < 1990 | fyear>2011)
replace wtaxchangeup_all=. if (fyear < 1990 | fyear>2011)

* contemporaneous changes
quiet areg diff_std_roaq taxchangeup taxchangedown D.($Xvars_state) DL.($Xvars), cluster(state) absorb(i_t)
	outreg2 using table2.xls,   bdec(3) sdec(3) rdec(3)  append
	test taxchangeup+taxchangedown=0

quiet areg diff_std_rnoaq taxchangeup taxchangedown D.($Xvars_state) DL.($Xvars), cluster(state) absorb(i_t)
	outreg2 using table2.xls,   bdec(3) sdec(3) rdec(3)  append
	test taxchangeup+taxchangedown=0

* Alternative tax change measures: NETS weighted tax changes

quiet areg diff_std_roaq wtaxchangeup_all wtaxchangedown_all D.($Xvars_state) DL.($Xvars), cluster(state) absorb(i_t)
	outreg2 using table2.xls,   bdec(3) sdec(3) rdec(3)  append
		test wtaxchangeup_all+wtaxchangedown_all=0

quiet areg diff_std_rnoaq wtaxchangeup_all wtaxchangedown_all D.($Xvars_state) DL.($Xvars), cluster(state) absorb(i_t)
	outreg2 using table2.xls,   bdec(3) sdec(3) rdec(3)  append
		test wtaxchangeup_all+wtaxchangedown_all=0

brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 323kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 198kUpdated Claude CodeCodexVerifiedInstall