capture clear all
set more off

global seed 80768940	// From random dollar bill, 7sep2016 Jon Mills
set seed $seed
//di "Random number generator seed starting point: `c(seed)'"

global cleandata 	"X:\Tulane\Clean Data\Data"
global data 		"X:\Tulane\Analyses and Papers\alt_schools\data"
global programs 	"X:\Tulane\Analyses and Papers\alt_schools\do_files"
global output		"X:\Tulane\Analyses and Papers\alt_schools\output"

//	project:	Discipline Disparities in LA 
//				Barrett, McEachin, Mills, Valant

//  Table of Contents
//==============================================================================
//  #1  Build Analysis Files 					(begin line 43)
//  #2  Create Table 1		 					(begin line 354)
//  #3  Create Table 2		 					(begin line 1118)
//  #4  Create Table 3		 					(begin line 1313)
//  #5  Create Table 4		 					(begin line 1414) 
//  #6  Create Table 5 & Table 6				(begin line 1515)
//  #7  Create Table 7 & Figure 7				(begin line 1695)
//  #8  Create Table 8							(begin line 2236)
//  #9  Create Table 9							(begin line 2313)
//  #10 Create Figure 1 & Figure 2				(begin line 2431)
//  #11 Create Figure 3, Figure 4, & Table A1	(begin line 2480)
//  #12 Create Figure 5, Figure 6, & Table A2	(begin line 2734)
//  #13 Create Table A3							(begin line 2998)
//  #14 Create Table A4							(begin line 3099)
//  #15 Create Table A5 & Table A6				(begin line 3200)
//  #16 Create Table A7 & Table A8				(begin line 3292)
//  #17 Create Table A9							(begin line 3472)
//  #18 Create Table A10						(begin line 3523)
//  #19 Create Table A11						(begin line 4027)
//  #20 Create Table A12						(begin line 4104)
//  #21 Create Figures A1-A4					(begin line 4222)
//==============================================================================


//==============================================================================
// 1 - Build Analysis Files
//==============================================================================

//=============================================================
//	#1A	Merge in cleaned data, starting with enrollment file
//=============================================================

	use "$data\00-enrollment_cleaned.dta", replace
	tab year,m

	merge 1:1 stud_id year using "X:\Tulane\Clean Data\Data\frpl\frpl_clean.dta"	// v02 update, pulling in file with persistent frpl status
	drop tot_free tot_red never_free_flg never_red_flg trans_red_flg _merge

	merge m:1 stud_id using "X:\Tulane\Clean Data\Data\gen_eth\gender_ethnicity_tbl.dta"
	keep if _merge==3		// 60,221 records dropped
	drop pre_11 post_11 race_change _merge

	merge 1:1 stud_id year using "X:\Tulane\Clean Data\Data\special_ed\sped_tbl.dta"
	tab year if _m==2
	drop if _merge==2		// 837,961 dropped
	drop _merge

	merge 1:1 stud_id year using "X:\Tulane\Clean Data\Data\lep\lep_tbl.dta"
	drop if _merge==2		// 837,961 dropped	(!! same number?)
	drop _merge

	merge 1:1 stud_id year using "X:\Tulane\Analyses and Papers\alt_schools\data\testscores_cleaned.dta"
	drop if _merge==2		// 273,847 records dropped
	drop grade_e1-sch_id_e2 test_male-_merge

	merge 1:m stud_id year using "$data\00-disc_cleaned.dta"
	drop if _m==2			// 6,588 records droppeds
	gen flag_nodisc = 1 if _m==1
	la var flag_nodisc "not in discipline data"
	drop _m


//===================================================
//	#1B	Cleaning up variables
//===================================================

* Updating suspension variables for non-suspended
	replace suspend = 0 		if flag_nodisc==1
	replace suspend_di = 0		if flag_nodisc==1
	replace suspend_vi = 0		if flag_nodisc==1
	replace suspend_nvi = 0		if flag_nodisc==1
	replace oss = 0 			if flag_nodisc==1
	replace evr_suspend = 0 	if flag_nodisc==1
	replace evr_suspend_di = 0	if flag_nodisc==1
	replace evr_suspend_vi = 0	if flag_nodisc==1
	replace evr_suspend_nvi = 0	if flag_nodisc==1
	replace mult_suspend = 0	if flag_nodisc==1
	replace disc_rank = 0 if missing(disc_cd)

* Identifying if student was suspended in the prior school year (v03 addition, 2018-0815 jnm)
	preserve
		keep stud_id year evr_suspend
		duplicates drop stud_id year, force	// keeping one record per student per year
		xtset stud_id year
		gen susp_prioryr = l.evr_suspend
		la var susp_prioryr "Suspended in Prior Year"
		la def spy 0 "0.not suspended last year" 1 "1.suspended last year"
		la val susp_prioryr spy
		tempfile lag_susp
		save `lag_susp'
	restore
	merge m:1 stud_id year using `lag_susp'
	drop _m
	order susp_prioryr, after(evr_suspend)
	sort stud_id year 

* Gender
	rename gender male

* Ethnicity
* creating mutually exclusive ethnicity variable. analysis will focus on
* black vs. white student comparisons. we are identifying black students
* as those who are ever identified as black (using race_10 or race_11) 
* and white students as those who are identified as white, and never
* identified as black.
	gen black = 1 if race_10==1 | race_11==1

	gen other = 1 if (inlist(race_10,3,4,5,6,7)) | (inlist(race_11,3,4,5,6,7))
		replace other = 0 if black==1
		
	gen white = 1 if (race_10==2 & race_11==2) 			///
				   | (race_10==2 & missing(race_11)) 	///
				   | (race_11==2 & missing(race_10))
		replace white = 0 if black==1 | other==1

	replace black = 0 if white==1 | other==1
	replace other = 0 if white==1

	replace black = . if missing(race_10) & missing(race_11)
	replace white = . if missing(race_10) & missing(race_11)
	replace other = . if missing(race_10) & missing(race_11)
		
	replace black = 0 if (white==1 | other==1) & (!missing(race_10) & !missing(race_11))
	replace other = 0 if (white==1 | black==1) & (!missing(race_10) & !missing(race_11))

* FRPL
	gen freepl = (frpl_det==3)
	replace freepl = . if missing(frpl_det)
	gen redpl = (frpl_det==2)
	replace redpl = . if missing(frpl_det)
	gen fullpl = (frpl_det==1)
	replace fullpl = . if missing(frpl_det)
	gen frpl = 1 if freepl==1 | redpl==1
		replace frpl = 0 if fullpl==1
		replace frpl = . if missing(freepl)
	
* Cluster var
	egen sgy = group(sch_id grade year)
	la var sgy "school-grade-year fixed effect"
	egen sg  = group(sch_id grade)
	la var sg "school-grade fixed effect"

	drop lea_id		//var is missing for most obs
	gen lea_id=substr(sch_id,1,3)
	la var lea_id "District id"
	order lea_id, before(sch_id)
	egen lgy = group(lea_id grade year)
	la var lgy "district-grade-year fixed effect"

* Label variables
	la var suspend 			"Suspension"
	la var oss 				"Out of School Suspension"
	la var mult_suspend		"Multiple Suspensions"
	la var first_off_di		"Discretionary 1st Off."
	la var first_off_vi		"Violent 1st Off."
	la var first_off_nvi 	"Non-Violent 1st Off."
	la var susp_days_est	"Days Suspended"
	la var susp_days_di		"Days Suspended for Discretionary Off."
	la var susp_days_vi		"Days Suspended for Violent Off."
	la var susp_days_nvi 	"Days Suspended for Non-violent Off."
	la var oss_days_est		"Days of Out of School Suspension"
	la var male 			"Male"
	la var black 			"Black"
	la var white 			"White"
	la var other 			"Other Race"
	la var freepl 			"Lunch Status: Free Lunch"
	la var redpl 			"Lunch Status: Reduced Price"
	la var fullpl 			"Lunch Status: Full Price"
	la var frpl				"Free- or Reduced-Price Lunch"
	la var sped2 			"Student with Disabilities"
	la var lep 				"Limited English Proficiency"
	la var math_lag1 		"Standardized Math (t-1)"
	la var ela_lag1  		"Standardized ELA (t-1)"


//==============================
//	#1C	Saving base files
//==============================

* Saving general file
	la data "all enrolled students, with multiple records per disciplined student \ 2018-0816 jnm"
	note: sample01-all_students-v02.dta \ all enrolled students, with multiple records per disciplined student \ build_analysis_file.do 2018-0816 jnm
	save "$data\sample01-all_students-v02.dta", replace

* Discipline file with demographic variables for analysis 
	drop if flag_nodisc==1
	la data "disciplined students only, multiple records per student \ 2018-0816 jnm"
	note: sample01-disc_students-v02.dta \ disciplined students only, multiple records per student \ build_analysis_file.do 2018-0816 jnm
	save "$data\sample01-disc_students-v02.dta", replace	

//=================================
//  #1D Create Fight analysis file
//=================================
*bring in admin data
	merge m:1 sch_id year using disc_admin2.dta
	drop if _merge==2
	drop _merge

*create performance groups
	foreach x of numlist 2001(1)2014 {
	xtile quart_`x' = math_lag1 if year==`x', nq(4)
	}
	egen perf_group=rowtotal( quart_2001- quart_2014)
	drop quart_2001- quart_2014

*Create indicators for first fights within year and across all years
	*within year
	bysort stud_id year: egen first_fight=min(disc_dt_start) if disc_cd==16
	bysort stud_id year: egen first_fight_yr=mode(first_fight)
	drop first_fight
	bysort stud_id year: egen first_disc_yr=min(disc_dt_start)
	gen first_disc_yr_fight=0
	replace first_disc_yr_fight=1 if first_fight_yr==first_disc_yr
	*across all years
	bysort stud_id: egen first_fight=min(disc_dt_start) if disc_cd==16
	bysort stud_id: egen first_fight_ever=mode(first_fight)
	drop first_fight
	bysort stud_id: egen first_disc=min(disc_dt_start)
	bysort stud_id: egen first_disc_ever=mode(first_disc)
	drop first_disc
	gen first_disc_ever_fight=0
	replace first_disc_ever_fight=1 if first_fight_ever==first_disc_ever

*Create variable that counts discipline within year and also an indicator of receiving at least one
	bysort stud_id year: egen fight_count=count(disc_dt_start) if disc_cd==16
	bysort stud_id year: egen disc_count=count(disc_dt_start)
	
	gen previous_fight=0
	replace previous_fight=1 if disc_dt_start>first_fight_yr
	gen previous_disc=0
	replace previous_disc=1 if disc_dt_start>first_disc_yr

*Create same fight indicator and incident id
	bysort sch_id year disc_dt_start: egen same_fight=count(stud_id) if disc_cd==16
	keep if same_fight==2
	egen incident_id=group(sch_id year disc_dt_start)

*Generate outcome variables
	workdays disc_dt_start disc_dt_end, gen(days)
	
	gen more_days=0
	replace more_days=1 if std_1_days>std_2_days & days==std_1_days
	replace more_days=1 if std_2_days>std_1_days & days==std_2_days

	gen diff_days=0
	replace diff_days=days-days[_n+1] if blk_wht==1 & std_1_blk==1
	replace diff_days=days-days[_n-1] if blk_wht==1 & std_2_blk==1

*Generate various indicators to identify fight groups
	bysort incident_id: egen prior_fights=max(previous_fight)
	bysort stud_id: egen first_ever=min(disc_dt_start) if disc_cd==16
	gen first_fight=0
	replace first_fight=1 if first_ever==disc_dt_start
	bysort incident_id: egen no_prior=min(first_fight)
	*first fight of the year
	bysort incident_id: egen use=min(first_disc_yr_fight)
	*first fight ever
	bysort incident_id: egen use2=min(first_disc_ever_fight)
	*only OSS
	bysort incident_id: egen mode_disc_type=mode(disc_type)

*generate student 1 and student 2 indicators and their associated demos
	sort incident_id stud_id
	
	gen     stud_one_eth   = stud_eth  		 if incident_id!=incident_id[_n-1]
	gen     stud_one_days  = days      		 if incident_id!=incident_id[_n-1]
	gen     stud_one_type  = disc_type 		 if incident_id!=incident_id[_n-1]
	gen     stud_one_frpl  = always_dis_flg  if incident_id!=incident_id[_n-1]
	gen     stud_one_non   = never_dis_flg   if incident_id!=incident_id[_n-1]
	gen     stud_one_grade = grade     		 if incident_id!=incident_id[_n-1]
	gen     stud_one_male  = stud_male 		 if incident_id!=incident_id[_n-1]
	gen     stud_one_sped  = sped2     		 if incident_id!=incident_id[_n-1]
	gen     stud_one_high  = 0        		 if incident_id!=incident_id[_n-1]
	replace stud_one_high  = 1        		 if stud_one_high==0 & perf_group==4
	gen     stud_one_low   = 0        		 if incident_id!=incident_id[_n-1]
	replace stud_one_low   = 1       		 if stud_one_low==0 & perf_group==1
	gen     stud_one_first = first_disc_yr   if incident_id!=incident_id[_n-1]

	replace stud_one_eth   = stud_one_eth[_n-1]   if incident_id==incident_id[_n-1]
	replace stud_one_days  = stud_one_days[_n-1]  if incident_id==incident_id[_n-1]
	replace stud_one_type  = stud_one_type[_n-1]  if incident_id==incident_id[_n-1]
	replace stud_one_frpl  = stud_one_frpl[_n-1]  if incident_id==incident_id[_n-1]
	replace stud_one_non   = stud_one_non[_n-1]   if incident_id==incident_id[_n-1]
	replace stud_one_grade = stud_one_grade[_n-1] if incident_id==incident_id[_n-1]
	replace stud_one_male  = stud_one_male[_n-1]  if incident_id==incident_id[_n-1]
	replace stud_one_sped  = stud_one_sped[_n-1]  if incident_id==incident_id[_n-1]
	replace stud_one_high  = stud_one_high[_n-1]  if incident_id==incident_id[_n-1]
	replace stud_one_low   = stud_one_low[_n-1]   if incident_id==incident_id[_n-1]
	replace stud_one_first = stud_one_first[_n-1] if incident_id==incident_id[_n-1]

	gen     stud_two_eth   = stud_eth 		 if incident_id!=incident_id[_n+1]
	gen     stud_two_days  = days      		 if incident_id!=incident_id[_n+1]
	gen     stud_two_type  = disc_type 		 if incident_id!=incident_id[_n+1]
	gen     stud_two_frpl  = always_dis_flg  if incident_id!=incident_id[_n+1]
	gen     stud_two_non   = never_dis_flg   if incident_id!=incident_id[_n+1]
	gen     stud_two_grade = grade    		 if incident_id!=incident_id[_n+1]
	gen     stud_two_male  = stud_male		 if incident_id!=incident_id[_n+1]
	gen     stud_two_sped  = sped2  		 if incident_id!=incident_id[_n+1]
	gen     stud_two_high  = 0        		 if incident_id!=incident_id[_n+1]
	replace stud_two_high  = 1        		 if stud_two_high==0 & perf_group==4
	gen     stud_two_low   = 0        		 if incident_id!=incident_id[_n+1]
	replace stud_two_low   = 1       		 if stud_two_low==0 & perf_group==1
	gen     stud_two_first = first_disc_yr   if incident_id!=incident_id[_n+1]

	replace stud_two_eth   = stud_two_eth[_n+1]   if incident_id==incident_id[_n+1]
	replace stud_two_days  = stud_two_days[_n+1]  if incident_id==incident_id[_n+1]
	replace stud_two_type  = stud_two_type[_n+1]  if incident_id==incident_id[_n+1]
	replace stud_two_frpl  = stud_two_frpl[_n+1]  if incident_id==incident_id[_n+1]
	replace stud_two_non   = stud_two_non[_n+1]   if incident_id==incident_id[_n+1]
	replace stud_two_grade = stud_two_grade[_n+1] if incident_id==incident_id[_n+1]
	replace stud_two_male  = stud_two_male[_n+1]  if incident_id==incident_id[_n+1]
	replace stud_two_sped  = stud_two_sped[_n+1]  if incident_id==incident_id[_n+1]
	replace stud_two_high  = stud_two_high[_n+1]  if incident_id==incident_id[_n+1]
	replace stud_two_low   = stud_two_low[_n+1]   if incident_id==incident_id[_n+1]
	replace stud_two_first = stud_two_first[_n+1] if incident_id==incident_id[_n+1]

*create black/white and poor/nonpoor fight groups
	replace stud_eth=. if stud_eth>2
	replace stud_eth=0 if stud_eth==2
	gsort incident_id -stud_eth	
	gen blk_wht = 0
	replace blk_wht = 1 if std_1_blk==1 & std_2_blk==0 & stud_eth==1
	replace blk_wht = 1 if std_1_blk==1 & std_2_blk==0 & stud_eth==0
	
	gsort incident_id -never_dis_flg	
	gen frp_non = 0
	replace frp_non = 1 if std_1_frp==1 & std_2_frp==0 & always_dis_flg==1
	replace frp_non = 1 if std_1_frp==1 & std_2_frp==0 & always_dis_flg==0

*save data	
	destring sch_id, force gen(group)
	la data "fight analysis file - nab"
	note: fights.dta
	save "$data\fights.dta", replace	

//==============================================================================
// 2 - Create Table 1
//==============================================================================

//==================================================================
//	#2A	Data prep
//==================================================================

	use  "$data\sample01-all_students-v02.dta", replace

* Dropping duplicates by stud_id year (all discipline students)
	duplicates drop stud_id year, force
	tab suspend,m

* Create categorical variables for descriptives
	gen female = .
		replace female = 1 if male==0
		replace female = 0 if male==1
	la var female "Female"

	rename sped2 sped
	la var sped "Student with Disabilities"

	gen notsped = .
		replace notsped = 1 if sped==0
		replace notsped = 0 if sped==1
	la var notsped "stu is not sped"
	
* Renaming achievement measures for output
	foreach test in ela math sci ss {
		rename `test'_lag1 `test'l1
	}	
	la var mathl1	"Standardized Math (t-1)"
	la var elal1  	"Standardized ELA (t-1)"
	la var scil1 	"Standardized Science (t-1)"
	la var ssl1  	"Standardized Social Studies (t-1)"

* Global identifying variables
	global achieve 	"mathl1 elal1 scil1 ssl1"

		
//================================
//	#2B	Build output matrix
//================================
	
	matrix out = J(31,10,.)
	matrix rownames out = ///
		tot		 susp	suspvi	suspnvi	__							///
		race 	 black 	white 	other	__ 							///
		poverty  free 	reduced full	always	sometimes never  __	///
		spedstat sped 	notsped __ 									///
		gender	 male 	female 	__ 									///
		achieve  ela 	math 	sci		soc
	matrix colnames out = ///
		alln	allp	///
		blackn 	blackp	///
		whiten 	whitep	///
		alwaysn alwaysp	///
		nevern	neverp	
		
	
//================================
//	#2C	Populate output matrix
//================================

//	1. All students
	scalar col = 1

	* Suspension
	count if !missing(evr_suspend) 
		scalar total = r(N)			// this will be the denominator, which includes all students
	count if evr_suspend==1
		scalar susp = r(N)			// count of students suspended once in year
	count if evr_suspend_vi==1
		scalar suspvi = r(N)
	count if evr_suspend_nvi==1
		scalar suspnvi = r(N)
	scalar susp_p = susp/total
	scalar suspvi_p = suspvi/total
	scalar suspnvi_p = suspnvi/total
	scalar row = rownumb(out,"tot")
	matrix out[row	,col  ] = total
	matrix out[row+1,col  ] = susp
	matrix out[row+1,col+1] = susp_p
	matrix out[row+2,col  ] = suspvi
	matrix out[row+2,col+1] = suspvi_p
	matrix out[row+3,col  ] = suspnvi
	matrix out[row+3,col+1] = suspnvi_p
		
	* Race
	count if !missing(black) & !missing(white) & !missing(other)
		scalar race_tot = r(N)
	count if black==1
		scalar race_black = r(N)
	count if white==1
		scalar race_white = r(N)
	count if other==1
		scalar race_other = r(N)
	scalar black_p = race_black/race_tot
	scalar white_p = race_white/race_tot
	scalar other_p = race_other/race_tot
	scalar row = rownumb(out,"race")
	matrix out[row  ,col  ] = race_tot
	matrix out[row+1,col  ] = race_black
	matrix out[row+1,col+1] = black_p
	matrix out[row+2,col  ] = race_white
	matrix out[row+2,col+1] = white_p
	matrix out[row+3,col  ] = race_other
	matrix out[row+3,col+1] = other_p
		
	* Poverty
	count if !missing(frpl)
		scalar pov_tot = r(N)
	count if freepl==1
		scalar pov_free = r(N)
	count if redpl==1
		scalar pov_red= r(N)
	count if fullpl==1
		scalar pov_full = r(N)
	count if always_dis_flg==1				// v02 addition
		scalar always = r(N)
	count if trans_dis_flg==1
		scalar sometimes = r(N)
	count if never_dis_flg==1		
		scalar notta = r(N)					// having issue making pct when using word 'never'. moving along.
	scalar free_p 	= pov_free/pov_tot
	scalar red_p 	= pov_red/pov_tot
	scalar full_p 	= pov_full/pov_tot
	scalar always_p = always/pov_tot		// v02 addition
	scalar notta_p 	= notta/pov_tot	
	scalar somet_p	= sometimes/pov_tot		
	scalar row = rownumb(out,"poverty")
	matrix out[row  ,col  ] = pov_tot
	matrix out[row+1,col  ] = pov_free
	matrix out[row+1,col+1] = free_p
	matrix out[row+2,col  ] = pov_red
	matrix out[row+2,col+1] = red_p
	matrix out[row+3,col  ] = pov_full
	matrix out[row+3,col+1] = full_p
	matrix out[row+4,col  ] = always		// v02 addition
	matrix out[row+4,col+1] = always_p
	matrix out[row+5,col  ] = sometimes
	matrix out[row+5,col+1] = somet_p
	matrix out[row+6,col  ] = notta
	matrix out[row+6,col+1] = notta_p	

	* Special Ed
	count if !missing(sped)
		scalar sped_tot = r(N)
	count if sped==1
		scalar spedn = r(N)
	count if notsped==1
		scalar notspedn= r(N)
	scalar sped_p = spedn/sped_tot
	scalar notsped_p = notspedn/sped_tot
	scalar row = rownumb(out,"spedstat")
	matrix out[row  ,col  ] = sped_tot
	matrix out[row+1,col  ] = spedn
	matrix out[row+1,col+1] = sped_p
	matrix out[row+2,col  ] = notspedn
	matrix out[row+2,col+1] = notsped_p

	* Gender
	count if !missing(male) & !missing(female)
		scalar gen_tot = r(N)
	count if male==1
		scalar malen = r(N)
	count if female==1
		scalar femalen= r(N)
	scalar male_p = malen/gen_tot
	scalar female_p = femalen/gen_tot
	scalar row = rownumb(out,"gender")

	matrix out[row  ,col  ] = gen_tot
	matrix out[row+1,col  ] = malen
	matrix out[row+1,col+1] = male_p
	matrix out[row+2,col  ] = femalen
	matrix out[row+2,col+1] = female_p

	* Achieve
	sum elal1
		scalar elan = r(N)
		scalar elam = r(mean)
	sum mathl1 
		scalar mathn = r(N)
		scalar mathm = r(mean)
	sum scil1
		scalar scin = r(N)
		scalar scim = r(mean)
	sum ssl1
		scalar ssn = r(N)
		scalar ssm = r(mean)
	scalar row = rownumb(out,"achieve")
	matrix out[row+1,col  ] = elan
	matrix out[row+1,col+1] = elam
	matrix out[row+2,col  ] = mathn
	matrix out[row+2,col+1] = mathm
	matrix out[row+3,col  ] = scin
	matrix out[row+3,col+1] = scim
	matrix out[row+4,col  ] = ssn
	matrix out[row+4,col+1] = ssm

//	2. Black students
	preserve
	keep if black==1
	scalar col = 3

	* Suspension
	count if !missing(evr_suspend) 
		scalar total = r(N)			// this will be the denominator, which includes all students
	count if evr_suspend==1
		scalar susp = r(N)			// count of students suspended once in year
	count if evr_suspend_vi==1
		scalar suspvi = r(N)
	count if evr_suspend_nvi==1
		scalar suspnvi = r(N)
	scalar susp_p = susp/total
	scalar suspvi_p = suspvi/total
	scalar suspnvi_p = suspnvi/total
	scalar row = rownumb(out,"tot")
	matrix out[row	,col  ] = total
	matrix out[row+1,col  ] = susp
	matrix out[row+1,col+1] = susp_p
	matrix out[row+2,col  ] = suspvi
	matrix out[row+2,col+1] = suspvi_p
	matrix out[row+3,col  ] = suspnvi
	matrix out[row+3,col+1] = suspnvi_p

	* Race
	count if !missing(black) & !missing(white) & !missing(other)
		scalar race_tot = r(N)
	count if black==1 
		scalar race_black = r(N)
	count if white==1
		scalar race_white = r(N)
	count if other==1
		scalar race_other = r(N)
	scalar black_p = race_black/race_tot
	scalar white_p = race_white/race_tot
	scalar other_p = race_other/race_tot
	scalar row = rownumb(out,"race")
	matrix out[row  ,col  ] = race_tot
	matrix out[row+1,col  ] = race_black
	matrix out[row+1,col+1] = black_p
	matrix out[row+2,col  ] = race_white
	matrix out[row+2,col+1] = white_p
	matrix out[row+3,col  ] = race_other
	matrix out[row+3,col+1] = other_p

	* Poverty
	count if !missing(frpl)
		scalar pov_tot = r(N)
	count if freepl==1
		scalar pov_free = r(N)
	count if redpl==1
		scalar pov_red= r(N)
	count if fullpl==1
		scalar pov_full = r(N)
	count if always_dis_flg==1				// v02 addition
		scalar always = r(N)
	count if trans_dis_flg==1
		scalar sometimes = r(N)
	count if never_dis_flg==1		
		scalar notta = r(N)					// having issue making pct when using word 'never'. moving along.
	scalar free_p 	= pov_free/pov_tot
	scalar red_p 	= pov_red/pov_tot
	scalar full_p 	= pov_full/pov_tot
	scalar always_p = always/pov_tot		// v02 addition
	scalar notta_p 	= notta/pov_tot	
	scalar somet_p	= sometimes/pov_tot		
	scalar row = rownumb(out,"poverty")
	matrix out[row  ,col  ] = pov_tot
	matrix out[row+1,col  ] = pov_free
	matrix out[row+1,col+1] = free_p
	matrix out[row+2,col  ] = pov_red
	matrix out[row+2,col+1] = red_p
	matrix out[row+3,col  ] = pov_full
	matrix out[row+3,col+1] = full_p
	matrix out[row+4,col  ] = always		// v02 addition
	matrix out[row+4,col+1] = always_p
	matrix out[row+5,col  ] = sometimes
	matrix out[row+5,col+1] = somet_p
	matrix out[row+6,col  ] = notta
	matrix out[row+6,col+1] = notta_p	

	* Special Ed
	count if !missing(sped)
		scalar sped_tot = r(N)
	count if sped==1
		scalar spedn = r(N)
	count if notsped==1
		scalar notspedn= r(N)
	scalar sped_p = spedn/sped_tot
	scalar notsped_p = notspedn/sped_tot
	scalar row = rownumb(out,"spedstat")
	matrix out[row  ,col  ] = sped_tot
	matrix out[row+1,col  ] = spedn
	matrix out[row+1,col+1] = sped_p
	matrix out[row+2,col  ] = notspedn
	matrix out[row+2,col+1] = notsped_p

	* Gender
	count if !missing(male) & !missing(female)
		scalar gen_tot = r(N)
	count if male==1
		scalar malen = r(N)
	count if female==1
		scalar femalen= r(N)
	scalar male_p = malen/gen_tot
	scalar female_p = femalen/gen_tot
	scalar row = rownumb(out,"gender")
	matrix out[row  ,col  ] = gen_tot
	matrix out[row+1,col  ] = malen
	matrix out[row+1,col+1] = male_p
	matrix out[row+2,col  ] = femalen
	matrix out[row+2,col+1] = female_p

	* Achieve
	sum elal1
		scalar elan = r(N)
		scalar elam = r(mean)
	sum mathl1 
		scalar mathn = r(N)
		scalar mathm = r(mean)
	sum scil1
		scalar scin = r(N)
		scalar scim = r(mean)
	sum ssl1
		scalar ssn = r(N)
		scalar ssm = r(mean)
	scalar row = rownumb(out,"achieve")
	matrix out[row+1,col  ] = elan
	matrix out[row+1,col+1] = elam
	matrix out[row+2,col  ] = mathn
	matrix out[row+2,col+1] = mathm
	matrix out[row+3,col  ] = scin
	matrix out[row+3,col+1] = scim
	matrix out[row+4,col  ] = ssn
	matrix out[row+4,col+1] = ssm

//	3. White students
	restore
	preserve
	keep if white==1
	scalar col = 5

	* Suspension
	count if !missing(evr_suspend) 
		scalar total = r(N)			// this will be the denominator, which includes all students
	count if evr_suspend==1
		scalar susp = r(N)			// count of students suspended once in year
	count if evr_suspend_vi==1
		scalar suspvi = r(N)
	count if evr_suspend_nvi==1
		scalar suspnvi = r(N)
	scalar susp_p = susp/total
	scalar suspvi_p = suspvi/total
	scalar suspnvi_p = suspnvi/total
	scalar row = rownumb(out,"tot")
	matrix out[row	,col  ] = total
	matrix out[row+1,col  ] = susp
	matrix out[row+1,col+1] = susp_p
	matrix out[row+2,col  ] = suspvi
	matrix out[row+2,col+1] = suspvi_p
	matrix out[row+3,col  ] = suspnvi
	matrix out[row+3,col+1] = suspnvi_p

	* Race
	count if !missing(black) & !missing(white) & !missing(other)
		scalar race_tot = r(N)
	count if black==1 
		scalar race_black = r(N)
	count if white==1
		scalar race_white = r(N)
	count if other==1
		scalar race_other = r(N)
	scalar black_p = race_black/race_tot
	scalar white_p = race_white/race_tot
	scalar other_p = race_other/race_tot
	scalar row = rownumb(out,"race")
	matrix out[row  ,col  ] = race_tot
	matrix out[row+1,col  ] = race_black
	matrix out[row+1,col+1] = black_p
	matrix out[row+2,col  ] = race_white
	matrix out[row+2,col+1] = white_p
	matrix out[row+3,col  ] = race_other
	matrix out[row+3,col+1] = other_p

	* Poverty
	count if !missing(frpl)
		scalar pov_tot = r(N)
	count if freepl==1
		scalar pov_free = r(N)
	count if redpl==1
		scalar pov_red= r(N)
	count if fullpl==1
		scalar pov_full = r(N)
	count if always_dis_flg==1				// v02 addition
		scalar always = r(N)
	count if trans_dis_flg==1
		scalar sometimes = r(N)
	count if never_dis_flg==1		
		scalar notta = r(N)					// having issue making pct when using word 'never'. moving along.
	scalar free_p 	= pov_free/pov_tot
	scalar red_p 	= pov_red/pov_tot
	scalar full_p 	= pov_full/pov_tot
	scalar always_p = always/pov_tot		// v02 addition
	scalar notta_p 	= notta/pov_tot	
	scalar somet_p	= sometimes/pov_tot		
	scalar row = rownumb(out,"poverty")
	matrix out[row  ,col  ] = pov_tot
	matrix out[row+1,col  ] = pov_free
	matrix out[row+1,col+1] = free_p
	matrix out[row+2,col  ] = pov_red
	matrix out[row+2,col+1] = red_p
	matrix out[row+3,col  ] = pov_full
	matrix out[row+3,col+1] = full_p
	matrix out[row+4,col  ] = always		// v02 addition
	matrix out[row+4,col+1] = always_p
	matrix out[row+5,col  ] = sometimes
	matrix out[row+5,col+1] = somet_p
	matrix out[row+6,col  ] = notta
	matrix out[row+6,col+1] = notta_p	

	* Special Ed
	count if !missing(sped)
		scalar sped_tot = r(N)
	count if sped==1
		scalar spedn = r(N)
	count if notsped==1
		scalar notspedn= r(N)
	scalar sped_p = spedn/sped_tot
	scalar notsped_p = notspedn/sped_tot
	scalar row = rownumb(out,"spedstat")
	matrix out[row  ,col  ] = sped_tot
	matrix out[row+1,col  ] = spedn
	matrix out[row+1,col+1] = sped_p
	matrix out[row+2,col  ] = notspedn
	matrix out[row+2,col+1] = notsped_p

	* Gender
	count if !missing(male) & !missing(female)
		scalar gen_tot = r(N)
	count if male==1
		scalar malen = r(N)
	count if female==1
		scalar femalen= r(N)
	scalar male_p = malen/gen_tot
	scalar female_p = femalen/gen_tot
	scalar row = rownumb(out,"gender")
	matrix out[row  ,col  ] = gen_tot
	matrix out[row+1,col  ] = malen
	matrix out[row+1,col+1] = male_p
	matrix out[row+2,col  ] = femalen
	matrix out[row+2,col+1] = female_p

	* Achieve
	sum elal1
		scalar elan = r(N)
		scalar elam = r(mean)
	sum mathl1 
		scalar mathn = r(N)
		scalar mathm = r(mean)
	sum scil1
		scalar scin = r(N)
		scalar scim = r(mean)
	sum ssl1
		scalar ssn = r(N)
		scalar ssm = r(mean)
	scalar row = rownumb(out,"achieve")
	matrix out[row+1,col  ] = elan
	matrix out[row+1,col+1] = elam
	matrix out[row+2,col  ] = mathn
	matrix out[row+2,col+1] = mathm
	matrix out[row+3,col  ] = scin
	matrix out[row+3,col+1] = scim
	matrix out[row+4,col  ] = ssn
	matrix out[row+4,col+1] = ssm

//	4.	Always frpl students
	restore
	preserve
	keep if always_dis_flg==1
	scalar col = 7

	* Suspension
	count if !missing(evr_suspend) 
		scalar total = r(N)			// this will be the denominator, which includes all students
	count if evr_suspend==1
		scalar susp = r(N)			// count of students suspended once in year
	count if evr_suspend_vi==1
		scalar suspvi = r(N)
	count if evr_suspend_nvi==1
		scalar suspnvi = r(N)
	scalar susp_p = susp/total
	scalar suspvi_p = suspvi/total
	scalar suspnvi_p = suspnvi/total
	scalar row = rownumb(out,"tot")
	matrix out[row	,col  ] = total
	matrix out[row+1,col  ] = susp
	matrix out[row+1,col+1] = susp_p
	matrix out[row+2,col  ] = suspvi
	matrix out[row+2,col+1] = suspvi_p
	matrix out[row+3,col  ] = suspnvi
	matrix out[row+3,col+1] = suspnvi_p

	* Race
	count if !missing(black) & !missing(white) & !missing(other)
		scalar race_tot = r(N)
	count if black==1 
		scalar race_black = r(N)
	count if white==1
		scalar race_white = r(N)
	count if other==1
		scalar race_other = r(N)
	scalar black_p = race_black/race_tot
	scalar white_p = race_white/race_tot
	scalar other_p = race_other/race_tot
	scalar row = rownumb(out,"race")
	matrix out[row  ,col  ] = race_tot
	matrix out[row+1,col  ] = race_black
	matrix out[row+1,col+1] = black_p
	matrix out[row+2,col  ] = race_white
	matrix out[row+2,col+1] = white_p
	matrix out[row+3,col  ] = race_other
	matrix out[row+3,col+1] = other_p

	* Poverty
	count if !missing(frpl)
		scalar pov_tot = r(N)
	count if freepl==1
		scalar pov_free = r(N)
	count if redpl==1
		scalar pov_red= r(N)
	count if fullpl==1
		scalar pov_full = r(N)
	count if always_dis_flg==1				// v02 addition
		scalar always = r(N)
	count if trans_dis_flg==1
		scalar sometimes = r(N)
	count if never_dis_flg==1		
		scalar notta = r(N)					// having issue making pct when using word 'never'. moving along.
	scalar free_p 	= pov_free/pov_tot
	scalar red_p 	= pov_red/pov_tot
	scalar full_p 	= pov_full/pov_tot
	scalar always_p = always/pov_tot		// v02 addition
	scalar notta_p 	= notta/pov_tot	
	scalar somet_p	= sometimes/pov_tot		
	scalar row = rownumb(out,"poverty")
	matrix out[row  ,col  ] = pov_tot
	matrix out[row+1,col  ] = pov_free
	matrix out[row+1,col+1] = free_p
	matrix out[row+2,col  ] = pov_red
	matrix out[row+2,col+1] = red_p
	matrix out[row+3,col  ] = pov_full
	matrix out[row+3,col+1] = full_p
	matrix out[row+4,col  ] = always		// v02 addition
	matrix out[row+4,col+1] = always_p
	matrix out[row+5,col  ] = sometimes
	matrix out[row+5,col+1] = somet_p
	matrix out[row+6,col  ] = notta
	matrix out[row+6,col+1] = notta_p	

	* Special Ed
	count if !missing(sped)
		scalar sped_tot = r(N)
	count if sped==1
		scalar spedn = r(N)
	count if notsped==1
		scalar notspedn= r(N)
	scalar sped_p = spedn/sped_tot
	scalar notsped_p = notspedn/sped_tot
	scalar row = rownumb(out,"spedstat")
	matrix out[row  ,col  ] = sped_tot
	matrix out[row+1,col  ] = spedn
	matrix out[row+1,col+1] = sped_p
	matrix out[row+2,col  ] = notspedn
	matrix out[row+2,col+1] = notsped_p

	* Gender
	count if !missing(male) & !missing(female)
		scalar gen_tot = r(N)
	count if male==1
		scalar malen = r(N)
	count if female==1
		scalar femalen= r(N)
	scalar male_p = malen/gen_tot
	scalar female_p = femalen/gen_tot
	scalar row = rownumb(out,"gender")
	matrix out[row  ,col  ] = gen_tot
	matrix out[row+1,col  ] = malen
	matrix out[row+1,col+1] = male_p
	matrix out[row+2,col  ] = femalen
	matrix out[row+2,col+1] = female_p

	* Achieve
	sum elal1
		scalar elan = r(N)
		scalar elam = r(mean)
	sum mathl1 
		scalar mathn = r(N)
		scalar mathm = r(mean)
	sum scil1
		scalar scin = r(N)
		scalar scim = r(mean)
	sum ssl1
		scalar ssn = r(N)
		scalar ssm = r(mean)
	scalar row = rownumb(out,"achieve")
	matrix out[row+1,col  ] = elan
	matrix out[row+1,col+1] = elam
	matrix out[row+2,col  ] = mathn
	matrix out[row+2,col+1] = mathm
	matrix out[row+3,col  ] = scin
	matrix out[row+3,col+1] = scim
	matrix out[row+4,col  ] = ssn
	matrix out[row+4,col+1] = ssm

//	5. never frpl students
	restore
	keep if never_dis_flg==1
	scalar col = 9

	* Suspension
	count if !missing(evr_suspend) 
		scalar total = r(N)			// this will be the denominator, which includes all students
	count if evr_suspend==1
		scalar susp = r(N)			// count of students suspended once in year
	count if evr_suspend_vi==1
		scalar suspvi = r(N)
	count if evr_suspend_nvi==1
		scalar suspnvi = r(N)
	scalar susp_p = susp/total
	scalar suspvi_p = suspvi/total
	scalar suspnvi_p = suspnvi/total
	scalar row = rownumb(out,"tot")
	matrix out[row	,col  ] = total
	matrix out[row+1,col  ] = susp
	matrix out[row+1,col+1] = susp_p
	matrix out[row+2,col  ] = suspvi
	matrix out[row+2,col+1] = suspvi_p
	matrix out[row+3,col  ] = suspnvi
	matrix out[row+3,col+1] = suspnvi_p

	* Race
	count if !missing(black) & !missing(white) & !missing(other)
		scalar race_tot = r(N)
	count if black==1 
		scalar race_black = r(N)
	count if white==1
		scalar race_white = r(N)
	count if other==1
		scalar race_other = r(N)
	scalar black_p = race_black/race_tot
	scalar white_p = race_white/race_tot
	scalar other_p = race_other/race_tot
	scalar row = rownumb(out,"race")
	matrix out[row  ,col  ] = race_tot
	matrix out[row+1,col  ] = race_black
	matrix out[row+1,col+1] = black_p
	matrix out[row+2,col  ] = race_white
	matrix out[row+2,col+1] = white_p
	matrix out[row+3,col  ] = race_other
	matrix out[row+3,col+1] = other_p

	* Poverty
	count if !missing(frpl)
		scalar pov_tot = r(N)
	count if freepl==1
		scalar pov_free = r(N)
	count if redpl==1
		scalar pov_red= r(N)
	count if fullpl==1
		scalar pov_full = r(N)
	count if always_dis_flg==1				// v02 addition
		scalar always = r(N)
	count if trans_dis_flg==1
		scalar sometimes = r(N)
	count if never_dis_flg==1		
		scalar notta = r(N)					// having issue making pct when using word 'never'. moving along.
	scalar free_p 	= pov_free/pov_tot
	scalar red_p 	= pov_red/pov_tot
	scalar full_p 	= pov_full/pov_tot
	scalar always_p = always/pov_tot		// v02 addition
	scalar notta_p 	= notta/pov_tot	
	scalar somet_p	= sometimes/pov_tot		
	scalar row = rownumb(out,"poverty")
	matrix out[row  ,col  ] = pov_tot
	matrix out[row+1,col  ] = pov_free
	matrix out[row+1,col+1] = free_p
	matrix out[row+2,col  ] = pov_red
	matrix out[row+2,col+1] = red_p
	matrix out[row+3,col  ] = pov_full
	matrix out[row+3,col+1] = full_p
	matrix out[row+4,col  ] = always		// v02 addition
	matrix out[row+4,col+1] = always_p
	matrix out[row+5,col  ] = sometimes
	matrix out[row+5,col+1] = somet_p
	matrix out[row+6,col  ] = notta
	matrix out[row+6,col+1] = notta_p	

	* Special Ed
	count if !missing(sped)
		scalar sped_tot = r(N)
	count if sped==1
		scalar spedn = r(N)
	count if notsped==1
		scalar notspedn= r(N)
	scalar sped_p = spedn/sped_tot
	scalar notsped_p = notspedn/sped_tot
	scalar row = rownumb(out,"spedstat")
	matrix out[row  ,col  ] = sped_tot
	matrix out[row+1,col  ] = spedn
	matrix out[row+1,col+1] = sped_p
	matrix out[row+2,col  ] = notspedn
	matrix out[row+2,col+1] = notsped_p

	* Gender
	count if !missing(male) & !missing(female)
		scalar gen_tot = r(N)
	count if male==1
		scalar malen = r(N)
	count if female==1
		scalar femalen= r(N)
	scalar male_p = malen/gen_tot
	scalar female_p = femalen/gen_tot
	scalar row = rownumb(out,"gender")
	matrix out[row  ,col  ] = gen_tot
	matrix out[row+1,col  ] = malen
	matrix out[row+1,col+1] = male_p
	matrix out[row+2,col  ] = femalen
	matrix out[row+2,col+1] = female_p

	* Achieve
	sum elal1
		scalar elan = r(N)
		scalar elam = r(mean)
	sum mathl1 
		scalar mathn = r(N)
		scalar mathm = r(mean)
	sum scil1
		scalar scin = r(N)
		scalar scim = r(mean)
	sum ssl1
		scalar ssn = r(N)
		scalar ssm = r(mean)
	scalar row = rownumb(out,"achieve")
	matrix out[row+1,col  ] = elan
	matrix out[row+1,col+1] = elam
	matrix out[row+2,col  ] = mathn
	matrix out[row+2,col+1] = mathm
	matrix out[row+3,col  ] = scin
	matrix out[row+3,col+1] = scim
	matrix out[row+4,col  ] = ssn
	matrix out[row+4,col+1] = ssm


//================================
// 	#2D	Output results
//================================

	cd "$output"
	mat2txt, matrix(out) saving(table01-describe_sample) replace

//==============================================================================
// 3 - Create Table 2
//==============================================================================

//==================================================================
//	#3A	Data prep
//==================================================================

	use  "$data\sample01-disc_students-v02.dta", replace

* Variable cleaning for analysis
	clonevar sped = sped2
	la var sped "Student with Disabilities"
	gen nonsped = 1 if sped==0
		replace nonsped = 0 if sped==1
		replace nonsped = . if missing(sped)
	la var nonsped "Not Student with Disabilities"
	clonevar spedgt = sped3
	la var spedgt "Gifted or Talented"

* keep suspended only
	keep if suspend==1


//=====================================
//	#3B	Create output table (Table 2)
//=====================================

	matrix out = J(13,10,.)
	matrix rownames out = r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 totsusp viol nonviol
	matrix colnames out = alln allp blackn blackp whiten whitep alwaysn alwaysp nevern neverp


//=====================================
//	#3C	Populate table
//=====================================

//	1. All students
	scalar col = colnumb(out,"alln")
	forvalues i = 1(1)10 {
		count if disc_rank==`i'
		scalar row = rownumb(out,"r`i'")
		scalar r`i'n = r(N)
		matrix out[row,col] = r`i'n
	}
	scalar totsusp = r1n + r2n + r3n + r4n + r5n + r6n + r7n + r8n + r9n + r10n
	forvalues i = 1(1)10 {
		scalar row = rownumb(out,"r`i'")
		scalar r`i'p = r`i'n/totsusp
		matrix out[row,col+1] = r`i'p
	}

	count if viol_incd==1
	scalar vin = r(N)
	count if viol_incd==0
	scalar nvin = r(N)
	scalar  vip = vin/totsusp
	scalar nvip = nvin/totsusp
	scalar row = rownumb(out,"totsusp")
	matrix out[row  ,col  ] = totsusp
	matrix out[row+1,col  ] = vin
	matrix out[row+1,col+1] = vip
	matrix out[row+2,col  ] = nvin
	matrix out[row+2,col+1] = nvip

//	2. Black students
	preserve
	keep if black==1
	scalar col = colnumb(out,"blackn")
	forvalues i = 1(1)10 {
		count if disc_rank==`i'
		scalar row = rownumb(out,"r`i'")
		scalar r`i'n = r(N)
		matrix out[row,col] = r`i'n
	}
	scalar totsusp = r1n + r2n + r3n + r4n + r5n + r6n + r7n + r8n + r9n + r10n
	forvalues i = 1(1)10 {
		scalar row = rownumb(out,"r`i'")
		scalar r`i'p = r`i'n/totsusp
		matrix out[row,col+1] = r`i'p
	}

	count if viol_incd==1
	scalar vin = r(N)
	count if viol_incd==0
	scalar nvin = r(N)
	scalar  vip = vin/totsusp
	scalar nvip = nvin/totsusp
	scalar row = rownumb(out,"totsusp")
	matrix out[row  ,col  ] = totsusp
	matrix out[row+1,col  ] = vin
	matrix out[row+1,col+1] = vip
	matrix out[row+2,col  ] = nvin
	matrix out[row+2,col+1] = nvip

//	3. White students
	restore
	preserve
	keep if white==1
	scalar col = colnumb(out,"whiten")
	forvalues i = 1(1)10 {
		count if disc_rank==`i'
		scalar row = rownumb(out,"r`i'")
		scalar r`i'n = r(N)
		matrix out[row,col] = r`i'n
	}
	scalar totsusp = r1n + r2n + r3n + r4n + r5n + r6n + r7n + r8n + r9n + r10n
	forvalues i = 1(1)10 {
		scalar row = rownumb(out,"r`i'")
		scalar r`i'p = r`i'n/totsusp
		matrix out[row,col+1] = r`i'p
	}
	count if viol_incd==1
	scalar vin = r(N)
	count if viol_incd==0
	scalar nvin = r(N)
	scalar  vip = vin/totsusp
	scalar nvip = nvin/totsusp
	scalar row = rownumb(out,"totsusp")
	matrix out[row  ,col  ] = totsusp
	matrix out[row+1,col  ] = vin
	matrix out[row+1,col+1] = vip
	matrix out[row+2,col  ] = nvin
	matrix out[row+2,col+1] = nvip

//	4. Always FRPL students
	restore
	preserve
	keep if always_dis_flg==1
	scalar col = colnumb(out,"alwaysn")
	forvalues i = 1(1)10 {
		count if disc_rank==`i'
		scalar row = rownumb(out,"r`i'")
		scalar r`i'n = r(N)
		matrix out[row,col] = r`i'n
	}
	scalar totsusp = r1n + r2n + r3n + r4n + r5n + r6n + r7n + r8n + r9n + r10n
	forvalues i = 1(1)10 {
		scalar row = rownumb(out,"r`i'")
		scalar r`i'p = r`i'n/totsusp
		matrix out[row,col+1] = r`i'p
	}

	count if viol_incd==1
	scalar vin = r(N)
	count if viol_incd==0
	scalar nvin = r(N)
	scalar  vip = vin/totsusp
	scalar nvip = nvin/totsusp
	scalar row = rownumb(out,"totsusp")
	matrix out[row  ,col  ] = totsusp
	matrix out[row+1,col  ] = vin
	matrix out[row+1,col+1] = vip
	matrix out[row+2,col  ] = nvin
	matrix out[row+2,col+1] = nvip

//	5. Never FRPL studentss
	restore
	preserve
	keep if never_dis_flg==1
	scalar col = colnumb(out,"nevern")
	forvalues i = 1(1)10 {
		count if disc_rank==`i'
		scalar row = rownumb(out,"r`i'")
		scalar r`i'n = r(N)
		matrix out[row,col] = r`i'n
	}
	scalar totsusp = r1n + r2n + r3n + r4n + r5n + r6n + r7n + r8n + r9n + r10n
	forvalues i = 1(1)10 {
		scalar row = rownumb(out,"r`i'")
		scalar r`i'p = r`i'n/totsusp
		matrix out[row,col+1] = r`i'p
	}

	count if viol_incd==1
	scalar vin = r(N)
	count if viol_incd==0
	scalar nvin = r(N)
	scalar  vip = vin/totsusp
	scalar nvip = nvin/totsusp
	scalar row = rownumb(out,"totsusp")
	matrix out[row  ,col  ] = totsusp
	matrix out[row+1,col  ] = vin
	matrix out[row+1,col+1] = vip
	matrix out[row+2,col  ] = nvin
	matrix out[row+2,col+1] = nvip

	
//=====================================
//	#3D	Output results
//=====================================

	cd "$output"
	mat2txt, matrix(out) saving(table02-infraction_types) replace

//==============================================================================
// 4 - Create Table 3
//==============================================================================

//===========================================
//	#4A	Initial data work
//===========================================

	use  "$data\sample01-disc_students-v02.dta", replace
	
* Censoring days suspended to 20
	clonevar susp_days = susp_days_est
		replace susp_days = 20 if susp_days_est>=20 & !missing(susp_days_est)

* Randomly keeping one record if duplicate on stud_id disc_dt_st disc_dt_end
	duplicates tag stud_id disc_dt_st disc_dt_end, gen(xdupe)
	gen double xrando = uniform() if xdupe>0
	bysort stud_id disc_dt_start: egen double xmin = min(xrando)
	keep if xrando==xmin
	gen flag_discdupe = 1 if !missing(xrando)
	la var flag_discdupe "flag: randomly retained dupe on stud_id disc_dt_st disc_dt_end"
	drop x*

* Regression globals
	cd "$output"

	global keepreg	"!missing(frpl)"
	global xrace	black other
	global xfrpl	always_dis_flg trans_dis_flg
	global xachieve	math_lag1 ela_lag1
	global disc		i.disc_rank
		
	
//============================
//	#4B	Regressions
//============================

* 1. Race only
	reg susp_days $xrace $disc i.year i.grade if $keepreg, cluster(sgy)
	outreg2 using table03-predict_susp_days.xml ///
		, 	dec(3) replace label addtext(Disc. Controls, Yes, Year FE, Yes, Grade FE, Yes, School-Grade-Year FE, No) ///
			title("Table 3. Predictors of Length of Suspension (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr)
	
	areg susp_days $xrace $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using table03-predict_susp_days.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table 3. Predictors of Length of Suspension (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
* 2. FRPL only
	reg susp_days $xfrpl $disc i.year i.grade if $keepreg, cluster(sgy)
	outreg2 using table03-predict_susp_days.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, Yes, Grade FE, Yes, School-Grade-Year FE, No) ///
			title("Table 3. Predictors of Length of Suspension (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
	areg susp_days $xfrpl $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using table03-predict_susp_days.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table 3. Predictors of Length of Suspension (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
* 3. Race and frpl
	reg susp_days $xrace $xfrpl $disc i.year i.grade if $keepreg, cluster(sgy)
	outreg2 using table03-predict_susp_days.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, Yes, Grade FE, Yes, School-Grade-Year FE, No) ///
			title("Table 3. Predictors of Length of Suspension (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
	areg susp_days $xrace $xfrpl $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using table03-predict_susp_days.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table 3. Predictors of Length of Suspension (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 		
	
	areg susp_days $xrace $xfrpl $disc susp_prioryr if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using table03-predict_susp_days.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table 3. Predictors of Length of Suspension (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 	
			
* 4. Combined model		
	reg susp_days $xrace $xfrpl sped2 male $xachieve $disc i.year i.grade if $keepreg, cluster(sgy)
	outreg2 using table03-predict_susp_days.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, Yes, Grade FE, Yes, School-Grade-Year FE, No) ///
			title("Table 3. Predictors of Length of Suspension (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
	areg susp_days $xrace $xfrpl sped2 male $xachieve $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using table03-predict_susp_days.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table 3. Predictors of Length of Suspension (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 

	areg susp_days $xrace $xfrpl sped2 male $xachieve susp_prioryr $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using table03-predict_susp_days.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table 3. Predictors of Length of Suspension (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 

//==============================================================================
// 5 - Create Table 4
//==============================================================================
//===========================================
//	#5A	Initial data work
//===========================================

	use  "$data\sample01-disc_students-v02.dta", replace

* Censoring days suspended to 20
	clonevar susp_days = susp_days_est
		replace susp_days = 20 if susp_days_est>=20 & !missing(susp_days_est)

* Keep first record for discipline students (unit of analysis is infraction for
* 	disciplined students in sample01-all_students.dta)
	keep if missing(disc_cd) | disc_dt_start==first_off_date
	duplicates tag stud_id year, gen(xdupe)

* Randomly keeping one observation for remaining dupes
	gen double xrand = runiform() if xdupe>0 & !missing(xdupe)
	bysort stud_id year: egen double xmin = min(xrand)
	keep if xdupe==0 | xrand==xmin

* Regression globals
	cd "$output"

	global keepreg	"!missing(frpl)"
	global xrace	black other
	global xfrpl	always_dis_flg trans_dis_flg
	global xachieve	math_lag1 ela_lag1
	global disc		i.disc_rank
		

//============================
//	#5B	Regressions
//============================
	
* 1. Race only
	reg susp_days $xrace $disc i.year i.grade if $keepreg, cluster(sgy)
	outreg2 using table04-predict_susp_days-1stoffs.xml ///
		, 	dec(3) replace label addtext(Disc. Controls, Yes, Year FE, Yes, Grade FE, Yes, School-Grade-Year FE, No) ///
			title("Table 4. Predictors of Length of Suspension for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr)
	
	areg susp_days $xrace $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using table04-predict_susp_days-1stoffs.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table 4. Predictors of Length of Suspension for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
* 2. FRPL only
	reg susp_days $xfrpl $disc i.year i.grade if $keepreg, cluster(sgy)
	outreg2 using table04-predict_susp_days-1stoffs.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, Yes, Grade FE, Yes, School-Grade-Year FE, No) ///
			title("Table 4. Predictors of Length of Suspension for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
	areg susp_days $xfrpl $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using table04-predict_susp_days-1stoffs.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table 4. Predictors of Length of Suspension for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
* 3. Race and frpl
	reg susp_days $xrace $xfrpl $disc i.year i.grade if $keepreg, cluster(sgy)
	outreg2 using table04-predict_susp_days-1stoffs.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, Yes, Grade FE, Yes, School-Grade-Year FE, No) ///
			title("Table 4. Predictors of Length of Suspension for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
	areg susp_days $xrace $xfrpl $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using table04-predict_susp_days-1stoffs.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table 4. Predictors of Length of Suspension for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 		
	
	areg susp_days $xrace $xfrpl $disc susp_prioryr if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using table04-predict_susp_days-1stoffs.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table 4. Predictors of Length of Suspension for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 	
			
* 4. Combined model		
	reg susp_days $xrace $xfrpl sped2 male $xachieve $disc i.year i.grade if $keepreg, cluster(sgy)
	outreg2 using table04-predict_susp_days-1stoffs.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, Yes, Grade FE, Yes, School-Grade-Year FE, No) ///
			title("Table 4. Predictors of Length of Suspension for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
	areg susp_days $xrace $xfrpl sped2 male $xachieve $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using table04-predict_susp_days-1stoffs.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table 4. Predictors of Length of Suspension for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 

	areg susp_days $xrace $xfrpl sped2 male $xachieve susp_prioryr $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using table04-predict_susp_days-1stoffs.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table 4. Predictors of Length of Suspension for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
	
//==============================================================================
// 6 - Create Tables 5 & 6
//==============================================================================
//===========================================
//	#6A Initial data work
//===========================================
	
	use "$data\fights.dta", replace
	cd "$output"
	
	*set globals
	global base_race	black fight_count
	global base_frpl	always_dis_flg fight_count
	global vars_race	always_dis_flg never_dis_flg sped2 stud_male math_lag1 ela_lag1 math_miss ela_miss sped_miss 
	global vars_frpl	black other_race sped2 stud_male math_lag1 ela_lag1 math_miss ela_miss sped_miss 
	global if_race		if same_fight==2 & blk_wht==1
	global if_frpl		if same_fight==2 & frp_non==1
	global outreg1a		outreg using table5_panelA, se varlabels starloc(1) nole starlevels(10 5 1) t("Table 5 Panel A: Discipline Disparities in Fights Between Two Students") keep(black _cons) basefont(roman fs12)
	global outreg1b		outreg using table5_panelB, se varlabels starloc(1) nole starlevels(10 5 1) t("Table 5 Panel B: Discipline Disparities in Fights Between Two Students") keep(always_dis_flg _cons) basefont(roman fs12)
	global outreg1c		outreg using table6_panelA, se varlabels starloc(1) nole starlevels(10 5 1) t("Table 6 Panel A: Discipline Disparities in Fights Between Two Students") keep(black _cons) basefont(roman fs12)
	global outreg1d		outreg using table6_panelB, se varlabels starloc(1) nole starlevels(10 5 1) t("Table 6 Panel B: Discipline Disparities in Fights Between Two Students") keep(always_dis_flg _cons) basefont(roman fs12)
	global outreg1e		note("Note: * p<0.1; ** p<0.05; *** p<0.01") summstat(N \ r2)

//===========================================
//	#6B Table 5 Panel A
//===========================================

	*full sample
	xtreg days $base_race $if_race, fe i(group) cluster(group)
			$outreg1a ct("", "(1)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e replace
			
	xtreg days $base_race $vars_race $if_race, fe i(group) cluster(group)
			$outreg1a ct("", "(2)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge
	
	*first fight of the year
	xtreg days $base_race $if_race & prior_fights==0, fe i(group) cluster(group)
			$outreg1a ct("", "(3)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e merge

	xtreg days $base_race $vars_race $if_race & prior_fights==0, fe i(group) cluster(group)
			$outreg1a ct("", "(4)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge

	*first fight ever
	xtreg days $base_race number_years $if_race & no_prior==1, fe i(group) cluster(group)
			$outreg1a ct("", "(5)") addrows("Student controls", "No" \ "# of years in data control", "Yes") $outreg1e merge

	xtreg days $base_race number_years $vars_race $if_race & no_prior==1, fe i(group) cluster(group)
			$outreg1a ct("", "(6)") addrows("Student controls", "Yes" \ "# of years in data control", "Yes") $outreg1e merge

	*first suspension of the year is a fight
	xtreg days $base_race $if_race & use==1, fe i(group) cluster(group)
			$outreg1a ct("", "(7)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e merge

	xtreg days $base_race $vars_race $if_race & use==1, fe i(group) cluster(group)
			$outreg1a ct("", "(8)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge

	*first suspension ever is a fight
	xtreg days $base_race number_years $if_race & use2==1, fe i(group) cluster(group)
			$outreg1a ct("", "(9)") addrows("Student controls", "No" \ "# of years in data control", "Yes") $outreg1e merge

	xtreg days $base_race number_years $vars_race $if_race & use2==1, fe i(group) cluster(group)
			$outreg1a ct("", "(10)") addrows("Student controls", "Yes" \ "# of years in data control", "Yes") $outreg1e merge
	
//===========================================
//	#6C Table 5 Panel B
//===========================================

	*full sample
	xtreg days $base_frpl $if_frpl, fe i(group) cluster(group)
			$outreg1b ct("", "(1)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e replace
			
	xtreg days $base_frpl $vars_frpl $if_frpl, fe i(group) cluster(group)
			$outreg1b ct("", "(2)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge
	
	*first fight of the year
	xtreg days $base_frpl $if_frpl & prior_fights==0, fe i(group) cluster(group)
			$outreg1b ct("", "(3)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e merge

	xtreg days $base_frpl $vars_frpl $if_frpl & prior_fights==0, fe i(group) cluster(group)
			$outreg1b ct("", "(4)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge

	*first fight ever
	xtreg days $base_frpl number_years $if_frpl & no_prior==1, fe i(group) cluster(group)
			$outreg1b ct("", "(5)") addrows("Student controls", "No" \ "# of years in data control", "Yes") $outreg1e merge

	xtreg days $base_frpl number_years $vars_frpl $if_frpl & no_prior==1, fe i(group) cluster(group)
			$outreg1b ct("", "(6)") addrows("Student controls", "Yes" \ "# of years in data control", "Yes") $outreg1e merge

	*first suspension of the year is a fight
	xtreg days $base_frpl $if_frpl & use==1, fe i(group) cluster(group)
			$outreg1b ct("", "(7)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e merge

	xtreg days $base_frpl $vars_frpl $if_frpl & use==1, fe i(group) cluster(group)
			$outreg1b ct("", "(8)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge

	*first suspension ever is a fight
	xtreg days $base_frpl number_years $if_frpl & use2==1, fe i(group) cluster(group)
			$outreg1b ct("", "(9)") addrows("Student controls", "No" \ "# of years in data control", "Yes") $outreg1e merge

	xtreg days $base_frpl number_years $vars_frpl $if_frpl & use2==1, fe i(group) cluster(group)
			$outreg1b ct("", "(10)") addrows("Student controls", "Yes" \ "# of years in data control", "Yes") $outreg1e merge
	
//===========================================
//	#6D Table 6 Panel A
//===========================================

	*full sample
	xtreg more_days $base_race $if_race, fe i(group) cluster(group)
			$outreg1c ct("", "(1)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e replace
			
	xtreg more_days $base_race $vars_race $if_race, fe i(group) cluster(group)
			$outreg1c ct("", "(2)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge
	
	*first fight of the year
	xtreg more_days $base_race $if_race & prior_fights==0, fe i(group) cluster(group)
			$outreg1c ct("", "(3)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e merge

	xtreg more_days $base_race $vars_race $if_race & prior_fights==0, fe i(group) cluster(group)
			$outreg1c ct("", "(4)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge

	*first fight ever
	xtreg more_days $base_race number_years $if_race & no_prior==1, fe i(group) cluster(group)
			$outreg1c ct("", "(5)") addrows("Student controls", "No" \ "# of years in data control", "Yes") $outreg1e merge

	xtreg more_days $base_race number_years $vars_race $if_race & no_prior==1, fe i(group) cluster(group)
			$outreg1c ct("", "(6)") addrows("Student controls", "Yes" \ "# of years in data control", "Yes") $outreg1e merge

	*first suspension of the year is a fight
	xtreg more_days $base_race $if_race & use==1, fe i(group) cluster(group)
			$outreg1c ct("", "(7)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e merge

	xtreg more_days $base_race $vars_race $if_race & use==1, fe i(group) cluster(group)
			$outreg1c ct("", "(8)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge

	*first suspension ever is a fight
	xtreg more_days $base_race number_years $if_race & use2==1, fe i(group) cluster(group)
			$outreg1c ct("", "(9)") addrows("Student controls", "No" \ "# of years in data control", "Yes") $outreg1e merge

	xtreg more_days $base_race number_years $vars_race $if_race & use2==1, fe i(group) cluster(group)
			$outreg1c ct("", "(10)") addrows("Student controls", "Yes" \ "# of years in data control", "Yes") $outreg1e merge
	
//===========================================
//	#6E Table 6 Panel B
//===========================================

	*full sample
	xtreg more_days $base_frpl $if_frpl, fe i(group) cluster(group)
			$outreg1d ct("", "(1)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e replace
			
	xtreg more_days $base_frpl $vars_frpl $if_frpl, fe i(group) cluster(group)
			$outreg1d ct("", "(2)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge
	
	*first fight of the year
	xtreg more_days $base_frpl $if_frpl & prior_fights==0, fe i(group) cluster(group)
			$outreg1d ct("", "(3)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e merge

	xtreg more_days $base_frpl $vars_frpl $if_frpl & prior_fights==0, fe i(group) cluster(group)
			$outreg1d ct("", "(4)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge

	*first fight ever
	xtreg more_days $base_frpl number_years $if_frpl & no_prior==1, fe i(group) cluster(group)
			$outreg1d ct("", "(5)") addrows("Student controls", "No" \ "# of years in data control", "Yes") $outreg1e merge

	xtreg more_days $base_frpl number_years $vars_frpl $if_frpl & no_prior==1, fe i(group) cluster(group)
			$outreg1d ct("", "(6)") addrows("Student controls", "Yes" \ "# of years in data control", "Yes") $outreg1e merge

	*first suspension of the year is a fight
	xtreg more_days $base_frpl $if_frpl & use==1, fe i(group) cluster(group)
			$outreg1d ct("", "(7)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e merge

	xtreg more_days $base_frpl $vars_frpl $if_frpl & use==1, fe i(group) cluster(group)
			$outreg1d ct("", "(8)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge

	*first suspension ever is a fight
	xtreg more_days $base_frpl number_years $if_frpl & use2==1, fe i(group) cluster(group)
			$outreg1d ct("", "(9)") addrows("Student controls", "No" \ "# of years in data control", "Yes") $outreg1e merge

	xtreg more_days $base_frpl number_years $vars_frpl $if_frpl & use2==1, fe i(group) cluster(group)
			$outreg1d ct("", "(10)") addrows("Student controls", "Yes" \ "# of years in data control", "Yes") $outreg1e merge

	
//==============================================================================
// 7 - Create Table 7 & Figure 7
//==============================================================================
//===========================================
//	#7A Initial data work
//===========================================
	
	use "$data\fights.dta", replace
	cd "$output"
	
***BLACK and WHITE by groups***
	*black white
	gen     totalb_w=0
	replace totalb_w=1 if stud_one_eth==1 & stud_two_eth==2
	replace totalb_w=1 if stud_one_eth==2 & stud_two_eth==1

	gen     sameb_w=0
	replace sameb_w=1 if totalb_w==1 & stud_one_days==stud_two_days

	gen     student1_moreb_w=0
	replace student1_moreb_w=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days
	replace student1_moreb_w=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days
	gen     student2_moreb_w=0
	replace student2_moreb_w=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days
	replace student2_moreb_w=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days

	*black black*
	gen     totalb_b=0
	replace totalb_b=1 if stud_one_eth==1 & stud_two_eth==1

	gen     sameb_b=0
	replace sameb_b = 1 if totalb_b==1 & stud_one_days==stud_two_days

	*white white*
	gen     totalw_w=0
	replace totalw_w=1 if stud_one_eth==2 & stud_two_eth==2

	gen     samew_w=0
	replace samew_w = 1 if totalw_w==1 & stud_one_days==stud_two_days

	*FRL Non FRL*
	gen     totalp_np = 0
	replace totalp_np=1 if stud_one_frpl==1 & stud_two_non==1
	replace totalp_np=1 if stud_one_non==1 & stud_two_frpl==1

	gen     samep_np=0
	replace samep_np=1 if totalp_np==1 & stud_one_days==stud_two_days

	gen     student1_morep_np=0
	replace student1_morep_np=1 if stud_one_frpl==1 & stud_two_non==1 & stud_one_days>stud_two_days
	replace student1_morep_np=1 if stud_one_non==1 & stud_two_frpl==1 & stud_one_days<stud_two_days

	gen     student2_morep_np=0
	replace student2_morep_np=1 if stud_one_frpl==1 & stud_two_non==1 & stud_one_days<stud_two_days
	replace student2_morep_np=1 if stud_one_non==1 & stud_two_frpl==1 & stud_one_days>stud_two_days

	*poor poor*
	gen     totalp_p=0
	replace totalp_p=1 if stud_one_frpl==1 & stud_two_frpl==1

	gen     samep_p=0
	replace samep_p = 1 if totalp_p==1 & stud_one_days==stud_two_days

	*nonpoor nonpoor*
	gen     totalnp_np=0
	replace totalnp_np=1 if stud_one_non==1 & stud_two_non==1

	gen     samenp_np=0
	replace samenp_np = 1 if totalnp_np==1 & stud_one_days==stud_two_days

	*male female*
	gen     totalm_f = 0
	replace totalm_f = 1 if stud_one_male!=stud_two_male

	gen     samem_f = 0
	replace samem_f = 1 if totalm_f==1 & stud_one_days==stud_two_days

	gen     student1_morem_f = 0
	replace student1_morem_f = 1 if totalm_f==1 & stud_one_male==1 & stud_two_male==0 & stud_one_days>stud_two_days
	replace student1_morem_f = 1 if totalm_f==1 & stud_one_male==0 & stud_two_male==1 & stud_one_days<stud_two_days

	gen     student2_morem_f = 0
	replace student2_morem_f = 1 if totalm_f==1 & stud_one_male==1 & stud_two_male==0 & stud_one_days<stud_two_days
	replace student2_morem_f = 1 if totalm_f==1 & stud_one_male==0 & stud_two_male==1 & stud_one_days>stud_two_days

	*male male*
	gen     totalm_m=0
	replace totalm_m=1 if stud_one_male==1 & stud_two_male==1

	gen     samem_m=0
	replace samem_m= 1 if totalm_m==1 & stud_one_days==stud_two_days

	*female female*
	gen     totalf_f=0
	replace totalf_f=1 if stud_one_male==0 & stud_two_male==0

	gen     samef_f=0
	replace samef_f = 1 if totalf_f==1 & stud_one_days==stud_two_days

	*high score low score*
	gen     totall_h = 0
	replace totall_h=1 if stud_one_high==1 & stud_two_low==1
	replace totall_h=1 if stud_one_low==1 & stud_two_high==1

	gen     samel_h=0
	replace samel_h=1 if totall_h==1 & stud_one_days==stud_two_days

	gen     student1_morel_h=0
	replace student1_morel_h=1 if stud_one_high==1 & stud_two_low==1 & stud_one_days<stud_two_days
	replace student1_morel_h=1 if stud_one_low==1 & stud_two_high==1 & stud_one_days>stud_two_days

	gen     student2_morel_h=0
	replace student2_morel_h=1 if stud_one_high==1 & stud_two_low==1 & stud_one_days>stud_two_days
	replace student2_morel_h=1 if stud_one_low==1 & stud_two_high==1 & stud_one_days<stud_two_days

	*low low*
	gen     totall_l=0
	replace totall_l=1 if stud_one_low==1 & stud_two_low==1
	gen     samel_l=0
	replace samel_l = 1 if totall_l==1 & stud_one_days==stud_two_days

	*high high*
	gen     totalh_h=0
	replace totalh_h=1 if stud_one_high==1 & stud_two_high==1
	gen     sameh_h=0
	replace sameh_h = 1 if totalh_h==1 & stud_one_days==stud_two_days

	*black poor white poor*
	gen     totalbp_wp = 0
	replace totalbp_wp = 1 if stud_one_eth==1 & stud_one_frpl==1 & stud_two_eth==2 & stud_two_frpl==1
	replace totalbp_wp = 1 if stud_one_eth==2 & stud_one_frpl==1 & stud_two_eth==1 & stud_two_frpl==1

	gen     samebp_wp = 0 if totalbp_wp==1
	replace samebp_wp = 1 if totalbp_wp==1 & stud_one_days==stud_two_days

	gen     student1_morebp_wp = 0 if totalbp_wp==1
	replace student1_morebp_wp = 1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbp_wp==1
	replace student1_morebp_wp = 1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbp_wp==1

	gen     student2_morebp_wp = 0 if totalbp_wp==1
	replace student2_morebp_wp = 1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbp_wp==1
	replace student2_morebp_wp = 1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbp_wp==1

	*black nonpoor white nonpoor*
	gen     totalbnp_wnp = 0
	replace totalbnp_wnp = 1 if stud_one_eth==1 & stud_one_non==1 & stud_two_eth==2 & stud_two_non==1
	replace totalbnp_wnp = 1 if stud_one_eth==2 & stud_one_non==1 & stud_two_eth==1 & stud_two_non==1

	gen     samebnp_wnp = 0 if totalbnp_wnp==1
	replace samebnp_wnp = 1 if totalbnp_wnp==1 & stud_one_days==stud_two_days

	gen     student1_morebnp_wnp = 0 if totalbnp_wnp==1
	replace student1_morebnp_wnp = 1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbnp_wnp==1
	replace student1_morebnp_wnp = 1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbnp_wnp==1

	gen     student2_morebnp_wnp = 0 if totalbnp_wnp==1
	replace student2_morebnp_wnp = 1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbnp_wnp==1
	replace student2_morebnp_wnp = 1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbnp_wnp==1

	*black poor white nonpoor*
	gen     totalbp_wnp = 0
	replace totalbp_wnp = 1 if stud_one_eth==1 & stud_one_frpl==1 & stud_two_eth==2 & stud_two_non==1
	replace totalbp_wnp = 1 if stud_one_eth==2 & stud_one_non==1 & stud_two_eth==1 & stud_two_frpl==1

	gen     samebp_wnp = 0 if totalbp_wnp==1
	replace samebp_wnp = 1 if totalbp_wnp==1 & stud_one_days==stud_two_days

	gen     student1_morebp_wnp = 0 if totalbp_wnp==1
	replace student1_morebp_wnp = 1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbp_wnp==1
	replace student1_morebp_wnp = 1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbp_wnp==1

	gen     student2_morebp_wnp = 0 if totalbp_wnp==1
	replace student2_morebp_wnp = 1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbp_wnp==1
	replace student2_morebp_wnp = 1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbp_wnp==1

	*black nonpoor white poor*
	gen     totalbnp_wp = 0
	replace totalbnp_wp = 1 if stud_one_eth==1 & stud_one_non==1 & stud_two_eth==2 & stud_two_frpl==1
	replace totalbnp_wp = 1 if stud_one_eth==2 & stud_one_frpl==1 & stud_two_eth==1 & stud_two_non==1

	gen     samebnp_wp = 0 if totalbnp_wp==1
	replace samebnp_wp = 1 if totalbnp_wp==1 & stud_one_days==stud_two_days

	gen     student1_morebnp_wp = 0 if totalbnp_wp==1
	replace student1_morebnp_wp = 1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbnp_wp==1
	replace student1_morebnp_wp = 1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbnp_wp==1

	gen     student2_morebnp_wp = 0 if totalbnp_wp==1
	replace student2_morebnp_wp = 1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbnp_wp==1
	replace student2_morebnp_wp = 1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbnp_wp==1

	*black male white male*
	gen     totalbm_wm=0
	replace totalbm_wm=1 if stud_one_eth==1 & stud_one_male==1 & stud_two_eth==2 & stud_two_male==1
	replace totalbm_wm=1 if stud_one_eth==2 & stud_one_male==1 & stud_two_eth==1 & stud_two_male==1

	gen     samebm_wm=0 if totalbm_wm==1
	replace samebm_wm=1 if totalbm_wm==1 & stud_one_days==stud_two_days

	gen     student1_morebm_wm=0 if totalbm_wm==1
	replace student1_morebm_wm=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbm_wm==1
	replace student1_morebm_wm=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbm_wm==1

	gen     student2_morebm_wm=0 if totalbm_wm==1
	replace student2_morebm_wm=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbm_wm==1
	replace student2_morebm_wm=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbm_wm==1

	*black female white female*
	gen     totalbf_wf=0
	replace totalbf_wf=1 if stud_one_eth==1 & stud_one_male==0 & stud_two_eth==2 & stud_two_male==0
	replace totalbf_wf=1 if stud_one_eth==2 & stud_one_male==0 & stud_two_eth==1 & stud_two_male==0

	gen     samebf_wf=0 if totalbf_wf==1
	replace samebf_wf=1 if totalbf_wf==1 & stud_one_days==stud_two_days

	gen     student1_morebf_wf=0 if totalbf_wf==1
	replace student1_morebf_wf=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbf_wf==1
	replace student1_morebf_wf=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbf_wf==1

	gen     student2_morebf_wf=0 if totalbf_wf==1
	replace student2_morebf_wf=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbf_wf==1
	replace student2_morebf_wf=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbf_wf==1

	*black male/white female
	gen     totalbm_wf=0
	replace totalbm_wf=1 if stud_one_eth==1 & stud_one_male==1 & stud_two_eth==2 & stud_two_male==0
	replace totalbm_wf=1 if stud_one_eth==2 & stud_one_male==0 & stud_two_eth==1 & stud_two_male==1

	gen     samebm_wf=0 if totalbm_wf==1
	replace samebm_wf=1 if totalbm_wf==1 & stud_one_days==stud_two_days

	gen     student1_morebm_wf=0 if totalbm_wf==1
	replace student1_morebm_wf=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbm_wf==1
	replace student1_morebm_wf=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbm_wf==1

	gen     student2_morebm_wf=0 if totalbm_wf==1
	replace student2_morebm_wf=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbm_wf==1
	replace student2_morebm_wf=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbm_wf==1

	*black girl/white boy
	gen     totalbf_wm=0
	replace totalbf_wm=1 if stud_one_eth==1 & stud_one_male==0 & stud_two_eth==2 & stud_two_male==1
	replace totalbf_wm=1 if stud_one_eth==2 & stud_one_male==1 & stud_two_eth==1 & stud_two_male==0

	gen     samebf_wm=0 if totalbf_wm==1
	replace samebf_wm=1 if totalbf_wm==1 & stud_one_days==stud_two_days

	gen     student1_morebf_wm=0 if totalbf_wm==1
	replace student1_morebf_wm=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbf_wm==1
	replace student1_morebf_wm=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbf_wm==1

	gen     student2_morebf_wm=0 if totalbf_wm==1
	replace student2_morebf_wm=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbf_wm==1
	replace student2_morebf_wm=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbf_wm==1

	*black high white high
	gen     totalbh_wh=0
	replace totalbh_wh=1 if stud_one_eth==1 & stud_one_high==1 & stud_two_eth==2 & stud_two_high==1
	replace totalbh_wh=1 if stud_one_eth==2 & stud_one_high==1 & stud_two_eth==1 & stud_two_high==1

	gen     samebh_wh=0 if totalbh_wh==1
	replace samebh_wh=1 if totalbh_wh==1 & stud_one_days==stud_two_days

	gen     student1_morebh_wh=0 if totalbh_wh==1
	replace student1_morebh_wh=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbh_wh==1
	replace student1_morebh_wh=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbh_wh==1

	gen     student2_morebh_wh=0 if totalbh_wh==1
	replace student2_morebh_wh=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbh_wh==1
	replace student2_morebh_wh=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbh_wh==1

	*black low white low
	gen     totalbl_wl=0
	replace totalbl_wl=1 if stud_one_eth==1 & stud_one_low==1 & stud_two_eth==2 & stud_two_low==1
	replace totalbl_wl=1 if stud_one_eth==2 & stud_one_low==1 & stud_two_eth==1 & stud_two_low==1

	gen     samebl_wl=0 if totalbl_wl==1
	replace samebl_wl=1 if totalbl_wl==1 & stud_one_days==stud_two_days

	gen     student1_morebl_wl=0 if totalbl_wl==1
	replace student1_morebl_wl=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbl_wl==1
	replace student1_morebl_wl=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbl_wl==1

	gen     student2_morebl_wl=0 if totalbl_wl==1
	replace student2_morebl_wl=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbl_wl==1
	replace student2_morebl_wl=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbl_wl==1

	*black high white low
	gen     totalbh_wl=0
	replace totalbh_wl=1 if stud_one_eth==1 & stud_one_high==1 & stud_two_eth==2 & stud_two_low==1
	replace totalbh_wl=1 if stud_one_eth==2 & stud_one_low==1 & stud_two_eth==1 & stud_two_high==1

	gen     samebh_wl=0 if totalbh_wl==1
	replace samebh_wl=1 if totalbh_wl==1 & stud_one_days==stud_two_days

	gen     student1_morebh_wl=0 if totalbh_wl==1
	replace student1_morebh_wl=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbh_wl==1
	replace student1_morebh_wl=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbh_wl==1

	gen     student2_morebh_wl=0 if totalbh_wl==1
	replace student2_morebh_wl=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbh_wl==1
	replace student2_morebh_wl=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbh_wl==1

	*black low white high
	gen     totalbl_wh=0
	replace totalbl_wh=1 if stud_one_eth==1 & stud_one_low==1 & stud_two_eth==2 & stud_two_high==1
	replace totalbl_wh=1 if stud_one_eth==2 & stud_one_high==1 & stud_two_eth==1 & stud_two_low==1

	gen     samebl_wh=0 if totalbl_wh==1
	replace samebl_wh=1 if totalbl_wh==1 & stud_one_days==stud_two_days

	gen     student1_morebl_wh=0 if totalbl_wh==1
	replace student1_morebl_wh=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbl_wh==1
	replace student1_morebl_wh=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbl_wh==1

	gen     student2_morebl_wh=0 if totalbl_wh==1
	replace student2_morebl_wh=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbl_wh==1
	replace student2_morebl_wh=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbl_wh==1

	*poor male non-poor male
	gen totalpm_npm=0
	replace totalpm_npm=1 if stud_one_frpl==1 & stud_one_male==1 & stud_two_non==1 & stud_two_male==1
	replace totalpm_npm=1 if stud_one_non==1 & stud_one_male==1 & stud_two_frpl==1 & stud_two_male==1

	gen samepm_npm=0 if totalpm_npm==1
	replace samepm_npm=1 if totalpm_npm==1 & stud_one_days==stud_two_days

	gen     student1_morepm_npm=0 if totalpm_npm==1
	replace student1_morepm_npm=1 if stud_one_frpl==1 & stud_two_non==1 & stud_one_days>stud_two_days & totalpm_npm==1
	replace student1_morepm_npm=1 if stud_two_frpl==1 & stud_one_non==1 & stud_one_days<stud_two_days & totalpm_npm==1

	gen     student2_morepm_npm=0 if totalpm_npm==1
	replace student2_morepm_npm=1 if stud_one_frpl==1 & stud_two_non==1 & stud_one_days<stud_two_days & totalpm_npm==1
	replace student2_morepm_npm=1 if stud_two_frpl==1 & stud_one_non==1 & stud_one_days>stud_two_days & totalpm_npm==1

	*poor female non-poor female
	gen totalpf_npf=0
	replace totalpf_npf=1 if stud_one_frpl==1 & stud_one_male==0 & stud_two_non==1 & stud_two_male==0
	replace totalpf_npf=1 if stud_one_non==1 & stud_one_male==0 & stud_two_frpl==1 & stud_two_male==0

	gen samepf_npf=0 if totalpf_npf==1
	replace samepf_npf=1 if totalpf_npf==1 & stud_one_days==stud_two_days

	gen     student1_morepf_npf=0 if totalpf_npf==1
	replace student1_morepf_npf=1 if stud_one_frpl==1 & stud_two_non==1 & stud_one_days>stud_two_days & totalpf_npf==1
	replace student1_morepf_npf=1 if stud_two_frpl==1 & stud_one_non==1 & stud_one_days<stud_two_days & totalpf_npf==1

	gen     student2_morepf_npf=0 if totalpf_npf==1
	replace student2_morepf_npf=1 if stud_one_frpl==1 & stud_two_non==1 & stud_one_days<stud_two_days & totalpf_npf==1
	replace student2_morepf_npf=1 if stud_two_frpl==1 & stud_one_non==1 & stud_one_days>stud_two_days & totalpf_npf==1

	*poor male non-poor female
	gen totalpm_npf=0
	replace totalpm_npf=1 if stud_one_frpl==1 & stud_one_male==1 & stud_two_non==1 & stud_two_male==0
	replace totalpm_npf=1 if stud_one_non==1 & stud_one_male==0 & stud_two_frpl==1 & stud_two_male==1

	gen samepm_npf=0 if totalpm_npf==1
	replace samepm_npf=1 if totalpm_npf==1 & stud_one_days==stud_two_days

	gen     student1_morepm_npf=0 if totalpm_npf==1
	replace student1_morepm_npf=1 if stud_one_frpl==1 & stud_one_male==1 & stud_one_days>stud_two_days & totalpm_npf==1
	replace student1_morepm_npf=1 if stud_two_frpl==1 & stud_two_male==1 & stud_one_days<stud_two_days & totalpm_npf==1

	gen     student2_morepm_npf=0 if totalpm_npf==1
	replace student2_morepm_npf=1 if stud_one_frpl==1 & stud_one_male==1 & stud_one_days<stud_two_days & totalpm_npf==1
	replace student2_morepm_npf=1 if stud_two_frpl==1 & stud_two_male==1 & stud_one_days>stud_two_days & totalpm_npf==1

	*poor female non-poor male
	gen totalpf_npm=0
	replace totalpf_npm=1 if stud_one_frpl==1 & stud_one_male==0 & stud_two_non==1 & stud_two_male==1
	replace totalpf_npm=1 if stud_one_non==1 & stud_one_male==1 & stud_two_frpl==1 & stud_two_male==0

	gen samepf_npm=0 if totalpf_npm==1
	replace samepf_npm=1 if totalpf_npm==1 & stud_one_days==stud_two_days

	gen     student1_morepf_npm=0 if totalpf_npm==1
	replace student1_morepf_npm=1 if stud_one_frpl==1 & stud_one_male==0 & stud_one_days>stud_two_days & totalpf_npm==1
	replace student1_morepf_npm=1 if stud_two_frpl==1 & stud_two_male==0 & stud_one_days<stud_two_days & totalpf_npm==1

	gen     student2_morepf_npm=0 if totalpf_npm==1
	replace student2_morepf_npm=1 if stud_one_frpl==1 & stud_one_male==0 & stud_one_days<stud_two_days & totalpf_npm==1
	replace student2_morepf_npm=1 if stud_two_frpl==1 & stud_two_male==0 & stud_one_days>stud_two_days & totalpf_npm==1

	*poor high non-poor high
	gen totalph_nph=0
	replace totalph_nph=1 if stud_one_frpl==1 & stud_one_high==1 & stud_two_non==1 & stud_two_high==1
	replace totalph_nph=1 if stud_one_non==1 & stud_one_high==1 & stud_two_frpl==1 & stud_two_high==1

	gen sameph_nph=0 if totalph_nph==1
	replace sameph_nph=1 if totalph_nph==1 & stud_one_days==stud_two_days

	gen     student1_moreph_nph=0 if totalph_nph==1
	replace student1_moreph_nph=1 if stud_one_frpl==1 & stud_one_high==1 & stud_one_days>stud_two_days & totalph_nph==1
	replace student1_moreph_nph=1 if stud_two_frpl==1 & stud_two_high==1 & stud_one_days<stud_two_days & totalph_nph==1

	gen     student2_moreph_nph=0 if totalph_nph==1
	replace student2_moreph_nph=1 if stud_one_frpl==1 & stud_one_high==1 & stud_one_days<stud_two_days & totalph_nph==1
	replace student2_moreph_nph=1 if stud_two_frpl==1 & stud_two_high==1 & stud_one_days>stud_two_days & totalph_nph==1

	*poor low non-poor low
	gen totalpl_npl=0
	replace totalpl_npl=1 if stud_one_frpl==1 & stud_one_low==1 & stud_two_non==1 & stud_two_low==1
	replace totalpl_npl=1 if stud_one_non==1 & stud_one_low==1 & stud_two_frpl==1 & stud_two_low==1

	gen samepl_npl=0 if totalpl_npl==1
	replace samepl_npl=1 if totalpl_npl==1 & stud_one_days==stud_two_days

	gen     student1_morepl_npl=0 if totalpl_npl==1
	replace student1_morepl_npl=1 if stud_one_frpl==1 & stud_one_low==1 & stud_one_days>stud_two_days & totalpl_npl==1
	replace student1_morepl_npl=1 if stud_two_frpl==1 & stud_two_low==1 & stud_one_days<stud_two_days & totalpl_npl==1

	gen     student2_morepl_npl=0 if totalpl_npl==1
	replace student2_morepl_npl=1 if stud_one_frpl==1 & stud_one_low==1 & stud_one_days<stud_two_days & totalpl_npl==1
	replace student2_morepl_npl=1 if stud_two_frpl==1 & stud_two_low==1 & stud_one_days>stud_two_days & totalpl_npl==1

	*poor high non-poor low
	gen totalph_npl=0
	replace totalph_npl=1 if stud_one_frpl==1 & stud_one_high==1 & stud_two_non==1 & stud_two_low==1
	replace totalph_npl=1 if stud_one_non==1 & stud_one_low==1 & stud_two_frpl==1 & stud_two_high==1

	gen sameph_npl=0 if totalph_npl==1
	replace sameph_npl=1 if totalph_npl==1 & stud_one_days==stud_two_days

	gen     student1_moreph_npl=0 if totalph_npl==1
	replace student1_moreph_npl=1 if stud_one_frpl==1 & stud_one_high==1 & stud_one_days>stud_two_days & totalph_npl==1
	replace student1_moreph_npl=1 if stud_two_frpl==1 & stud_two_high==1 & stud_one_days<stud_two_days & totalph_npl==1

	gen     student2_moreph_npl=0 if totalph_npl==1
	replace student2_moreph_npl=1 if stud_one_frpl==1 & stud_one_high==1 & stud_one_days<stud_two_days & totalph_npl==1
	replace student2_moreph_npl=1 if stud_two_frpl==1 & stud_two_high==1 & stud_one_days>stud_two_days & totalph_npl==1

	*poor low non-poor high
	gen totalpl_nph=0
	replace totalpl_nph=1 if stud_one_frpl==1 & stud_one_low==1 & stud_two_non==1 & stud_two_high==1
	replace totalpl_nph=1 if stud_one_non==1 & stud_one_high==1 & stud_two_frpl==1 & stud_two_low==1

	gen samepl_nph=0 if totalpl_nph==1
	replace samepl_nph=1 if totalpl_nph==1 & stud_one_days==stud_two_days

	gen     student1_morepl_nph=0 if totalpl_nph==1
	replace student1_morepl_nph=1 if stud_one_frpl==1 & stud_one_low==1 & stud_one_days>stud_two_days & totalpl_nph==1
	replace student1_morepl_nph=1 if stud_two_frpl==1 & stud_two_low==1 & stud_one_days<stud_two_days & totalpl_nph==1

	gen     student2_morepl_nph=0 if totalpl_nph==1
	replace student2_morepl_nph=1 if stud_one_frpl==1 & stud_one_low==1 & stud_one_days<stud_two_days & totalpl_nph==1
	replace student2_morepl_nph=1 if stud_two_frpl==1 & stud_two_low==1 & stud_one_days>stud_two_days & totalpl_nph==1

//===========================================
//	#7B Create Figure 7
//===========================================

	***HISTOGRAMS
	*Panel A
	replace stud_one_days=20 if stud_one_days>20
	replace stud_two_days=20 if stud_two_days>20
	gen     blk_days_diff=stud_one_days-stud_two_days if stud_one_eth==1 & stud_two_eth==2
	replace blk_days_diff=stud_two_days-stud_one_days if stud_one_eth==2 & stud_two_eth==1
	gen     wht_days_diff=stud_one_days-stud_two_days if stud_one_eth==2 & stud_two_eth==1
	replace wht_days_diff=stud_two_days-stud_one_days if stud_one_eth==1 & stud_two_eth==2
	gen total = blk_days_diff if blk_days_diff>0
	replace total = wht_days_diff if wht_days_diff>0
	gen blk_wht=.
	replace blk_wht=1 if blk_days_diff>0 & total!=.
	replace blk_wht=2 if wht_days_diff>0 & total!=.
	la de race 1 "Black" 2 "White"
	la val blk_wht race
	histogram blk_days_diff if blk_days_diff!=0, discrete width(1) frequency ytitle(Number of Fights) ///
	xtitle(Black Student Days-White Student Days) xlabel(-20(5)20) ///
	title(Distribution of Differences in Suspension Days in Black-White Fights, size(medium)) scheme(s1mono)
	save figure7_panelA.gph, replace
	*Panel B
	gen     poor_days_diff=stud_one_days-stud_two_days if stud_one_frpl==1 & stud_two_non==1
	replace poor_days_diff=stud_two_days-stud_one_days if stud_one_non==1 & stud_two_frpl==1
	gen     nonpoor_days_diff=stud_one_days-stud_two_days if stud_one_non==1 & stud_two_frpl==1
	replace nonpoor_days_diff=stud_two_days-stud_one_days if stud_one_frpl==1 & stud_two_non==1
	gen total2 = poor_days_diff if poor_days_diff>0
	replace total2 = nonpoor_days_diff if nonpoor_days_diff>0
	gen poor_nonpoor=.
	replace poor_nonpoor=1 if poor_days_diff>0 & total2!=.
	replace poor_nonpoor=2 if nonpoor_days_diff>0 & total2!=.
	la de income 1 "Poor" 2 "Non-Poor"
	la val poor_nonpoor income
	histogram poor_days_diff if poor_days_diff!=0, discrete width(1) frequency ytitle(Number of Fights) ///
	xtitle(Poor Student Days-Non-Poor Student Days) xlabel(-20(5)20) ///
	title(Distribution of Differences in Suspension Days in Poor-Non-Poor Fights, size(medium)) scheme(s1mono)
	save figure7_panelB.gph, replace

//===========================================
//	#7C Create Table 7
//===========================================

	collapse (sum) totalb_w-student2_morepl_nph
	gen check=1
	reshape long total same student1_more student2_more, i(check) j(comparison) s
	gen order=0
	replace order =1 if comparison=="b_w"
	replace order =2 if comparison=="b_b"
	replace order =3 if comparison=="w_w"
	replace order =4 if comparison=="p_np"
	replace order =5 if comparison=="p_p"
	replace order =6 if comparison=="np_np"
	replace order =7 if comparison=="m_f"
	replace order =8 if comparison=="m_m"
	replace order =9 if comparison=="f_f"
	replace order =10 if comparison=="l_h"
	replace order =11 if comparison=="l_l"
	replace order =12 if comparison=="h_h"
	replace order =13 if comparison=="bp_wp"
	replace order =14 if comparison=="bnp_wnp"
	replace order =15 if comparison=="bp_wnp"
	replace order =16 if comparison=="bnp_wp"
	replace order =17 if comparison=="bm_wm"
	replace order =18 if comparison=="bf_wf"
	replace order =19 if comparison=="bm_wf"
	replace order =20 if comparison=="bf_wm"
	replace order =21 if comparison=="bh_wh"
	replace order =22 if comparison=="bl_wl"
	replace order =23 if comparison=="bh_wl"
	replace order =24 if comparison=="bl_wh"
	replace order =25 if comparison=="pm_npm"
	replace order =26 if comparison=="pf_npf"
	replace order =27 if comparison=="pm_npf"
	replace order =28 if comparison=="pf_npm"
	replace order =29 if comparison=="ph_nph"
	replace order =30 if comparison=="pl_npl"
	replace order =31 if comparison=="ph_npl"
	replace order =32 if comparison=="pl_nph"

	sort order
	gen denominator1=(student1+student2)/(2*total)
	gen denominator2=1-denominator1 
	gen denominator3=denominator1*denominator2*(1/total+1/total)
	gen denominator=sqrt(denominator3)
	replace student1=student1/total
	replace student2=student2/total
	replace same=same/total
	gen numerator=student1-student2
	gen zscore=numerator/denominator 
	drop denominator1-numerator
	export excel using table7.xlsx, replace
	
//==============================================================================
// 8 - Create Table 8
//==============================================================================
//===========================================
//	#8A Initial data work
//===========================================
	
	use "$data\fights.dta", replace
	cd "$output"
	*amdin groups
	gen admin=0
	replace admin=1 if pct_wht_princ>=.75
	replace admin=2 if pct_wht_princ>.25 & pct_wht_princ<.75
	replace admin=3 if pct_wht_princ<=.25
	*school race groups
	gen race=0
	replace race=1 if tot_white/tot_studs>=.75
	replace race=3 if tot_white/tot_studs<=.25
	replace race=2 if race==0
	*school frpl groups
	gen frp1=lunch_frpl/tot_studs
	xtile frp=frp1, nq(3)
	replace frp=0 if frp==1
	replace frp=1 if frp==3
	replace frp=3 if frp==0
	drop frp1
	*school locale groups
	replace urbanicity=0
	replace urbanicity=1 if urban==1
	replace urbanicity=2 if suburban==1
	replace urbanicity=3 if rural==1
	bysort sch_id: egen mode_urban=mode(urbanicity)
	replace urbanicity=mode_urban if urbanicity==0
	drop mode_urban
	*school level groups
	gen grade_group=0
	replace grade_group=1 if stud_1_indicator==1 & grade<6
	replace grade_group=2 if stud_1_indicator==1 & grade>5 & grade<9
	replace grade_group=3 if stud_1_indicator==1 & grade>8
	sort incident_id stud_1_indicator
	replace grade_group=grade_group[_n+1] if incident_id==incident_id[_n+1]
	
//===========================================
//	#8B Create Table 8
//===========================================

	foreach x of varlist admin race frp urbanicity grade_group {
	foreach y in 1 2 3 {

	xtreg days black fight_count if same_fight==2 & blk_wht==1 & `x'==`y', fe i(group) cluster(group)
		outreg using table8_`x'_`y', se varlabels starloc(1) nole starlevels(10 5 1) ///
		t("Table X: Discipline Disparities in Fights Between Two Students - Heterogeneity Analyses `x' `y'") basefont(roman fs12) ///
		keep(black) ct("", "(1)") /// 
		addrows("Student Controls", "No" \ "School FEs", "Yes") ///
		note("Note: * p<0.1; ** p<0.05; *** p<0.01") summstat(N \ r2) replace
		
	xtreg days black if same_fight==2 & blk_wht==1 & use==1 & `x'==`y', fe i(group) cluster(group)
		outreg using table8_`x'_`y', se varlabels starloc(1) nole starlevels(10 5 1) ///
		basefont(roman fs12) keep(black) ct("", "(2)") /// 
		addrows("Student Controls", "No" \ "School FEs", "Yes") ///
		note("Note: * p<0.1; ** p<0.05; *** p<0.01") summstat(N \ r2) merge

	xtreg more_days black fight_count if same_fight==2 & blk_wht==1 & `x'==`y', fe i(group) cluster(group)
		outreg using table8_`x'_`y', se varlabels starloc(1) nole starlevels(10 5 1) ///
		basefont(roman fs12) keep(black) ct("", "(3)") /// 
		addrows("Student Controls", "No" \ "School FEs", "Yes") ///
		note("Note: * p<0.1; ** p<0.05; *** p<0.01") summstat(N \ r2) merge
		
	xtreg more_days black if same_fight==2 & blk_wht==1 & use==1 & `x'==`y', fe i(group) cluster(group)
		outreg using table8_`x'_`y', se varlabels starloc(1) nole starlevels(10 5 1) ///
		basefont(roman fs12) keep(black) ct("", "(4)") /// 
		addrows("Student Controls", "No" \ "School FEs", "Yes") ///
		note("Note: * p<0.1; ** p<0.05; *** p<0.01") summstat(N \ r2) merge

}
}

//==============================================================================
// 9 - Create Table 9
//==============================================================================
//===========================================
//	#9A Initial data work
//===========================================
	
	use "$data\fights.dta", replace
	cd "$output"
	
	*black white
	gen     totalb_w=0
	replace totalb_w=1 if stud_one_eth==1 & stud_two_eth==2
	replace totalb_w=1 if stud_one_eth==2 & stud_two_eth==1

	gen     innb_w=0
	replace innb_w=1 if totalb_w==1 & stud_one_type>3 & stud_two_type>3

	gen     outb_w=0
	replace outb_w=1 if totalb_w==1 & stud_one_type==2 & stud_two_type==2

	gen     out1_in2b_w=0
	replace out1_in2b_w=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_type==2 & stud_two_type>3
	replace out1_in2b_w=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_type>3 & stud_two_type==2

	gen     in1_out2b_w=0
	replace in1_out2b_w=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_type>3 & stud_two_type==2
	replace in1_out2b_w=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_type==2 & stud_two_type>3

	*black black*
	gen     totalb_b=0
	replace totalb_b=1 if stud_one_eth==1 & stud_two_eth==1

	gen     innb_b=0
	replace innb_b = 1 if totalb_b==1 & stud_one_type>3 & stud_two_type>3

	gen     outb_b=0
	replace outb_b=1 if totalb_b==1 & stud_one_type==2 & stud_two_type==2


	*white white*
	gen     totalw_w=0
	replace totalw_w=1 if stud_one_eth==2 & stud_two_eth==2

	gen     innw_w=0
	replace innw_w = 1 if totalw_w==1 & stud_one_type>3 & stud_two_type>3

	gen     outw_w=0
	replace outw_w=1 if totalw_w==1 & stud_one_type==2 & stud_two_type==2

	*FRL Non FRL*
	gen     totalp_np = 0
	replace totalp_np=1 if stud_one_frpl==1 & stud_two_non==1
	replace totalp_np=1 if stud_one_non==1 & stud_two_frpl==1

	gen     innp_np=0
	replace innp_np=1 if totalp_np==1 & stud_one_type>3 & stud_two_type>3

	gen     outp_np=0
	replace outp_np=1 if totalp_np==1 & stud_one_type==2 & stud_two_type==2

	gen     out1_in2p_np=0
	replace out1_in2p_np=1 if stud_one_frpl==1 & stud_two_non==1 & stud_one_type==2 & stud_two_type>3
	replace out1_in2p_np=1 if stud_one_non==1 & stud_two_frpl==1 & stud_one_type>3 & stud_two_type==2

	gen     in1_out2p_np=0
	replace in1_out2p_np=1 if stud_one_frpl==1 & stud_two_non==1 & stud_one_type>3 & stud_two_type==2
	replace in1_out2p_np=1 if stud_one_non==1 & stud_two_frpl==1 & stud_one_type==2 & stud_two_type>3

	*poor poor*
	gen     totalp_p=0
	replace totalp_p=1 if stud_one_frpl==1 & stud_two_frpl==1

	gen     innp_p=0
	replace innp_p=1 if totalp_p==1 & stud_one_type>3 & stud_two_type>3

	gen     outp_p=0
	replace outp_p=1 if totalp_p==1 & stud_one_type==2 & stud_two_type==2

	*nonpoor nonpoor*
	gen     totalnp_np=0
	replace totalnp_np=1 if stud_one_non==1 & stud_two_non==1

	gen     innnp_np=0
	replace innnp_np = 1 if totalnp_np==1 & stud_one_type>3 & stud_two_type>3

	gen     outnp_np=0
	replace outnp_np=1 if totalnp_np==1 & stud_one_type==2 & stud_two_type==2

//===========================================
//	#9B Initial data work
//===========================================

	collapse (sum) totalb_w-outnp_np
	gen check=1
	reshape long total inn out out1_in2 in1_out2, i(check) j(comparison) s
	gen order=0
	replace order =1 if comparison=="b_w"
	replace order =2 if comparison=="b_b"
	replace order =3 if comparison=="w_w"
	replace order =4 if comparison=="p_np"
	replace order =5 if comparison=="p_p"
	replace order =6 if comparison=="np_np"

	sort order
	gen denominator1=(out1_in2+in1_out2)/(2*total)
	gen denominator2=1-denominator1 
	gen denominator3=denominator1*denominator2*(1/total+1/total)
	gen denominator=sqrt(denominator3)
	replace out1_in2=out1_in2/total
	replace in1_out2=in1_out2/total
	replace out=out/total
	replace inn=inn/total
	gen numerator=out1_in2-in1_out2
	gen zscore=numerator/denominator 
	drop denominator1-numerator	
	export excel using table9.xlsx, replace

//==============================================================================
// 10 - Figure 1 & Figure 2
//==============================================================================
	
//=================================
//	#10A Data work
//=================================

	use  "$data\sample01-all_students-v02.dta", replace

* Restricting to suspended students
	keep if suspend==1

* Simplifying Always FRPL and Never FRPL for output matrix
	rename always_dis_flg alwaysp
	rename never_dis_flg neverp


//==========================================
//	#10B Create output matrix and populate
//==========================================

* Create output matrix
	matrix out = J(4,14,.)
	matrix rownames out = black white alwaysp neverp
	matrix colnames out = y2001 y2002 y2003 y2004 y2005 y2006 y2007 y2008 y2009 y2010 y2011 y2012 y2013 y2014

* Calculate ISS rates by year and populate output matrix
	foreach stu in black white alwaysp neverp {
		forvalues yr = 2001(1)2014 {
			qui: count if `stu'==1 & year==`yr'
			scalar `stu'tot = r(N)
			qui: count if `stu' & disc_type==2 & year==`yr'
			scalar `stu'iss = r(N)
			scalar `stu'prop = `stu'iss/`stu'tot
			scalar row = rownumb(out,"`stu'")
			scalar col = colnumb(out,"y`yr'")
			matrix out[row,col] = round(`stu'prop,.0001)
		}
	}


//====================
//	#10C Save results
//====================

	cd "$output"
	mat2txt, matrix(out) saving(figure0102-describe-iss_v_oss_over_time) replace

//==============================================================================
// 11 - Figure 3, Figure 4, & Table A1
//==============================================================================

//============================================================================
//	#11A Data prep
//============================================================================

	use  "$data\sample01-all_students-v02.dta", replace

* Keep first record for discipline students (unit of analysis is infraction for
* disciplined students in sample01-all_students.dta)
	keep if missing(disc_cd) | disc_dt_start==first_off_date
	duplicates tag stud_id year, gen(xdupe)
	
	* Randomly keeping one observation for remaining dupes
	gen double xrand = runiform() if xdupe>0 & !missing(xdupe)
	bysort stud_id year: egen double xmin = min(xrand)
	keep if xdupe==0 | xrand==xmin
	duplicates report stud_id year
	
* prep for loops
	rename suspend susp
	rename mult_suspend mults
	rename sch_id sch
	rename lea_id lea
	rename sped2 sped
	rename always_dis_flg afrpl
	
	foreach demo in male black sped {
		gen not`demo' = 1-`demo'
		la var not`demo' "stu `demo'==0"
		tab `demo' not`demo', m
		tab year `demo', m
	}
	
* special coding for Always FRPL/Never FRPL comparison (ignoring transitorily FRPL)
	gen notafrpl = never_dis_flg
	replace notafrpl = . if trans_dis_flg==1
	la var notafrpl "Never FRPL"
	replace afrpl = . if trans_dis_flg==1
		

//============================================================================
//	#11B Decomposition 
//============================================================================

* identify school and district counts
	bysort year sch: egen schn = count(stud_id)
	bysort year lea: egen lean = count(stud_id)

* loops
	foreach demo in male black afrpl sped {
		//==========================================================================
		//	Step 1 - Create overall gap 
		//==========================================================================
			
		* Create cross-products
			gen `demo'_susp = `demo'*susp						// jnm: redundant. can substitute cross product directly into total()
			la var `demo'_susp "`demo'==1 & susp"
			gen not`demo'_susp = not`demo'*susp
			la var not`demo'_susp "`demo'==0 & susp"
			
		* Create overall measures
			bysort grade: egen top = total(`demo'_susp)			// jnm: summing across students, by grade
			bysort grade: egen bot = total(`demo')
			gen st_susprt_`demo' = top/bot
			la var st_susprt_`demo' "state lev. susp rate among `demo'==1, by grade"
			drop top bot
			
			bysort grade: egen top = total(not`demo'_susp)
			bysort grade: egen bot = total(not`demo')
			gen st_susprt_not`demo' = top/bot
			la var st_susprt_not`demo' "state lev. susp rate among `demo'==0, by grade"
			drop top bot
			
		* Overall gap
			gen st_susp_`demo'_gap = st_susprt_`demo' - st_susprt_not`demo'		// jnm: gap accounts for differences across years
			la var st_susp_`demo'_gap "state lev. diff in susp rates, by grade"
			
		//==============================================
		//	Step 2 - Create district-level measures
		//==============================================
			
		* district level suspension rate 
			bysort grade year lea: egen lea_susprt_`demo'analysis = mean(susp)
			la var lea_susprt_`demo'analysis "district lev. susp rate for `demo' analysis, by year and grade"
			
		* Cross prod. to create district level `demo'==1 and `demo'==0 suspension rates
			gen `demo'_susprt = `demo'*lea_susprt_`demo'analysis
			la var `demo'_susprt "`demo'==1 and district lev. susp rate"
			gen not`demo'_susprt = not`demo'*lea_susprt_`demo'analysis
			la var not`demo'_susprt "`demo'==0 and district lev. susp rate"
			
		* district level conditional suspension rates (Ed(susprt) & En(susprt))
			bysort grade: egen top = total(`demo'_susprt)
			bysort grade: egen bot = total(`demo')
			gen lea_susprt_`demo' = top/bot
			la var lea_susprt_`demo' "lea. lev. susp rate among `demo'==1, by grade"
			drop top bot `demo'_susprt 
			
			bysort grade: egen top = total(not`demo'_susprt)
			bysort grade: egen bot = total(not`demo')
			gen lea_susprt_not`demo' = top/bot
			la var lea_susprt_not`demo' "lea. lev. susp rate among `demo'==0, by grade"
			drop top bot not`demo'_susprt
	
		* check
			*tab lea_susprt_`demo' year
			
		//==============================================
		//	Step 3 - Create school-level measures  
		//===============================================

		* School level suspension rate
			bysort grade year sch: egen sch_susprt_`demo'analysis = mean(susp)	if !missing(`demo')		// 02.22.17 - jnmills -- adjusted for missing other students in black/white comparison
			la var sch_susprt_`demo'analysis "schrict lev. susp rate for `demo' analysis, by year and grade"
			
		* Cross prod. to create schrict level `demo'==1 and `demo'==0 suspension rates
			gen `demo'_susprt = `demo'*sch_susprt_`demo'analysis
			la var `demo'_susprt "`demo'==1 and schrict lev. susp rate"
			gen not`demo'_susprt = not`demo'*sch_susprt_`demo'analysis
			la var not`demo'_susprt "`demo'==0 and schrict lev. susp rate"
			
		* School level conditional suspension rates (Ed(susprt) & En(susprt))
			bysort grade: egen top = total(`demo'_susprt)
			bysort grade: egen bot = total(`demo')
			gen sch_susprt_`demo' = top/bot
			la var sch_susprt_`demo' "sch. lev. susp rate among `demo'==1, by grade"
			drop top bot `demo'_susprt 
			
			bysort grade: egen top = total(not`demo'_susprt)
			bysort grade: egen bot = total(not`demo')
			gen sch_susprt_not`demo' = top/bot
			la var sch_susprt_not`demo' "sch. lev. susp rate among `demo'==0, by grade"
			drop top bot not`demo'_susprt
			
		* check
			*tab sch_susprt_`demo' year
			
		//=============================================================
		//	Step 4 - Decompose gap into district, school, and grade
		//=============================================================

		* district component
			gen lea_susp_`demo'_gap = lea_susprt_`demo' - lea_susprt_not`demo' if !missing(`demo')		// 02.22.17 - jnmills -- adjusted for missing other students in black/white comparison
			la var lea_susp_`demo'_gap "district lev. diff in susp rates"

		* School component
			gen sch_susp_`demo'_gap = [(sch_susprt_`demo' - lea_susprt_`demo')] - [(sch_susprt_not`demo' - lea_susprt_not`demo')]
			la var sch_susp_`demo'_gap "school lev. diff in susp rates"
			
		* within school component
			gen wthnsch_susp_`demo'_gap = [(st_susprt_`demo' - sch_susprt_`demo')] - [(st_susprt_not`demo' - sch_susprt_not`demo')]
			la var wthnsch_susp_`demo'_gap "grade lev. diff in susp rates"
	}

* Output table
	matrix out = J(65,4,.)
	matrix rownames out = ///
		grade0	_overall	_dist	_sch	_within	///
		grade1	_overall	_dist	_sch	_within	///
		grade2	_overall	_dist	_sch	_within	///
		grade3	_overall	_dist	_sch	_within	///
		grade4	_overall	_dist	_sch	_within	///
		grade5	_overall	_dist	_sch	_within	///
		grade6	_overall	_dist	_sch	_within	///
		grade7	_overall	_dist	_sch	_within	///
		grade8	_overall	_dist	_sch	_within	///
		grade9	_overall	_dist	_sch	_within	///
		grade10	_overall	_dist	_sch	_within	///
		grade11	_overall	_dist	_sch	_within	///
		grade12	_overall	_dist	_sch	_within	
	matrix colnames out = ///
		s_male s_black s_afrpl s_sped 
		
	* graph matrix
	matrix graph = J(13,20,.)
	matrix rownames graph = ///
		grade0 grade1 grade2 grade3 grade4 grade5 grade6 ///
		grade7 grade8 grade9 grade10 grade11 grade12

	matrix colnames graph = ///
		male_n  male_o  male_bd  male_bs  male_ws  ///
		black_n black_o black_bd black_bs black_ws ///
		afrpl_n afrpl_o afrpl_bd afrpl_bs afrpl_ws  ///
		sped_n  sped_o  sped_bd  sped_bs  sped_ws	

	forvalues g = 0(1)12 {
		foreach demo in male black afrpl sped {
			scalar row = rownumb(out,"grade`g'")
			scalar colm = colnumb(out,"s_`demo'")
			qui: sum st_susp_`demo'_gap if grade==`g'
			scalar state = r(mean)
			qui: sum lea_susp_`demo'_gap if grade==`g'
			scalar lea = r(mean)
			qui: sum sch_susp_`demo'_gap if grade==`g'
			scalar school = r(mean)
			qui: sum wthnsch_susp_`demo'_gap if grade==`g'
			scalar within = r(mean)
			
			matrix out[row+1,colm] = state
			matrix out[row+2,colm] = lea
			matrix out[row+3,colm] = school
			matrix out[row+4,colm] = within

			scalar row = rownumb(graph,"grade`g'")
			scalar col = colnumb(graph,"`demo'_o")
			matrix graph[row,col  ] = state
			matrix graph[row,col+1] = lea
			matrix graph[row,col+2] = school
			matrix graph[row,col+3] = within
				
		}
	}
	
	matlist out
	cd "$output"
	mat2txt, matrix(out) saving(tableA01-decomp-psusp) replace

//============================================================
//	#11C	Graphing decomp results
//============================================================
			
	drop _all
	svmat graph, names(col)
	gen grade = _n-1
	order grade, first
	set scheme s2mono

* Black vs. White
	twoway 	(area  black_o grade, astyle(background))  /// 
			(line black_bd grade, connect(direct) lpattern(solid) 	 lcolor(black) 	lwidth(medthick)) ///
			(line black_bs grade, connect(direct) lpattern(longdash) lcolor(black)	lwidth(medthick)) ///
			(line black_ws grade, connect(direct) lpattern(dash) 	 lcolor(black) 	lwidth(medthick)) ///
			(function y=0, range(grade) clcolor(fg) lpattern(solid)) ///
		, 	ytitle(Suspension Gap) xtitle(Grade) ///
			legend(order(1 2 3 4) lab(1 "Overall Gap") lab(2 "Across Districts") lab(3 "Across Schools") lab(4 "Within Schools")) ///
			yscale(range(-.02 .20)) ylabel(-.02(.02).20) ///
			xlabel(0(1)12, valuelabel) 
	graph export "$output\figure03-psusp-race.png", replace		

* FRPL
	twoway 	(area  afrpl_o grade, astyle(background)) ///
			(line afrpl_bd grade, connect(direct) lpattern(solid) 	 lcolor(black) 	lwidth(medthick)) ///
			(line afrpl_bs grade, connect(direct) lpattern(longdash) lcolor(black)	lwidth(medthick)) ///
			(line afrpl_ws grade, connect(direct) lpattern(dash) 	 lcolor(black) 	lwidth(medthick)) ///
			(function y=0, range(grade) clcolor(fg) lpattern(solid)) ///
		 , 	ytitle(Suspension Gap) xtitle(Grade) ///		
			legend(order(1 2 3 4) lab(1 "Overall Gap") lab(2 "Across Districts") lab(3 "Across Schools") lab(4 "Within Schools")) ///
			yscale(range(-.02 .20)) ylabel(-.02(.02).20) ///
			xlabel(0(1)12, valuelabel)	
	graph export "$output\figure04-psusp-poverty.png", replace		

//==============================================================================
// 12 - Figure 5, Figure 6, & Table A2
//==============================================================================
//============================================================================
//	#12A Data prep
//============================================================================

	use  "$data\sample01-all_students-v02.dta", replace

	* Censoring days suspended to 20
		clonevar susp_days = susp_days_est
			replace susp_days = 20 if susp_days_est>=20 & !missing(susp_days_est)

	* Create totdays in year
		bysort stud_id year: egen totdays = total(susp_days) if suspend==1
			replace totdays = 0 if flag_nodisc==1	//updating for non-enrolled
		la var totdays "Total Days Suspended in School Year"

	* Restricting to suspended students
		keep if suspend==1

	* Keep first record for discipline students (unit of analysis is infraction for
	* disciplined students in sample01-all_students.dta)
		keep if missing(disc_cd) | disc_dt_start==first_off_date
		duplicates tag stud_id year, gen(xdupe)

	* Randomly keeping one observation for remaining dupes
		gen double xrand = runiform() if xdupe>0 & !missing(xdupe)
		bysort stud_id year: egen double xmin = min(xrand)
		keep if xdupe==0 | xrand==xmin
		duplicates report stud_id year

	* prep for loops
		rename sch_id sch
		rename lea_id lea
		rename sped2 sped
		rename always_dis_flg afrpl	// jhr addition

		foreach demo in male black sped {
			gen not`demo' = 1-`demo'
			la var not`demo' "stu `demo'==0"
			tab `demo' not`demo', m
			tab year `demo', m
		}

	* special coding for Always FRPL/Never FRPL comparison (ignoring transitorily FRPL)
		gen notafrpl = never_dis_flg
		replace notafrpl = . if trans_dis_flg==1
		la var notafrpl "Never FRPL"
		replace afrpl = . if trans_dis_flg==1
		

//============================================================================
//	#12B Decomposition 
//============================================================================

	* identify school and district counts
		bysort year sch: egen schn = count(stud_id)
		bysort year lea: egen lean = count(stud_id)

	* loops
		foreach demo in male black afrpl sped {
		//==========================================================================
		//	Step 1 - Create overall gap 
		//==========================================================================
			
		* Create cross-products
			gen `demo'_totdays = `demo'*totdays						// jnm: redundant. can substitute cross product directly into total()
			la var `demo'_totdays "`demo'==1 & totdays"
			gen not`demo'_totdays = not`demo'*totdays
			la var not`demo'_totdays "`demo'==0 & totdays"
			
		* Create overall measures
			bysort grade: egen top = total(`demo'_totdays)			// jnm: summing across students, by grade
			bysort grade: egen bot = total(`demo')
			gen st_totdaysavg_`demo' = top/bot
			la var st_totdaysavg_`demo' "state lev. totdays avg among `demo'==1, by grade"
			drop top bot
			
			bysort grade: egen top = total(not`demo'_totdays)
			bysort grade: egen bot = total(not`demo')
			gen st_totdaysavg_not`demo' = top/bot
			la var st_totdaysavg_not`demo' "state lev. totdays avg among `demo'==0, by grade"
			drop top bot
			
		* Overall gap
			gen st_totdays_`demo'_gap = st_totdaysavg_`demo' - st_totdaysavg_not`demo'		// jnm: gap accounts for differences across years
			la var st_totdays_`demo'_gap "state lev. diff in totdays avgs, by grade"
			
				
		//==============================================
		//	Step 2 - Create district-level measures
		//==============================================
			
		* district level total days suspended average 
			bysort grade year lea: egen lea_totdaysavg_`demo'analysis = mean(totdays)
			la var lea_totdaysavg_`demo'analysis "district lev. totdays avg for `demo' analysis, by year and grade"
			
		* Cross prod. to create district level `demo'==1 and `demo'==0 total days suspended average 
			gen `demo'_totdaysavg = `demo'*lea_totdaysavg_`demo'analysis
			la var `demo'_totdaysavg "`demo'==1 and district lev. totdays avg"
			gen not`demo'_totdaysavg = not`demo'*lea_totdaysavg_`demo'analysis
			la var not`demo'_totdaysavg "`demo'==0 and district lev. totdays avg"
			
		* district level conditional total days suspended average  (Ed(susprt) & En(susprt))
			bysort grade: egen top = total(`demo'_totdaysavg)
			bysort grade: egen bot = total(`demo')
			gen lea_totdaysavg_`demo' = top/bot
			la var lea_totdaysavg_`demo' "lea. lev. totdays avg among `demo'==1, by grade"
			drop top bot `demo'_totdaysavg 
			
			bysort grade: egen top = total(not`demo'_totdaysavg)
			bysort grade: egen bot = total(not`demo')
			gen lea_totdaysavg_not`demo' = top/bot
			la var lea_totdaysavg_not`demo' "lea. lev. totdays avg among `demo'==0, by grade"
			drop top bot not`demo'_totdaysavg
	
		* check
			*tab lea_totdaysavg_`demo' year
			
		//==============================================
		//	Step 3 - Create school-level measures  
		//===============================================

		* School level total days suspended average 
			bysort grade year sch: egen sch_totdaysavg_`demo'analysis = mean(totdays)	if !missing(`demo')		// 02.22.17 - jnmills -- adjusted for missing other students in black/white comparison
			la var sch_totdaysavg_`demo'analysis "schrict lev. totdays avg for `demo' analysis, by year and grade"
			
		* Cross prod. to create schrict level `demo'==1 and `demo'==0 total days suspended average 
			gen `demo'_totdaysavg = `demo'*sch_totdaysavg_`demo'analysis
			la var `demo'_totdaysavg "`demo'==1 and schrict lev. totdays avg"
			gen not`demo'_totdaysavg = not`demo'*sch_totdaysavg_`demo'analysis
			la var not`demo'_totdaysavg "`demo'==0 and schrict lev. totdays avg"
			
		* School level conditional total days suspended average (Ed(susprt) & En(susprt))
			bysort grade: egen top = total(`demo'_totdaysavg)
			bysort grade: egen bot = total(`demo')
			gen sch_totdaysavg_`demo' = top/bot
			la var sch_totdaysavg_`demo' "sch. lev. totdays avg among `demo'==1, by grade"
			drop top bot `demo'_totdaysavg 
			
			bysort grade: egen top = total(not`demo'_totdaysavg)
			bysort grade: egen bot = total(not`demo')
			gen sch_totdaysavg_not`demo' = top/bot
			la var sch_totdaysavg_not`demo' "sch. lev. totdays avg among `demo'==0, by grade"
			drop top bot not`demo'_totdaysavg
			
		* check
			*tab sch_totdaysavg_`demo' year
			
		//=============================================================
		//	Step 4 - Decompose gap into district, school, and grade
		//=============================================================

		* district component
			gen lea_totdays_`demo'_gap = lea_totdaysavg_`demo' - lea_totdaysavg_not`demo' if !missing(`demo')		// 02.22.17 - jnmills -- adjusted for missing other students in black/white comparison
			la var lea_totdays_`demo'_gap "district lev. diff in totdays avgs"

		* School component
			gen sch_totdays_`demo'_gap = [(sch_totdaysavg_`demo' - lea_totdaysavg_`demo')] - [(sch_totdaysavg_not`demo' - lea_totdaysavg_not`demo')]
			la var sch_totdays_`demo'_gap "school lev. diff in totdays avgs"
			
		* within school component
			gen wthnsch_totdays_`demo'_gap = [(st_totdaysavg_`demo' - sch_totdaysavg_`demo')] - [(st_totdaysavg_not`demo' - sch_totdaysavg_not`demo')]
			la var wthnsch_totdays_`demo'_gap "grade lev. diff in totdays avgs"
	}

* Output table
	matrix out = J(65,4,.)
	matrix rownames out = ///
		grade0	_overall	_dist	_sch	_within	///
		grade1	_overall	_dist	_sch	_within	///
		grade2	_overall	_dist	_sch	_within	///
		grade3	_overall	_dist	_sch	_within	///
		grade4	_overall	_dist	_sch	_within	///
		grade5	_overall	_dist	_sch	_within	///
		grade6	_overall	_dist	_sch	_within	///
		grade7	_overall	_dist	_sch	_within	///
		grade8	_overall	_dist	_sch	_within	///
		grade9	_overall	_dist	_sch	_within	///
		grade10	_overall	_dist	_sch	_within	///
		grade11	_overall	_dist	_sch	_within	///
		grade12	_overall	_dist	_sch	_within	
	matrix colnames out = ///
		s_male s_black s_afrpl s_sped 
		
	* graph matrix
	matrix graph = J(13,20,.)
	matrix rownames graph = ///
		grade0 grade1 grade2 grade3 grade4 grade5 grade6 ///
		grade7 grade8 grade9 grade10 grade11 grade12

	matrix colnames graph = ///
		male_n  male_o  male_bd  male_bs  male_ws  ///
		black_n black_o black_bd black_bs black_ws ///
		afrpl_n  afrpl_o  afrpl_bd  afrpl_bs  afrpl_ws  ///
		sped_n  sped_o  sped_bd  sped_bs  sped_ws	

	forvalues g = 0(1)12 {
		foreach demo in male black afrpl sped {
			scalar row = rownumb(out,"grade`g'")
			scalar colm = colnumb(out,"s_`demo'")
			qui: sum st_totdays_`demo'_gap if grade==`g'
			scalar state = r(mean)
			qui: sum lea_totdays_`demo'_gap if grade==`g'
			scalar lea = r(mean)
			qui: sum sch_totdays_`demo'_gap if grade==`g'
			scalar school = r(mean)
			qui: sum wthnsch_totdays_`demo'_gap if grade==`g'
			scalar within = r(mean)
			
			matrix out[row+1,colm] = state
			matrix out[row+2,colm] = lea
			matrix out[row+3,colm] = school
			matrix out[row+4,colm] = within

			scalar row = rownumb(graph,"grade`g'")
			scalar col = colnumb(graph,"`demo'_o")
			matrix graph[row,col  ] = state
			matrix graph[row,col+1] = lea
			matrix graph[row,col+2] = school
			matrix graph[row,col+3] = within
				
		}
	}
	
	matlist out
	cd "$output"
	mat2txt, matrix(out) saving(tableA02-decomp-days_susp) replace

//============================================================
//	#12C Graphing decomp results
//============================================================
			
	drop _all
	svmat graph, names(col)
	gen grade = _n-1
	order grade, first
	set scheme s2mono
	
* Black vs. White
	twoway 	(area  black_o grade, astyle(background))  /// 
			(line black_bd grade, connect(direct) lpattern(solid) 	 lcolor(black) 	lwidth(medthick)) ///
			(line black_bs grade, connect(direct) lpattern(longdash) lcolor(black)	lwidth(medthick)) ///
			(line black_ws grade, connect(direct) lpattern(dash) 	 lcolor(black) 	lwidth(medthick)) ///
			(function y=0, range(grade) clcolor(fg) lpattern(solid)) ///
		, 	ytitle(Days Suspended Gap) xtitle(Grade) ///
			legend(order(1 2 3 4) lab(1 "Overall Gap") lab(2 "Across Districts") lab(3 "Across Schools") lab(4 "Within Schools")) ///
			yscale(range(-.6 2.6)) ylabel(-.6(.4)2.6) ///
			xlabel(0(1)12, valuelabel) 
	graph export "$output\figure05-decomp-days_susp-race.png", replace		

* Always FRPL
	twoway 	(area  afrpl_o grade, astyle(background)) ///
			(line afrpl_bd grade, connect(direct) lpattern(solid) 	 lcolor(black) 	lwidth(medthick)) ///
			(line afrpl_bs grade, connect(direct) lpattern(longdash) lcolor(black)	lwidth(medthick)) ///
			(line afrpl_ws grade, connect(direct) lpattern(dash) 	 lcolor(black) 	lwidth(medthick)) ///
			(function y=0, range(grade) clcolor(fg) lpattern(solid)) ///
		 , 	ytitle(Days Suspended Gap) xtitle(Grade) ///		
			legend(order(1 2 3 4) lab(1 "Overall Gap") lab(2 "Across Districts") lab(3 "Across Schools") lab(4 "Within Schools")) ///
			yscale(range(-.6 2.6)) ylabel(-.6(.4)2.6) ///
			xlabel(0(1)12, valuelabel)	
	graph export "$output\figure06-decomp-days_susp-always_frpl.png", replace		

//==============================================================================
// 13 - Table A3
//==============================================================================

//===========================================
//	#13A Initial data work
//===========================================

	use  "$data\sample01-disc_students-v02.dta", replace
		
* Censoring OSS days to 20
	clonevar oss_days = oss_days_est
		replace oss_days = 20 if oss_days_est>=20 & !missing(oss_days_est)

* Randomly keeping one record if duplicate on stud_id disc_dt_st disc_dt_end
	duplicates tag stud_id disc_dt_st disc_dt_end, gen(xdupe)
	gen double xrando = uniform() if xdupe>0
	bysort stud_id disc_dt_start: egen double xmin = min(xrando)
	keep if xrando==xmin
	gen flag_discdupe = 1 if !missing(xrando)
	la var flag_discdupe "flag: randomly retained dupe on stud_id disc_dt_st disc_dt_end"
	drop x*

* Regression globals
	cd "$output"

	global keepreg	"!missing(frpl)"
	global xrace	black other
	global xfrpl	always_dis_flg trans_dis_flg
	global xachieve	math_lag1 ela_lag1
	global disc		i.disc_rank
	
	
//============================
//	#13B Regressions
//============================

* 1. Race only
	reg oss_days $xrace $disc i.year i.grade if $keepreg, cluster(sgy)
	outreg2 using tableA03-predict_oss_days.xml ///
		, 	dec(3) replace label addtext(Disc. Controls, Yes, Year FE, Yes, Grade FE, Yes, School-Grade-Year FE, No) ///
			title("Table A3. Predictors of Length of OSS (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr)
	
	areg oss_days $xrace $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using tableA03-predict_oss_days.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table A3. Predictors of Length of OSS (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
* 2. FRPL only
	reg oss_days $xfrpl $disc i.year i.grade if $keepreg, cluster(sgy)
	outreg2 using tableA03-predict_oss_days.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, Yes, Grade FE, Yes, School-Grade-Year FE, No) ///
			title("Table A3. Predictors of Length of OSS (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
	areg oss_days $xfrpl $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using tableA03-predict_oss_days.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table A3. Predictors of Length of OSS (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
* 3. Race and frpl
	reg oss_days $xrace $xfrpl $disc i.year i.grade if $keepreg, cluster(sgy)
	outreg2 using tableA03-predict_oss_days.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, Yes, Grade FE, Yes, School-Grade-Year FE, No) ///
			title("Table A3. Predictors of Length of OSS (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
	areg oss_days $xrace $xfrpl $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using tableA03-predict_oss_days.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table A3. Predictors of Length of OSS (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 		
	
	areg oss_days $xrace $xfrpl $disc susp_prioryr if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using tableA03-predict_oss_days.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table A3. Predictors of Length of OSS (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 	
			
* 4. Combined model		
	reg oss_days $xrace $xfrpl sped2 male $xachieve $disc i.year i.grade if $keepreg, cluster(sgy)
	outreg2 using tableA03-predict_oss_days.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, Yes, Grade FE, Yes, School-Grade-Year FE, No) ///
			title("Table A3. Predictors of Length of OSS (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
	areg oss_days $xrace $xfrpl sped2 male $xachieve $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using tableA03-predict_oss_days.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table A3. Predictors of Length of OSS (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 

	areg oss_days $xrace $xfrpl sped2 male $xachieve susp_prioryr $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using tableA03-predict_oss_days.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table A3. Predictors of Length of OSS (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 

//==============================================================================
// 14 - Table A4
//==============================================================================

//===========================================
//	#14A Initial data work
//===========================================
	use  "$data\sample01-disc_students-v02.dta", replace

* Censoring OSS days to 20
	clonevar oss_days = oss_days_est
		replace oss_days = 20 if oss_days_est>=20 & !missing(oss_days_est)

* Keep first record for discipline students (unit of analysis is infraction for
* 	disciplined students in sample01-all_students.dta)
	keep if missing(disc_cd) | disc_dt_start==first_off_date
	duplicates tag stud_id year, gen(xdupe)

* Randomly keeping one observation for remaining dupes
	gen double xrand = runiform() if xdupe>0 & !missing(xdupe)
	bysort stud_id year: egen double xmin = min(xrand)
	keep if xdupe==0 | xrand==xmin

* Regression globals
	cd "$output"

	global keepreg	"!missing(frpl)"
	global xrace	black other
	global xfrpl	always_dis_flg trans_dis_flg
	global xachieve	math_lag1 ela_lag1
	global disc		i.disc_rank
		

//============================
//	#14B Regressions
//============================
	
* 1. Race only
	reg oss_days $xrace $disc i.year i.grade if $keepreg, cluster(sgy)
	outreg2 using tableA04-predict_oss_days-1stoffs.xml ///
		, 	dec(3) replace label addtext(Disc. Controls, Yes, Year FE, Yes, Grade FE, Yes, School-Grade-Year FE, No) ///
			title("Table A4. Predictors of Length of OSS for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr)
	
	areg oss_days $xrace $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using tableA04-predict_oss_days-1stoffs.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table A4. Predictors of Length of OSS for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
* 2. FRPL only
	reg oss_days $xfrpl $disc i.year i.grade if $keepreg, cluster(sgy)
	outreg2 using tableA04-predict_oss_days-1stoffs.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, Yes, Grade FE, Yes, School-Grade-Year FE, No) ///
			title("Table A4. Predictors of Length of OSS for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
	areg oss_days $xfrpl $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using tableA04-predict_oss_days-1stoffs.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table A4. Predictors of Length of OSS for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
* 3. Race and frpl
	reg oss_days $xrace $xfrpl $disc i.year i.grade if $keepreg, cluster(sgy)
	outreg2 using tableA04-predict_oss_days-1stoffs.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, Yes, Grade FE, Yes, School-Grade-Year FE, No) ///
			title("Table A4. Predictors of Length of OSS for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
	areg oss_days $xrace $xfrpl $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using tableA04-predict_oss_days-1stoffs.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table A4. Predictors of Length of OSS for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 		
	
	areg oss_days $xrace $xfrpl $disc susp_prioryr if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using tableA04-predict_oss_days-1stoffs.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table A4. Predictors of Length of OSS for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 	
			
* 4. Combined model		
	reg oss_days $xrace $xfrpl sped2 male $xachieve $disc i.year i.grade if $keepreg, cluster(sgy)
	outreg2 using tableA04-predict_oss_days-1stoffs.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, Yes, Grade FE, Yes, School-Grade-Year FE, No) ///
			title("Table A4. Predictors of Length of OSS for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 
			
	areg oss_days $xrace $xfrpl sped2 male $xachieve $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using tableA04-predict_oss_days-1stoffs.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table A4. Predictors of Length of OSS for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 

	areg oss_days $xrace $xfrpl sped2 male $xachieve susp_prioryr $disc if $keepreg, absorb(sgy) cluster(sgy)
	outreg2 using tableA04-predict_oss_days-1stoffs.xml ///
		, 	dec(3) label addtext(Disc. Controls, Yes, Year FE, No, Grade FE, No, School-Grade-Year FE, Yes) ///
			title("Table A4. Predictors of Length of OSS for First Offense (in Days)") ///
			keep($xrace $xfrpl sped2 male $xachieve susp_prioryr) 

//==============================================================================
// 15 - Table A5 & A6
//==============================================================================

//===========================================
//	#15A Initial data work
//===========================================
	use "$data\fights.dta", replace
	cd "$output"
	
	*set globals
	global base_race	black fight_count
	global base_frpl	always_dis_flg fight_count
	global vars_race	always_dis_flg never_dis_flg sped2 stud_male math_lag1 ela_lag1 math_miss ela_miss sped_miss 
	global vars_frpl	black other_race sped2 stud_male math_lag1 ela_lag1 math_miss ela_miss sped_miss 
	global if_stmt		if same_fight==2 & blk_wht==1
	global outreg1a		outreg using tableA5, se varlabels starloc(1) nole starlevels(10 5 1) t("Table A5: Discipline Gaps in Fights Between Black and White Students") basefont(roman fs12)
	global outreg1b		outreg using tableA6, se varlabels starloc(1) nole starlevels(10 5 1) t("Table A6: Discipline Gaps in Fights Between Poor and Non-Poor Students") basefont(roman fs12)
	global outreg1c		note("Note: * p<0.1; ** p<0.05; *** p<0.01") summstat(N \ r2)

//===========================================
//	#15B Table A5 - black white
//===========================================

	*num days no controls school FE
	xtreg days $base_race $if_stmt, fe i(group) cluster(group)
			$outreg1a ct("", "(1)") addrows("School FEs", "Yes" \ "Fight occurrence FEs", "No") $outreg1c replace

	*num days no controls incident FE
	xtreg days $base_race $if_stmt, fe i(incident_id) cluster(incident_id)
			$outreg1a ct("", "(2)") addrows("School FEs", "No" \ "Fight occurrence FEs", "Yes") $outreg1c merge

	*num days controls school FE
	xtreg days $base_race $vars_race $if_stmt, fe i(group) cluster(group)
			$outreg1a ct("", "(3)") addrows("School FEs", "Yes" \ "Fight occurrence FEs", "No") $outreg1c merge

	*num days controls incident FE
	xtreg days $base_race $vars_race $if_stmt, fe i(incident_id) cluster(incident_id)
			$outreg1a ct("", "(4)") addrows("School FEs", "No" \ "Fight occurrence FEs", "Yes") $outreg1c merge
	
	*more days no controls school FE
	xtreg more_days $base_race $if_stmt, fe i(group) cluster(group)
			$outreg1a ct("", "(5)") addrows("School FEs", "Yes" \ "Fight occurrence FEs", "No") $outreg1c merge

	*more days no controls incident FE
	xtreg more_days $base_race $if_stmt, fe i(incident_id) cluster(incident_id)
			$outreg1a ct("", "(6)") addrows("School FEs", "No" \ "Fight occurrence FEs", "Yes") $outreg1c merge

	*more days controls school FE
	xtreg more_days $base_race $vars_race $if_stmt, fe i(group) cluster(group)
			$outreg1a ct("", "(7)") addrows("School FEs", "Yes" \ "Fight occurrence FEs", "No") $outreg1c merge

	*more days controls incident FE
	xtreg more_days $base_race $vars_race $if_stmt, fe i(incident_id) cluster(incident_id)
			$outreg1a ct("", "(8)") addrows("School FEs", "No" \ "Fight occurrence FEs", "Yes") $outreg1c merge

//===========================================
//	#15C Table A6 - poor nonpoor
//===========================================

	*num days no controls school FE
	xtreg days $base_frpl $if_stmt, fe i(group) cluster(group)
			$outreg1b ct("", "(1)") addrows("School FEs", "Yes" \ "Fight occurrence FEs", "No") $outreg1c replace

	*num days no controls incident FE
	xtreg days $base_frpl $if_stmt, fe i(incident_id) cluster(incident_id)
			$outreg1b ct("", "(2)") addrows("School FEs", "No" \ "Fight occurrence FEs", "Yes") $outreg1c merge

	*num days controls school FE
	xtreg days $base_frpl $vars_frpl $if_stmt, fe i(group) cluster(group)
			$outreg1b ct("", "(3)") addrows("School FEs", "Yes" \ "Fight occurrence FEs", "No") $outreg1c merge

	*num days controls incident FE
	xtreg days $base_frpl $vars_frpl $if_stmt, fe i(incident_id) cluster(incident_id)
			$outreg1b ct("", "(4)") addrows("School FEs", "No" \ "Fight occurrence FEs", "Yes") $outreg1c merge
	
	*more days no controls school FE
	xtreg more_days $base_frpl $if_stmt, fe i(group) cluster(group)
			$outreg1b ct("", "(5)") addrows("School FEs", "Yes" \ "Fight occurrence FEs", "No") $outreg1c merge

	*more days no controls incident FE
	xtreg more_days $base_frpl $if_stmt, fe i(incident_id) cluster(incident_id)
			$outreg1b ct("", "(6)") addrows("School FEs", "No" \ "Fight occurrence FEs", "Yes") $outreg1c merge

	*more days controls school FE
	xtreg more_days $base_frpl $vars_frpl $if_stmt, fe i(group) cluster(group)
			$outreg1b ct("", "(7)") addrows("School FEs", "Yes" \ "Fight occurrence FEs", "No") $outreg1c merge

	*more days controls incident FE
	xtreg more_days $base_frpl $vars_frpl $if_stmt, fe i(incident_id) cluster(incident_id)
			$outreg1b ct("", "(8)") addrows("School FEs", "No" \ "Fight occurrence FEs", "Yes") $outreg1c merge

//==============================================================================
// 16 - Create Tables A7 & A8
//==============================================================================
//===========================================
//	#16A Initial data work
//===========================================
	
	use "$data\fights.dta", replace
	cd "$output"
	bysort incident_id: egen mode_disc_type=mode(disc_type)

	*set globals
	global base_race	black fight_count
	global base_frpl	always_dis_flg fight_count
	global vars_race	always_dis_flg never_dis_flg sped2 stud_male math_lag1 ela_lag1 math_miss ela_miss sped_miss 
	global vars_frpl	black other_race sped2 stud_male math_lag1 ela_lag1 math_miss ela_miss sped_miss 
	global if_race		if same_fight==2 & blk_wht==1 & mode_disc_type==2
	global if_frpl		if same_fight==2 & frp_non==1 & mode_disc_type==2
	global outreg1a		outreg using tableA7_panelA, se varlabels starloc(1) nole starlevels(10 5 1) t("Table A7 Panel A: Discipline Disparities in Fights Between Two Students - OSS Only") keep(black _cons) basefont(roman fs12)
	global outreg1b		outreg using tableA7_panelB, se varlabels starloc(1) nole starlevels(10 5 1) t("Table A7 Panel B: Discipline Disparities in Fights Between Two Students - OSS Only") keep(always_dis_flg _cons) basefont(roman fs12)
	global outreg1c		outreg using tableA8_panelA, se varlabels starloc(1) nole starlevels(10 5 1) t("Table A8 Panel A: Discipline Disparities in Fights Between Two Students - OSS Only") keep(black _cons) basefont(roman fs12)
	global outreg1d		outreg using tableA8_panelB, se varlabels starloc(1) nole starlevels(10 5 1) t("Table A8 Panel B: Discipline Disparities in Fights Between Two Students - OSS Only") keep(always_dis_flg _cons) basefont(roman fs12)
	global outreg1e		note("Note: * p<0.1; ** p<0.05; *** p<0.01") summstat(N \ r2)

//===========================================
//	#16B Table A7 Panel A
//===========================================

	*full sample
	xtreg days $base_race $if_race, fe i(group) cluster(group)
			$outreg1a ct("", "(1)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e replace
			
	xtreg days $base_race $vars_race $if_race, fe i(group) cluster(group)
			$outreg1a ct("", "(2)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge
	
	*first fight of the year
	xtreg days $base_race $if_race & prior_fights==0, fe i(group) cluster(group)
			$outreg1a ct("", "(3)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e merge

	xtreg days $base_race $vars_race $if_race & prior_fights==0, fe i(group) cluster(group)
			$outreg1a ct("", "(4)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge

	*first fight ever
	xtreg days $base_race number_years $if_race & no_prior==1, fe i(group) cluster(group)
			$outreg1a ct("", "(5)") addrows("Student controls", "No" \ "# of years in data control", "Yes") $outreg1e merge

	xtreg days $base_race number_years $vars_race $if_race & no_prior==1, fe i(group) cluster(group)
			$outreg1a ct("", "(6)") addrows("Student controls", "Yes" \ "# of years in data control", "Yes") $outreg1e merge

	*first suspension of the year is a fight
	xtreg days $base_race $if_race & use==1, fe i(group) cluster(group)
			$outreg1a ct("", "(7)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e merge

	xtreg days $base_race $vars_race $if_race & use==1, fe i(group) cluster(group)
			$outreg1a ct("", "(8)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge

	*first suspension ever is a fight
	xtreg days $base_race number_years $if_race & use2==1, fe i(group) cluster(group)
			$outreg1a ct("", "(9)") addrows("Student controls", "No" \ "# of years in data control", "Yes") $outreg1e merge

	xtreg days $base_race number_years $vars_race $if_race & use2==1, fe i(group) cluster(group)
			$outreg1a ct("", "(10)") addrows("Student controls", "Yes" \ "# of years in data control", "Yes") $outreg1e merge
	
//===========================================
//	#16C Table A7 Panel B
//===========================================

	*full sample
	xtreg days $base_frpl $if_frpl, fe i(group) cluster(group)
			$outreg1b ct("", "(1)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e replace
			
	xtreg days $base_frpl $vars_frpl $if_frpl, fe i(group) cluster(group)
			$outreg1b ct("", "(2)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge
	
	*first fight of the year
	xtreg days $base_frpl $if_frpl & prior_fights==0, fe i(group) cluster(group)
			$outreg1b ct("", "(3)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e merge

	xtreg days $base_frpl $vars_frpl $if_frpl & prior_fights==0, fe i(group) cluster(group)
			$outreg1b ct("", "(4)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge

	*first fight ever
	xtreg days $base_frpl number_years $if_frpl & no_prior==1, fe i(group) cluster(group)
			$outreg1b ct("", "(5)") addrows("Student controls", "No" \ "# of years in data control", "Yes") $outreg1e merge

	xtreg days $base_frpl number_years $vars_frpl $if_frpl & no_prior==1, fe i(group) cluster(group)
			$outreg1b ct("", "(6)") addrows("Student controls", "Yes" \ "# of years in data control", "Yes") $outreg1e merge

	*first suspension of the year is a fight
	xtreg days $base_frpl $if_frpl & use==1, fe i(group) cluster(group)
			$outreg1b ct("", "(7)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e merge

	xtreg days $base_frpl $vars_frpl $if_frpl & use==1, fe i(group) cluster(group)
			$outreg1b ct("", "(8)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge

	*first suspension ever is a fight
	xtreg days $base_frpl number_years $if_frpl & use2==1, fe i(group) cluster(group)
			$outreg1b ct("", "(9)") addrows("Student controls", "No" \ "# of years in data control", "Yes") $outreg1e merge

	xtreg days $base_frpl number_years $vars_frpl $if_frpl & use2==1, fe i(group) cluster(group)
			$outreg1b ct("", "(10)") addrows("Student controls", "Yes" \ "# of years in data control", "Yes") $outreg1e merge
	
//===========================================
//	#16D Table A8 Panel A
//===========================================

	*full sample
	xtreg more_days $base_race $if_stmt, fe i(group) cluster(group)
			$outreg1c ct("", "(1)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e replace
			
	xtreg more_days $base_race $vars_race $if_stmt, fe i(group) cluster(group)
			$outreg1c ct("", "(2)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge
	
	*first fight of the year
	xtreg more_days $base_race $if_stmt & prior_fights==0, fe i(group) cluster(group)
			$outreg1c ct("", "(3)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e merge

	xtreg more_days $base_race $vars_race $if_stmt & prior_fights==0, fe i(group) cluster(group)
			$outreg1c ct("", "(4)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge

	*first fight ever
	xtreg more_days $base_race number_years $if_stmt & no_prior==1, fe i(group) cluster(group)
			$outreg1c ct("", "(5)") addrows("Student controls", "No" \ "# of years in data control", "Yes") $outreg1e merge

	xtreg more_days $base_race number_years $vars_race $if_stmt & no_prior==1, fe i(group) cluster(group)
			$outreg1c ct("", "(6)") addrows("Student controls", "Yes" \ "# of years in data control", "Yes") $outreg1e merge

	*first suspension of the year is a fight
	xtreg more_days $base_race $if_stmt & use==1, fe i(group) cluster(group)
			$outreg1c ct("", "(7)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e merge

	xtreg more_days $base_race $vars_race $if_stmt & use==1, fe i(group) cluster(group)
			$outreg1c ct("", "(8)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge

	*first suspension ever is a fight
	xtreg more_days $base_race number_years $if_stmt & use2==1, fe i(group) cluster(group)
			$outreg1c ct("", "(9)") addrows("Student controls", "No" \ "# of years in data control", "Yes") $outreg1e merge

	xtreg more_days $base_race number_years $vars_race $if_stmt & use2==1, fe i(group) cluster(group)
			$outreg1c ct("", "(10)") addrows("Student controls", "Yes" \ "# of years in data control", "Yes") $outreg1e merge
	
//===========================================
//	#16E Table A8 Panel B
//===========================================

	*full sample
	xtreg more_days $base_frpl $if_stmt, fe i(group) cluster(group)
			$outreg1d ct("", "(1)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e replace
			
	xtreg more_days $base_frpl $vars_frpl $if_stmt, fe i(group) cluster(group)
			$outreg1d ct("", "(2)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge
	
	*first fight of the year
	xtreg more_days $base_frpl $if_stmt & prior_fights==0, fe i(group) cluster(group)
			$outreg1d ct("", "(3)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e merge

	xtreg more_days $base_frpl $vars_frpl $if_stmt & prior_fights==0, fe i(group) cluster(group)
			$outreg1d ct("", "(4)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge

	*first fight ever
	xtreg more_days $base_frpl number_years $if_stmt & no_prior==1, fe i(group) cluster(group)
			$outreg1d ct("", "(5)") addrows("Student controls", "No" \ "# of years in data control", "Yes") $outreg1e merge

	xtreg more_days $base_frpl number_years $vars_frpl $if_stmt & no_prior==1, fe i(group) cluster(group)
			$outreg1d ct("", "(6)") addrows("Student controls", "Yes" \ "# of years in data control", "Yes") $outreg1e merge

	*first suspension of the year is a fight
	xtreg more_days $base_frpl $if_stmt & use==1, fe i(group) cluster(group)
			$outreg1d ct("", "(7)") addrows("Student controls", "No" \ "# of years in data control", "No") $outreg1e merge

	xtreg more_days $base_frpl $vars_frpl $if_stmt & use==1, fe i(group) cluster(group)
			$outreg1d ct("", "(8)") addrows("Student controls", "Yes" \ "# of years in data control", "No") $outreg1e merge

	*first suspension ever is a fight
	xtreg more_days $base_frpl number_years $if_stmt & use2==1, fe i(group) cluster(group)
			$outreg1d ct("", "(9)") addrows("Student controls", "No" \ "# of years in data control", "Yes") $outreg1e merge

	xtreg more_days $base_frpl number_years $vars_frpl $if_stmt & use2==1, fe i(group) cluster(group)
			$outreg1d ct("", "(10)") addrows("Student controls", "Yes" \ "# of years in data control", "Yes") $outreg1e merge

//==============================================================================
// 17 - Create Table A9
//==============================================================================
//===========================================
//	#17A Initial data work
//===========================================
*sensitivity analysis to alternate censoring on max days and gaps
	
	use "$data\fights.dta", replace
	cd "$output"
	*create maximum days indicator
	bysort incident_id: egen max_days=max(days)
	*create gap indicators
	gen diff_days1=max_days-min_days
	gen diff_ind=0
	replace diff_ind=3 if diff_days1>2
	replace diff_ind=5 if diff_days1>4

//===========================================
//	#17B Regressions
//===========================================

foreach x in 5 10 20 {
foreach y in 0 3 5 {
	*Black white
	*full sample
	xtreg days black fight_count if same_fight==2 & blk_wht==1 & max_days<=`x' & diff_ind<=`y', fe i(group) cluster(group)
		outreg using fight_race_`x'_`y', se varlabels starloc(1) nole starlevels(10 5 1) ///
		t("Table X: Discipline Disparities by Race in Fights Between Two Students - Censored at `x' Days and `y' Gaps") basefont(roman fs12) ///
		ct("", "(1)") /// 
		note("Note: * p<0.1; ** p<0.05; *** p<0.01") summstat(N \ r2) replace
	*first suspension of the year	
	xtreg days black if same_fight==2 & blk_wht==1 & use==1 & max_days<=`x' & diff_ind<=`y', fe i(group) cluster(group)
		outreg using fight_race_`x'_`y', se varlabels starloc(1) nole starlevels(10 5 1) ///
		keep(black _cons) basefont(roman fs12) ct("", "(7)") /// 
		note("Note: * p<0.1; ** p<0.05; *** p<0.01") summstat(N \ r2) merge

	*poor non-poor
	xtreg days always_dis_flg fight_count if same_fight==2 & frp_non==1 & max_days<=`x' & diff_ind<=`y', fe i(group) cluster(group)
		outreg using fight_frpl_`x'_`y', se varlabels starloc(1) nole starlevels(10 5 1) ///
		t("Table X: Discipline Disparities by Race in Fights Between Two Students - Censored at `x' Days and `y' Gaps") basefont(roman fs12) ///
		ct("", "(1)") /// 
		note("Note: * p<0.1; ** p<0.05; *** p<0.01") summstat(N \ r2) replace
	*first suspension of the year	
	xtreg days always_dis_flg if same_fight==2 & frp_non==1 & use==1 & max_days<=`x' & diff_ind<=`y', fe i(group) cluster(group)
		outreg using fight_frpl_`x'_`y', se varlabels starloc(1) nole starlevels(10 5 1) ///
		keep(always_dis_flg _cons) basefont(roman fs12) ct("", "(7)") /// 
		note("Note: * p<0.1; ** p<0.05; *** p<0.01") summstat(N \ r2) merge
}
}
			
//==============================================================================
// 18 - Create Table A10
//==============================================================================
//===========================================
//	#18A Initial data work
//===========================================
	*same as Table 7 but limiting on first suspension of the year being for a fight for both participants
	use "$data\fights.dta", replace
	cd "$output"
			
	keep if use==1
	***BLACK and WHITE by groups***
	*black white
	gen     totalb_w=0
	replace totalb_w=1 if stud_one_eth==1 & stud_two_eth==2
	replace totalb_w=1 if stud_one_eth==2 & stud_two_eth==1

	gen     sameb_w=0
	replace sameb_w=1 if totalb_w==1 & stud_one_days==stud_two_days

	gen     student1_moreb_w=0
	replace student1_moreb_w=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days
	replace student1_moreb_w=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days

	gen     student2_moreb_w=0
	replace student2_moreb_w=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days
	replace student2_moreb_w=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days

	*black black*
	gen     totalb_b=0
	replace totalb_b=1 if stud_one_eth==1 & stud_two_eth==1

	gen     sameb_b=0
	replace sameb_b = 1 if totalb_b==1 & stud_one_days==stud_two_days

	*white white*
	gen     totalw_w=0
	replace totalw_w=1 if stud_one_eth==2 & stud_two_eth==2

	gen     samew_w=0
	replace samew_w = 1 if totalw_w==1 & stud_one_days==stud_two_days

	*FRL Non FRL*
	gen     totalp_np = 0
	replace totalp_np=1 if stud_one_frpl==1 & stud_two_non==1
	replace totalp_np=1 if stud_one_non==1 & stud_two_frpl==1

	gen     samep_np=0
	replace samep_np=1 if totalp_np==1 & stud_one_days==stud_two_days

	gen     student1_morep_np=0
	replace student1_morep_np=1 if stud_one_frpl==1 & stud_two_non==1 & stud_one_days>stud_two_days
	replace student1_morep_np=1 if stud_one_non==1 & stud_two_frpl==1 & stud_one_days<stud_two_days

	gen     student2_morep_np=0
	replace student2_morep_np=1 if stud_one_frpl==1 & stud_two_non==1 & stud_one_days<stud_two_days
	replace student2_morep_np=1 if stud_one_non==1 & stud_two_frpl==1 & stud_one_days>stud_two_days

	*poor poor*
	gen     totalp_p=0
	replace totalp_p=1 if stud_one_frpl==1 & stud_two_frpl==1

	gen     samep_p=0
	replace samep_p = 1 if totalp_p==1 & stud_one_days==stud_two_days

	*nonpoor nonpoor*
	gen     totalnp_np=0
	replace totalnp_np=1 if stud_one_non==1 & stud_two_non==1

	gen     samenp_np=0
	replace samenp_np = 1 if totalnp_np==1 & stud_one_days==stud_two_days

	*male female*
	gen     totalm_f = 0
	replace totalm_f = 1 if stud_one_male!=stud_two_male

	gen     samem_f = 0
	replace samem_f = 1 if totalm_f==1 & stud_one_days==stud_two_days

	gen     student1_morem_f = 0
	replace student1_morem_f = 1 if totalm_f==1 & stud_one_male==1 & stud_two_male==0 & stud_one_days>stud_two_days
	replace student1_morem_f = 1 if totalm_f==1 & stud_one_male==0 & stud_two_male==1 & stud_one_days<stud_two_days

	gen     student2_morem_f = 0
	replace student2_morem_f = 1 if totalm_f==1 & stud_one_male==1 & stud_two_male==0 & stud_one_days<stud_two_days
	replace student2_morem_f = 1 if totalm_f==1 & stud_one_male==0 & stud_two_male==1 & stud_one_days>stud_two_days

	*male male*
	gen     totalm_m=0
	replace totalm_m=1 if stud_one_male==1 & stud_two_male==1

	gen     samem_m=0
	replace samem_m= 1 if totalm_m==1 & stud_one_days==stud_two_days

	*female female*
	gen     totalf_f=0
	replace totalf_f=1 if stud_one_male==0 & stud_two_male==0

	gen     samef_f=0
	replace samef_f = 1 if totalf_f==1 & stud_one_days==stud_two_days

	*high score low score*
	gen     totall_h = 0
	replace totall_h=1 if stud_one_high==1 & stud_two_low==1
	replace totall_h=1 if stud_one_low==1 & stud_two_high==1

	gen     samel_h=0
	replace samel_h=1 if totall_h==1 & stud_one_days==stud_two_days

	gen     student1_morel_h=0
	replace student1_morel_h=1 if stud_one_high==1 & stud_two_low==1 & stud_one_days<stud_two_days
	replace student1_morel_h=1 if stud_one_low==1 & stud_two_high==1 & stud_one_days>stud_two_days

	gen     student2_morel_h=0
	replace student2_morel_h=1 if stud_one_high==1 & stud_two_low==1 & stud_one_days>stud_two_days
	replace student2_morel_h=1 if stud_one_low==1 & stud_two_high==1 & stud_one_days<stud_two_days

	*low low*
	gen     totall_l=0
	replace totall_l=1 if stud_one_low==1 & stud_two_low==1
	gen     samel_l=0
	replace samel_l = 1 if totall_l==1 & stud_one_days==stud_two_days

	*high high*
	gen     totalh_h=0
	replace totalh_h=1 if stud_one_high==1 & stud_two_high==1
	gen     sameh_h=0
	replace sameh_h = 1 if totalh_h==1 & stud_one_days==stud_two_days

	*black poor white poor*
	gen     totalbp_wp = 0
	replace totalbp_wp = 1 if stud_one_eth==1 & stud_one_frpl==1 & stud_two_eth==2 & stud_two_frpl==1
	replace totalbp_wp = 1 if stud_one_eth==2 & stud_one_frpl==1 & stud_two_eth==1 & stud_two_frpl==1

	gen     samebp_wp = 0 if totalbp_wp==1
	replace samebp_wp = 1 if totalbp_wp==1 & stud_one_days==stud_two_days

	gen     student1_morebp_wp = 0 if totalbp_wp==1
	replace student1_morebp_wp = 1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbp_wp==1
	replace student1_morebp_wp = 1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbp_wp==1

	gen     student2_morebp_wp = 0 if totalbp_wp==1
	replace student2_morebp_wp = 1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbp_wp==1
	replace student2_morebp_wp = 1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbp_wp==1

	*black nonpoor white nonpoor*
	gen     totalbnp_wnp = 0
	replace totalbnp_wnp = 1 if stud_one_eth==1 & stud_one_non==1 & stud_two_eth==2 & stud_two_non==1
	replace totalbnp_wnp = 1 if stud_one_eth==2 & stud_one_non==1 & stud_two_eth==1 & stud_two_non==1

	gen     samebnp_wnp = 0 if totalbnp_wnp==1
	replace samebnp_wnp = 1 if totalbnp_wnp==1 & stud_one_days==stud_two_days

	gen     student1_morebnp_wnp = 0 if totalbnp_wnp==1
	replace student1_morebnp_wnp = 1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbnp_wnp==1
	replace student1_morebnp_wnp = 1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbnp_wnp==1

	gen     student2_morebnp_wnp = 0 if totalbnp_wnp==1
	replace student2_morebnp_wnp = 1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbnp_wnp==1
	replace student2_morebnp_wnp = 1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbnp_wnp==1

	*black poor white nonpoor*
	gen     totalbp_wnp = 0
	replace totalbp_wnp = 1 if stud_one_eth==1 & stud_one_frpl==1 & stud_two_eth==2 & stud_two_non==1
	replace totalbp_wnp = 1 if stud_one_eth==2 & stud_one_non==1 & stud_two_eth==1 & stud_two_frpl==1

	gen     samebp_wnp = 0 if totalbp_wnp==1
	replace samebp_wnp = 1 if totalbp_wnp==1 & stud_one_days==stud_two_days

	gen     student1_morebp_wnp = 0 if totalbp_wnp==1
	replace student1_morebp_wnp = 1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbp_wnp==1
	replace student1_morebp_wnp = 1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbp_wnp==1

	gen     student2_morebp_wnp = 0 if totalbp_wnp==1
	replace student2_morebp_wnp = 1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbp_wnp==1
	replace student2_morebp_wnp = 1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbp_wnp==1

	*black nonpoor white poor*
	gen     totalbnp_wp = 0
	replace totalbnp_wp = 1 if stud_one_eth==1 & stud_one_non==1 & stud_two_eth==2 & stud_two_frpl==1
	replace totalbnp_wp = 1 if stud_one_eth==2 & stud_one_frpl==1 & stud_two_eth==1 & stud_two_non==1

	gen     samebnp_wp = 0 if totalbnp_wp==1
	replace samebnp_wp = 1 if totalbnp_wp==1 & stud_one_days==stud_two_days

	gen     student1_morebnp_wp = 0 if totalbnp_wp==1
	replace student1_morebnp_wp = 1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbnp_wp==1
	replace student1_morebnp_wp = 1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbnp_wp==1

	gen     student2_morebnp_wp = 0 if totalbnp_wp==1
	replace student2_morebnp_wp = 1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbnp_wp==1
	replace student2_morebnp_wp = 1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbnp_wp==1

	*black male white male*
	gen     totalbm_wm=0
	replace totalbm_wm=1 if stud_one_eth==1 & stud_one_male==1 & stud_two_eth==2 & stud_two_male==1
	replace totalbm_wm=1 if stud_one_eth==2 & stud_one_male==1 & stud_two_eth==1 & stud_two_male==1

	gen     samebm_wm=0 if totalbm_wm==1
	replace samebm_wm=1 if totalbm_wm==1 & stud_one_days==stud_two_days

	gen     student1_morebm_wm=0 if totalbm_wm==1
	replace student1_morebm_wm=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbm_wm==1
	replace student1_morebm_wm=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbm_wm==1

	gen     student2_morebm_wm=0 if totalbm_wm==1
	replace student2_morebm_wm=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbm_wm==1
	replace student2_morebm_wm=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbm_wm==1

	*black female white female*
	gen     totalbf_wf=0
	replace totalbf_wf=1 if stud_one_eth==1 & stud_one_male==0 & stud_two_eth==2 & stud_two_male==0
	replace totalbf_wf=1 if stud_one_eth==2 & stud_one_male==0 & stud_two_eth==1 & stud_two_male==0

	gen     samebf_wf=0 if totalbf_wf==1
	replace samebf_wf=1 if totalbf_wf==1 & stud_one_days==stud_two_days

	gen     student1_morebf_wf=0 if totalbf_wf==1
	replace student1_morebf_wf=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbf_wf==1
	replace student1_morebf_wf=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbf_wf==1

	gen     student2_morebf_wf=0 if totalbf_wf==1
	replace student2_morebf_wf=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbf_wf==1
	replace student2_morebf_wf=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbf_wf==1

	*black male/white female
	gen     totalbm_wf=0
	replace totalbm_wf=1 if stud_one_eth==1 & stud_one_male==1 & stud_two_eth==2 & stud_two_male==0
	replace totalbm_wf=1 if stud_one_eth==2 & stud_one_male==0 & stud_two_eth==1 & stud_two_male==1

	gen     samebm_wf=0 if totalbm_wf==1
	replace samebm_wf=1 if totalbm_wf==1 & stud_one_days==stud_two_days

	gen     student1_morebm_wf=0 if totalbm_wf==1
	replace student1_morebm_wf=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbm_wf==1
	replace student1_morebm_wf=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbm_wf==1

	gen     student2_morebm_wf=0 if totalbm_wf==1
	replace student2_morebm_wf=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbm_wf==1
	replace student2_morebm_wf=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbm_wf==1

	*black girl/white boy
	gen     totalbf_wm=0
	replace totalbf_wm=1 if stud_one_eth==1 & stud_one_male==0 & stud_two_eth==2 & stud_two_male==1
	replace totalbf_wm=1 if stud_one_eth==2 & stud_one_male==1 & stud_two_eth==1 & stud_two_male==0

	gen     samebf_wm=0 if totalbf_wm==1
	replace samebf_wm=1 if totalbf_wm==1 & stud_one_days==stud_two_days

	gen     student1_morebf_wm=0 if totalbf_wm==1
	replace student1_morebf_wm=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbf_wm==1
	replace student1_morebf_wm=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbf_wm==1

	gen     student2_morebf_wm=0 if totalbf_wm==1
	replace student2_morebf_wm=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbf_wm==1
	replace student2_morebf_wm=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbf_wm==1

	*black high white high
	gen     totalbh_wh=0
	replace totalbh_wh=1 if stud_one_eth==1 & stud_one_high==1 & stud_two_eth==2 & stud_two_high==1
	replace totalbh_wh=1 if stud_one_eth==2 & stud_one_high==1 & stud_two_eth==1 & stud_two_high==1

	gen     samebh_wh=0 if totalbh_wh==1
	replace samebh_wh=1 if totalbh_wh==1 & stud_one_days==stud_two_days

	gen     student1_morebh_wh=0 if totalbh_wh==1
	replace student1_morebh_wh=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbh_wh==1
	replace student1_morebh_wh=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbh_wh==1

	gen     student2_morebh_wh=0 if totalbh_wh==1
	replace student2_morebh_wh=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbh_wh==1
	replace student2_morebh_wh=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbh_wh==1

	*black low white low
	gen     totalbl_wl=0
	replace totalbl_wl=1 if stud_one_eth==1 & stud_one_low==1 & stud_two_eth==2 & stud_two_low==1
	replace totalbl_wl=1 if stud_one_eth==2 & stud_one_low==1 & stud_two_eth==1 & stud_two_low==1

	gen     samebl_wl=0 if totalbl_wl==1
	replace samebl_wl=1 if totalbl_wl==1 & stud_one_days==stud_two_days

	gen     student1_morebl_wl=0 if totalbl_wl==1
	replace student1_morebl_wl=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbl_wl==1
	replace student1_morebl_wl=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbl_wl==1

	gen     student2_morebl_wl=0 if totalbl_wl==1
	replace student2_morebl_wl=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbl_wl==1
	replace student2_morebl_wl=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbl_wl==1

	*black high white low
	gen     totalbh_wl=0
	replace totalbh_wl=1 if stud_one_eth==1 & stud_one_high==1 & stud_two_eth==2 & stud_two_low==1
	replace totalbh_wl=1 if stud_one_eth==2 & stud_one_low==1 & stud_two_eth==1 & stud_two_high==1

	gen     samebh_wl=0 if totalbh_wl==1
	replace samebh_wl=1 if totalbh_wl==1 & stud_one_days==stud_two_days

	gen     student1_morebh_wl=0 if totalbh_wl==1
	replace student1_morebh_wl=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbh_wl==1
	replace student1_morebh_wl=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbh_wl==1

	gen     student2_morebh_wl=0 if totalbh_wl==1
	replace student2_morebh_wl=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbh_wl==1
	replace student2_morebh_wl=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbh_wl==1

	*black low white high
	gen     totalbl_wh=0
	replace totalbl_wh=1 if stud_one_eth==1 & stud_one_low==1 & stud_two_eth==2 & stud_two_high==1
	replace totalbl_wh=1 if stud_one_eth==2 & stud_one_high==1 & stud_two_eth==1 & stud_two_low==1

	gen     samebl_wh=0 if totalbl_wh==1
	replace samebl_wh=1 if totalbl_wh==1 & stud_one_days==stud_two_days

	gen     student1_morebl_wh=0 if totalbl_wh==1
	replace student1_morebl_wh=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days>stud_two_days & totalbl_wh==1
	replace student1_morebl_wh=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days<stud_two_days & totalbl_wh==1

	gen     student2_morebl_wh=0 if totalbl_wh==1
	replace student2_morebl_wh=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_days<stud_two_days & totalbl_wh==1
	replace student2_morebl_wh=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_days>stud_two_days & totalbl_wh==1

	*poor male non-poor male
	gen totalpm_npm=0
	replace totalpm_npm=1 if stud_one_frpl==1 & stud_one_male==1 & stud_two_non==1 & stud_two_male==1
	replace totalpm_npm=1 if stud_one_non==1 & stud_one_male==1 & stud_two_frpl==1 & stud_two_male==1

	gen samepm_npm=0 if totalpm_npm==1
	replace samepm_npm=1 if totalpm_npm==1 & stud_one_days==stud_two_days

	gen     student1_morepm_npm=0 if totalpm_npm==1
	replace student1_morepm_npm=1 if stud_one_frpl==1 & stud_two_non==1 & stud_one_days>stud_two_days & totalpm_npm==1
	replace student1_morepm_npm=1 if stud_two_frpl==1 & stud_one_non==1 & stud_one_days<stud_two_days & totalpm_npm==1

	gen     student2_morepm_npm=0 if totalpm_npm==1
	replace student2_morepm_npm=1 if stud_one_frpl==1 & stud_two_non==1 & stud_one_days<stud_two_days & totalpm_npm==1
	replace student2_morepm_npm=1 if stud_two_frpl==1 & stud_one_non==1 & stud_one_days>stud_two_days & totalpm_npm==1

	*poor female non-poor female
	gen totalpf_npf=0
	replace totalpf_npf=1 if stud_one_frpl==1 & stud_one_male==0 & stud_two_non==1 & stud_two_male==0
	replace totalpf_npf=1 if stud_one_non==1 & stud_one_male==0 & stud_two_frpl==1 & stud_two_male==0

	gen samepf_npf=0 if totalpf_npf==1
	replace samepf_npf=1 if totalpf_npf==1 & stud_one_days==stud_two_days

	gen     student1_morepf_npf=0 if totalpf_npf==1
	replace student1_morepf_npf=1 if stud_one_frpl==1 & stud_two_non==1 & stud_one_days>stud_two_days & totalpf_npf==1
	replace student1_morepf_npf=1 if stud_two_frpl==1 & stud_one_non==1 & stud_one_days<stud_two_days & totalpf_npf==1

	gen     student2_morepf_npf=0 if totalpf_npf==1
	replace student2_morepf_npf=1 if stud_one_frpl==1 & stud_two_non==1 & stud_one_days<stud_two_days & totalpf_npf==1
	replace student2_morepf_npf=1 if stud_two_frpl==1 & stud_one_non==1 & stud_one_days>stud_two_days & totalpf_npf==1

	*poor male non-poor female
	gen totalpm_npf=0
	replace totalpm_npf=1 if stud_one_frpl==1 & stud_one_male==1 & stud_two_non==1 & stud_two_male==0
	replace totalpm_npf=1 if stud_one_non==1 & stud_one_male==0 & stud_two_frpl==1 & stud_two_male==1

	gen samepm_npf=0 if totalpm_npf==1
	replace samepm_npf=1 if totalpm_npf==1 & stud_one_days==stud_two_days

	gen     student1_morepm_npf=0 if totalpm_npf==1
	replace student1_morepm_npf=1 if stud_one_frpl==1 & stud_one_male==1 & stud_one_days>stud_two_days & totalpm_npf==1
	replace student1_morepm_npf=1 if stud_two_frpl==1 & stud_two_male==1 & stud_one_days<stud_two_days & totalpm_npf==1

	gen     student2_morepm_npf=0 if totalpm_npf==1
	replace student2_morepm_npf=1 if stud_one_frpl==1 & stud_one_male==1 & stud_one_days<stud_two_days & totalpm_npf==1
	replace student2_morepm_npf=1 if stud_two_frpl==1 & stud_two_male==1 & stud_one_days>stud_two_days & totalpm_npf==1

	*poor female non-poor male
	gen totalpf_npm=0
	replace totalpf_npm=1 if stud_one_frpl==1 & stud_one_male==0 & stud_two_non==1 & stud_two_male==1
	replace totalpf_npm=1 if stud_one_non==1 & stud_one_male==1 & stud_two_frpl==1 & stud_two_male==0

	gen samepf_npm=0 if totalpf_npm==1
	replace samepf_npm=1 if totalpf_npm==1 & stud_one_days==stud_two_days

	gen     student1_morepf_npm=0 if totalpf_npm==1
	replace student1_morepf_npm=1 if stud_one_frpl==1 & stud_one_male==0 & stud_one_days>stud_two_days & totalpf_npm==1
	replace student1_morepf_npm=1 if stud_two_frpl==1 & stud_two_male==0 & stud_one_days<stud_two_days & totalpf_npm==1

	gen     student2_morepf_npm=0 if totalpf_npm==1
	replace student2_morepf_npm=1 if stud_one_frpl==1 & stud_one_male==0 & stud_one_days<stud_two_days & totalpf_npm==1
	replace student2_morepf_npm=1 if stud_two_frpl==1 & stud_two_male==0 & stud_one_days>stud_two_days & totalpf_npm==1

	*poor high non-poor high
	gen totalph_nph=0
	replace totalph_nph=1 if stud_one_frpl==1 & stud_one_high==1 & stud_two_non==1 & stud_two_high==1
	replace totalph_nph=1 if stud_one_non==1 & stud_one_high==1 & stud_two_frpl==1 & stud_two_high==1

	gen sameph_nph=0 if totalph_nph==1
	replace sameph_nph=1 if totalph_nph==1 & stud_one_days==stud_two_days

	gen     student1_moreph_nph=0 if totalph_nph==1
	replace student1_moreph_nph=1 if stud_one_frpl==1 & stud_one_high==1 & stud_one_days>stud_two_days & totalph_nph==1
	replace student1_moreph_nph=1 if stud_two_frpl==1 & stud_two_high==1 & stud_one_days<stud_two_days & totalph_nph==1

	gen     student2_moreph_nph=0 if totalph_nph==1
	replace student2_moreph_nph=1 if stud_one_frpl==1 & stud_one_high==1 & stud_one_days<stud_two_days & totalph_nph==1
	replace student2_moreph_nph=1 if stud_two_frpl==1 & stud_two_high==1 & stud_one_days>stud_two_days & totalph_nph==1

	*poor low non-poor low
	gen totalpl_npl=0
	replace totalpl_npl=1 if stud_one_frpl==1 & stud_one_low==1 & stud_two_non==1 & stud_two_low==1
	replace totalpl_npl=1 if stud_one_non==1 & stud_one_low==1 & stud_two_frpl==1 & stud_two_low==1

	gen samepl_npl=0 if totalpl_npl==1
	replace samepl_npl=1 if totalpl_npl==1 & stud_one_days==stud_two_days

	gen     student1_morepl_npl=0 if totalpl_npl==1
	replace student1_morepl_npl=1 if stud_one_frpl==1 & stud_one_low==1 & stud_one_days>stud_two_days & totalpl_npl==1
	replace student1_morepl_npl=1 if stud_two_frpl==1 & stud_two_low==1 & stud_one_days<stud_two_days & totalpl_npl==1

	gen     student2_morepl_npl=0 if totalpl_npl==1
	replace student2_morepl_npl=1 if stud_one_frpl==1 & stud_one_low==1 & stud_one_days<stud_two_days & totalpl_npl==1
	replace student2_morepl_npl=1 if stud_two_frpl==1 & stud_two_low==1 & stud_one_days>stud_two_days & totalpl_npl==1

	*poor high non-poor low
	gen totalph_npl=0
	replace totalph_npl=1 if stud_one_frpl==1 & stud_one_high==1 & stud_two_non==1 & stud_two_low==1
	replace totalph_npl=1 if stud_one_non==1 & stud_one_low==1 & stud_two_frpl==1 & stud_two_high==1

	gen sameph_npl=0 if totalph_npl==1
	replace sameph_npl=1 if totalph_npl==1 & stud_one_days==stud_two_days

	gen     student1_moreph_npl=0 if totalph_npl==1
	replace student1_moreph_npl=1 if stud_one_frpl==1 & stud_one_high==1 & stud_one_days>stud_two_days & totalph_npl==1
	replace student1_moreph_npl=1 if stud_two_frpl==1 & stud_two_high==1 & stud_one_days<stud_two_days & totalph_npl==1

	gen     student2_moreph_npl=0 if totalph_npl==1
	replace student2_moreph_npl=1 if stud_one_frpl==1 & stud_one_high==1 & stud_one_days<stud_two_days & totalph_npl==1
	replace student2_moreph_npl=1 if stud_two_frpl==1 & stud_two_high==1 & stud_one_days>stud_two_days & totalph_npl==1

	*poor low non-poor high
	gen totalpl_nph=0
	replace totalpl_nph=1 if stud_one_frpl==1 & stud_one_low==1 & stud_two_non==1 & stud_two_high==1
	replace totalpl_nph=1 if stud_one_non==1 & stud_one_high==1 & stud_two_frpl==1 & stud_two_low==1

	gen samepl_nph=0 if totalpl_nph==1
	replace samepl_nph=1 if totalpl_nph==1 & stud_one_days==stud_two_days

	gen     student1_morepl_nph=0 if totalpl_nph==1
	replace student1_morepl_nph=1 if stud_one_frpl==1 & stud_one_low==1 & stud_one_days>stud_two_days & totalpl_nph==1
	replace student1_morepl_nph=1 if stud_two_frpl==1 & stud_two_low==1 & stud_one_days<stud_two_days & totalpl_nph==1

	gen     student2_morepl_nph=0 if totalpl_nph==1
	replace student2_morepl_nph=1 if stud_one_frpl==1 & stud_one_low==1 & stud_one_days<stud_two_days & totalpl_nph==1
	replace student2_morepl_nph=1 if stud_two_frpl==1 & stud_two_low==1 & stud_one_days>stud_two_days & totalpl_nph==1
			
//===========================================
//	#18B Gererates Table A10
//===========================================

	collapse (sum) totalb_w-student2_morepl_nph
	gen check=1
	reshape long total same student1_more student2_more, i(check) j(comparison) s
	gen order=0
	replace order =1 if comparison=="b_w"
	replace order =2 if comparison=="b_b"
	replace order =3 if comparison=="w_w"
	replace order =4 if comparison=="p_np"
	replace order =5 if comparison=="p_p"
	replace order =6 if comparison=="np_np"
	replace order =7 if comparison=="m_f"
	replace order =8 if comparison=="m_m"
	replace order =9 if comparison=="f_f"
	replace order =10 if comparison=="l_h"
	replace order =11 if comparison=="l_l"
	replace order =12 if comparison=="h_h"
	replace order =13 if comparison=="bp_wp"
	replace order =14 if comparison=="bnp_wnp"
	replace order =15 if comparison=="bp_wnp"
	replace order =16 if comparison=="bnp_wp"
	replace order =17 if comparison=="bm_wm"
	replace order =18 if comparison=="bf_wf"
	replace order =19 if comparison=="bm_wf"
	replace order =20 if comparison=="bf_wm"
	replace order =21 if comparison=="bh_wh"
	replace order =22 if comparison=="bl_wl"
	replace order =23 if comparison=="bh_wl"
	replace order =24 if comparison=="bl_wh"
	replace order =25 if comparison=="pm_npm"
	replace order =26 if comparison=="pf_npf"
	replace order =27 if comparison=="pm_npf"
	replace order =28 if comparison=="pf_npm"
	replace order =29 if comparison=="ph_nph"
	replace order =30 if comparison=="pl_npl"
	replace order =31 if comparison=="ph_npl"
	replace order =32 if comparison=="pl_nph"


	sort order
	gen denominator1=(student1+student2)/(2*total)
	gen denominator2=1-denominator1 
	gen denominator3=denominator1*denominator2*(1/total+1/total)
	gen denominator=sqrt(denominator3)
	replace student1=student1/total
	replace student2=student2/total
	replace same=same/total
	gen numerator=student1-student2
	gen zscore=numerator/denominator 
	drop denominator1-numerator
	export excel using tableA10.xlsx, replace

//==============================================================================
// 19 - Create Table A11
//==============================================================================
//===========================================
//	#19A Initial data work
//===========================================
	
	use "$data\fights.dta", replace
	cd "$output"
	*amdin groups
	gen admin=0
	replace admin=1 if pct_wht_princ>=.75
	replace admin=2 if pct_wht_princ>.25 & pct_wht_princ<.75
	replace admin=3 if pct_wht_princ<=.25
	*school race groups
	gen race=0
	replace race=1 if tot_white/tot_studs>=.75
	replace race=3 if tot_white/tot_studs<=.25
	replace race=2 if race==0
	*school frpl groups
	gen frp1=lunch_frpl/tot_studs
	xtile frp=frp1, nq(3)
	replace frp=0 if frp==1
	replace frp=1 if frp==3
	replace frp=3 if frp==0
	drop frp1
	*school locale groups
	replace urbanicity=0
	replace urbanicity=1 if urban==1
	replace urbanicity=2 if suburban==1
	replace urbanicity=3 if rural==1
	bysort sch_id: egen mode_urban=mode(urbanicity)
	replace urbanicity=mode_urban if urbanicity==0
	drop mode_urban
	*school level groups
	gen grade_group=0
	replace grade_group=1 if stud_1_indicator==1 & grade<6
	replace grade_group=2 if stud_1_indicator==1 & grade>5 & grade<9
	replace grade_group=3 if stud_1_indicator==1 & grade>8
	sort incident_id stud_1_indicator
	replace grade_group=grade_group[_n+1] if incident_id==incident_id[_n+1]
	
//===========================================
//	#19B Create Table A11
//===========================================

	foreach x of varlist admin race frp urbanicity grade_group {
	foreach y in 1 2 3 {

	xtreg days always_dis_flg fight_count if same_fight==2 & blk_wht==1 & `x'==`y', fe i(group) cluster(group)
		outreg using tableA11`x'_`y', se varlabels starloc(1) nole starlevels(10 5 1) ///
		t("Table X: Discipline Disparities in Fights Between Two Students - Heterogeneity Analyses `x' `y'") basefont(roman fs12) ///
		keep(black) ct("", "(1)") /// 
		addrows("Student Controls", "No" \ "School FEs", "Yes") ///
		note("Note: * p<0.1; ** p<0.05; *** p<0.01") summstat(N \ r2) replace
		
	xtreg days always_dis_flg if same_fight==2 & blk_wht==1 & use==1 & `x'==`y', fe i(group) cluster(group)
		outreg using tableA11`x'_`y', se varlabels starloc(1) nole starlevels(10 5 1) ///
		basefont(roman fs12) keep(black) ct("", "(2)") /// 
		addrows("Student Controls", "No" \ "School FEs", "Yes") ///
		note("Note: * p<0.1; ** p<0.05; *** p<0.01") summstat(N \ r2) merge

	xtreg more_days always_dis_flg fight_count if same_fight==2 & blk_wht==1 & `x'==`y', fe i(group) cluster(group)
		outreg using tableA11`x'_`y', se varlabels starloc(1) nole starlevels(10 5 1) ///
		basefont(roman fs12) keep(black) ct("", "(3)") /// 
		addrows("Student Controls", "No" \ "School FEs", "Yes") ///
		note("Note: * p<0.1; ** p<0.05; *** p<0.01") summstat(N \ r2) merge
		
	xtreg more_days always_dis_flg if same_fight==2 & blk_wht==1 & use==1 & `x'==`y', fe i(group) cluster(group)
		outreg using tableA11`x'_`y', se varlabels starloc(1) nole starlevels(10 5 1) ///
		basefont(roman fs12) keep(black) ct("", "(4)") /// 
		addrows("Student Controls", "No" \ "School FEs", "Yes") ///
		note("Note: * p<0.1; ** p<0.05; *** p<0.01") summstat(N \ r2) merge

}
}

//==============================================================================
// 20 - Create Table A12
//==============================================================================
//===========================================
//	#20A Initial data work
//===========================================
*replicates table 9 but for students whose first suspension is OSS and from a fight
	use "$data\fights.dta", replace
	cd "$output"

	keep if use==1
	*black white
	gen     totalb_w=0
	replace totalb_w=1 if stud_one_eth==1 & stud_two_eth==2
	replace totalb_w=1 if stud_one_eth==2 & stud_two_eth==1

	gen     innb_w=0
	replace innb_w=1 if totalb_w==1 & stud_one_type>3 & stud_two_type>3

	gen     outb_w=0
	replace outb_w=1 if totalb_w==1 & stud_one_type==2 & stud_two_type==2

	gen     out1_in2b_w=0
	replace out1_in2b_w=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_type==2 & stud_two_type>3
	replace out1_in2b_w=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_type>3 & stud_two_type==2

	gen     in1_out2b_w=0
	replace in1_out2b_w=1 if stud_one_eth==1 & stud_two_eth==2 & stud_one_type>3 & stud_two_type==2
	replace in1_out2b_w=1 if stud_one_eth==2 & stud_two_eth==1 & stud_one_type==2 & stud_two_type>3

	*black black*
	gen     totalb_b=0
	replace totalb_b=1 if stud_one_eth==1 & stud_two_eth==1

	gen     innb_b=0
	replace innb_b = 1 if totalb_b==1 & stud_one_type>3 & stud_two_type>3

	gen     outb_b=0
	replace outb_b=1 if totalb_b==1 & stud_one_type==2 & stud_two_type==2


	*white white*
	gen     totalw_w=0
	replace totalw_w=1 if stud_one_eth==2 & stud_two_eth==2

	gen     innw_w=0
	replace innw_w = 1 if totalw_w==1 & stud_one_type>3 & stud_two_type>3

	gen     outw_w=0
	replace outw_w=1 if totalw_w==1 & stud_one_type==2 & stud_two_type==2

	*FRL Non FRL*
	gen     totalp_np = 0
	replace totalp_np=1 if stud_one_frpl==1 & stud_two_non==1
	replace totalp_np=1 if stud_one_non==1 & stud_two_frpl==1

	gen     innp_np=0
	replace innp_np=1 if totalp_np==1 & stud_one_type>3 & stud_two_type>3

	gen     outp_np=0
	replace outp_np=1 if totalp_np==1 & stud_one_type==2 & stud_two_type==2

	gen     out1_in2p_np=0
	replace out1_in2p_np=1 if stud_one_frpl==1 & stud_two_non==1 & stud_one_type==2 & stud_two_type>3
	replace out1_in2p_np=1 if stud_one_non==1 & stud_two_frpl==1 & stud_one_type>3 & stud_two_type==2

	gen     in1_out2p_np=0
	replace in1_out2p_np=1 if stud_one_frpl==1 & stud_two_non==1 & stud_one_type>3 & stud_two_type==2
	replace in1_out2p_np=1 if stud_one_non==1 & stud_two_frpl==1 & stud_one_type==2 & stud_two_type>3

	*poor poor*
	gen     totalp_p=0
	replace totalp_p=1 if stud_one_frpl==1 & stud_two_frpl==1

	gen     innp_p=0
	replace innp_p=1 if totalp_p==1 & stud_one_type>3 & stud_two_type>3

	gen     outp_p=0
	replace outp_p=1 if totalp_p==1 & stud_one_type==2 & stud_two_type==2

	*nonpoor nonpoor*
	gen     totalnp_np=0
	replace totalnp_np=1 if stud_one_non==1 & stud_two_non==1

	gen     innnp_np=0
	replace innnp_np = 1 if totalnp_np==1 & stud_one_type>3 & stud_two_type>3

	gen     outnp_np=0
	replace outnp_np=1 if totalnp_np==1 & stud_one_type==2 & stud_two_type==2

//===========================================
//	#20B Generate Table A12
//===========================================
	collapse (sum) totalb_w-outnp_np
	gen check=1
	reshape long total inn out out1_in2 in1_out2, i(check) j(comparison) s
	gen order=0
	replace order =1 if comparison=="b_w"
	replace order =2 if comparison=="b_b"
	replace order =3 if comparison=="w_w"
	replace order =4 if comparison=="p_np"
	replace order =5 if comparison=="p_p"
	replace order =6 if comparison=="np_np"

	sort order
	gen denominator1=(out1_in2+in1_out2)/(2*total)
	gen denominator2=1-denominator1 
	gen denominator3=denominator1*denominator2*(1/total+1/total)
	gen denominator=sqrt(denominator3)
	replace out1_in2=out1_in2/total
	replace in1_out2=in1_out2/total
	replace out=out/total
	replace inn=inn/total
	gen numerator=out1_in2-in1_out2
	gen zscore=numerator/denominator 
	drop denominator1-numerator
	export excel using tableA12.xlsx, replace

//==============================================================================
// 21 - Create Figures A1-A4
//==============================================================================
//=========================================
//	#21A Build data
//=========================================

	use  "$data\sample01-all_students-v02.dta", replace

// Keep first student record to create student-year dataset (this really just
//	involves keeping the first record for disciplined students because they are 
//	only students with multiple records per years)
	unique stud_id year
	keep if missing(disc_cd) | disc_dt_start==first_off_date
	duplicates tag stud_id year, gen(xdupe)

* Randomly keeping one observation for remaining dupes
	gen double xrand = runiform() if xdupe>0 & !missing(xdupe)
	bysort stud_id year: egen double xmin = min(xrand)
	keep if xdupe==0 | xrand==xmin
	duplicates report stud_id year
		
// Streamlining data
	keep stud_id lea_id sch_id year free_flg frpl_det black
	unique stud_id year
	tabmiss

// Build district- and school-level percent Free lunch and Black

	* Prep variables 
	clonevar free2 = free_flg
		replace free2 = . if free_flg!=1
	clonevar black2 = black
		replace black2 = . if black2!=1
		
	* Create district-year variables
	bysort lea_id year: egen dytot = count(free_flg)
	la var dytot "total district enrollment, by year"
	bysort lea_id year: egen dyfree = count(free2)
	la var dyfree "district free lunch qualifiers, by year"
	gen dyfreepct = dyfree/dytot
	la var dyfreepct "district free lunch pct, by year"
	bysort lea_id year: egen dyblack = count(black2)
	la var dyblack "district black student enrollment, by year"
	gen dyblackpct = dyblack/dytot
	la var dyblackpct "district black student pct, by year"

	* Create school-year vars
	bysort sch_id year: egen sytot = count(frpl_det)
	la var sytot "total school enrollment, by year"
	bysort sch_id year: egen syfree = count(free2)
	la var syfree "school free lunch qualifiers, by year"
	gen syfreepct = syfree/sytot
	la var syfreepct "school free lunch pct, by year"
	bysort sch_id year: egen syblack = count(black2)
	la var syblack "school black student enrollment, by year"
	gen syblackpct = syblack/sytot
	la var syblackpct "school black student pct, by year"

	* Create category identifiers for graphs
	foreach var in free black {
		foreach type in dy sy {
			gen `type'`var'cat = .
				replace `type'`var'cat = 1  if `type'`var'pct<=.1
				replace `type'`var'cat = 2  if `type'`var'pct>.1 & `type'`var'pct<=.2
				replace `type'`var'cat = 3  if `type'`var'pct>.2 & `type'`var'pct<=.3
				replace `type'`var'cat = 4  if `type'`var'pct>.3 & `type'`var'pct<=.4
				replace `type'`var'cat = 5  if `type'`var'pct>.4 & `type'`var'pct<=.5
				replace `type'`var'cat = 6  if `type'`var'pct>.5 & `type'`var'pct<=.6
				replace `type'`var'cat = 7  if `type'`var'pct>.6 & `type'`var'pct<=.7
				replace `type'`var'cat = 8  if `type'`var'pct>.7 & `type'`var'pct<=.8
				replace `type'`var'cat = 9  if `type'`var'pct>.8 & `type'`var'pct<=.9
				replace `type'`var'cat = 10 if `type'`var'pct>.9 & `type'`var'pct<=1
		}
	}
	la var dyfreecat  "district free lunch pct category"
	la var dyblackcat "district black student pct category"
	la var syfreecat  "school free lunch pct category"
	la var syblackcat "school black student pct category"
	la def cat  1 "0-10%" ///
				2 "10-20%" ///
				3 "20-30%" ///
				4 "30-40%" ///
				5 "40-50%" ///
				6 "50-60%" ///
				7 "60-70%" ///
				8 "70-80%" ///
				9 "80-90%" ///
				10 "90-100%"
	la val dyfreecat  cat
	la val dyblackcat cat
	la val syfreecat  cat
	la val syblackcat cat


//====================================
//	#21B Create data for graphs
//====================================

	matrix out = J(10,4,.)
	matrix colnames out = dyfree dyblack syfree syblack
	foreach var in free black {
		foreach type in dy sy {
			forvalues i = 1(1)10 {
				qui: count if `type'`var'cat==`i'
				scalar row = rownumb(out,"r`i'")
				scalar col = colnumb(out,"`type'`var'")
				matrix out[row,col] = r(N)
			}
		}
	}

* Bringing output matrix data into working memory
	drop _all
	svmat out, names(col)
	gen category = _n
	la val category cat
	order category, first

* Scale variables
	foreach var in dyfree dyblack syfree syblack {
		replace `var' = `var'/1000000
	}


//=========================================
//	#21C Graphing Figures A1-A4
//=========================================

* 1. Enrollment by district % free lunch
	graph bar dyfree	///
		, over(category, label(labsize(*.75))) ///
		  ytitle(Students (in millions)) 
	graph export "$output\figureA01-enrollment_by_district_FL_pct.png", replace		

* 2. Enrollment by district % black
	graph bar dyblack	///
		, over(category, label(labsize(*.75))) ///
		  ytitle(Students (in millions)) 
	graph export "$output\figureA02-enrollment_by_district_Black_pct.png", replace	

* 3. Enrollment by school % free lunch
	graph bar syfree	///
		, over(category, label(labsize(*.75))) ///
		  ytitle(Students (in millions)) 
	graph export "$output\figureA03-enrollment_by_school_FL_pct.png", replace		

* 4. Enrollment by school % black lunch
	graph bar syblack	///
		, over(category, label(labsize(*.75))) ///
		  ytitle(Students (in millions)) 
	graph export "$output\figureA04-enrollment_by_school_Black_pct.png", replace	

// End
log close
exit
	
	
