// Preliminary setup
{
	set scheme s1mono, perm
	clear all
	global opts	a f plain coll(none) nodep nomti c(b(star fmt(%9.2f)) se(abs par fmt(%9.2f))) star(* .10 ** .05 *** .01) noobs
	global outcomes specific1 specific2 generic google_classroom khan_academy kahoot
	
	global data 	"C:\Users\joshuagoodman.USERS\Dropbox\CovidGoogleTrends\Data\AnalysisFiles"
	global paper 	"C:\Users\joshuagoodman.USERS\Dropbox\CovidGoogleTrends\Paper"

}

// MAIN EXHIBITS
	
// Figure 1 - Nationwide log(search) over time
	
	program define f_usa
	{
	cd "$data"
	use dma_merged_analysis_file.dta if dma_name=="US", clear
	foreach y of varlist $outcomes {
		replace `y'=ln(`y')
		g tempref`y' = `y' if week==21975
		egen ref`y' = max(tempref`y')
		replace `y'=`y'-ref`y'
		replace `y'=-.75 if `y'<-.75
	}		
	
	drop if week<21063 // Limit to 3 years
	g weekt = (week-21975)/7
	drop if weekt>12
	
	scatter specific1 week, 	///
		connect(none) msym(O) msize(small) mcolor(black) xline(21975, lw(thin) lp(dash))	///
		xlabel(21063 "9/1/17" 21245 "3/1/18" 21428 "9/1/18" 21610 "3/1/19" 21793 "9/1/19" 21975 "3/1/20") ///
		xtitle("Week", height(3)) ytitle("Log(search intensity)", height(5)) ///
		ylabel(-.75 0 .75) yline(0) ///
		title("(A) School-Centered Resources") ///
		saving(a.gph, replace)
				
	scatter generic week, 	///
		connect(none) msym(O) msize(small) mcolor(black) xline(21975, lw(thin) lp(dash))	///
		xlabel(21063 "9/1/17" 21245 "3/1/18" 21428 "9/1/18" 21610 "3/1/19" 21793 "9/1/19" 21975 "3/1/20") ///
		xtitle("Week", height(3)) ytitle("Log(search intensity)", height(5)) ///
		ylabel(-.75 0 .75) yline(0) ///
		title("(B) Parent-Centered Resources") ///
		saving(b.gph, replace)			
		
	graph combine a.gph b.gph, rows(2) xsize(8.5) ysize(11)
	rm a.gph
	rm b.gph
	cd "$paper"
	graph export f_usa.pdf, replace

	}
	end
	
// Figure 2 - Map of DMAs by SES
	
	program define f_map
	{
	cd "$data"
	use dma_merged_analysis_file.dta if dma_name!="US", clear
	foreach y of varlist specific1 generic {
		replace `y'=1 if `y'==.
		replace `y'=ln(`y')
	}
	
	g dma0 = substr(dma_json_id,-3,.)
	destring dma0, replace
	
	g weeknum = week(week)
	keep if inrange(weeknum,13,21)
	
	g pre_specific1 = specific1 if week<=21975
	g pre_generic 	= generic	if week<=21975
	g post_specific1 = specific1 if week>21975
	g post_generic = generic 	 if week>21975	
	collapse pre_* post_* ba_plus hh_med_inc hh_mean_inc hh_broadband hh_computer stu_ach, by(dma0 population)
	g diff_specific1 = post_specific1 - pre_specific1
	g diff_generic = post_generic - pre_generic
	xtile difsq = diff_specific1 [aw=population], n(4)
	xtile difgq = diff_generic [aw=population], n(4)
	
	pca ba_plus hh_med_inc hh_mean_inc hh_broadband hh_computer [aw=population]
	predict ses
	xtile sesq = ses [aw=population], n(4)
	
	merge 1:1 dma0 using dma_db_curved
	spmap sesq using dma_coord_curved.dta if cartodb_id <= 206, id(id) fcolor(Greys2) cln(6) /// 
			legend(pos(8) size(*1.75) lab(5 "Top quartile") lab(4 "3rd quartile") lab(3 "2nd quartile") lab(2 "Bottom quartile")) ///
			title("(A) Socioeconomic Status") saving(a.gph, replace)
	spmap difsq using dma_coord_curved.dta if cartodb_id <= 206, id(id) fcolor(Greys2) cln(5) /// 
			legend(pos(8) size(*1.75) lab(5 "Top quartile") lab(4 "3rd quartile") lab(3 "2nd quartile") lab(2 "Bottom quartile")) ///
			title("(B) Post-Covid Change in Search Intensity") saving(b.gph, replace)

	graph combine a.gph b.gph, rows(2) xsize(8.5) ysize(11) imargin(medium)
	rm a.gph
	rm b.gph
	cd "$paper"
	graph export f_map.pdf, replace
	
	}
	end
		
// Figure 3 - DMA event study regression coefficients by SES, log(search)	

	program define f_eventses
	{
	cd "$data"
	use dma_merged_analysis_file.dta if dma_name!="US", clear
	foreach y of varlist $outcomes {
		replace `y'=1 if `y'==.
		replace `y'=ln(`y')
	}		

	pca ba_plus hh_med_inc hh_mean_inc hh_broadband hh_computer [aw=population]
	predict ses
	xtile sesq = ses [aw=population], n(2)
	g high = (sesq==2)
		
	g weeknum = week(week)
	g schoolyear = year(week)+(month(week)>=6)
	g weekt = (week-21975)/7
	drop if weekt>12
	
	g thxgiv = inlist(week,20414,20778,21142,21506,21877)
	g xmas	 = inlist(week,20442,20813,21177,21541,21905)	
	g newyrs = inlist(week,20449,20814,21184,21548,21912)
	drop if thxgiv|xmas|newyrs

	g preperiod = (weekt<-25)
	g preperiod_high = preperiod*high
	
	forval j = 25(-1)1 {
		g byte preweek`j' = (weekt==(- 1*`j'))
		g byte preweek`j'_high = (weekt==(- 1*`j'))*high
		lab var preweek`j'_high " "		
	}
	forval j = 1/12 {
		g byte postweek`j' = (weekt==(`j'))
		g byte postweek`j'_high = (weekt==(`j'))*high
		lab var postweek`j'_high " "
	}	
	g zero = 0
	lab var preweek20_high "-20"
	lab var preweek10_high "-10"	
	lab var zero "0"
	lab var postweek10_high "10"
	
	reghdfe specific1 	preperiod* preweek* zero postweek* high [aw=population], a(weeknum schoolyear) vce(cluster dma_json_id) 
	est sto specific1
	reghdfe generic		preperiod* preweek* zero postweek* high [aw=population], a(weeknum schoolyear) vce(cluster dma_json_id) 
	est sto generic
	
	coefplot specific1, omit ///
				keep(preweek*_high zero postweek*_high) drop(postweek11_high postweek12_high) ///
				vertical legend(off) nooffset msize(medium)  ///
				xline(26, lp(dash) lwidth(thin)) ///
				xtitle("Week relative to 3/1/20", height(4)) ///
				ytitle("High - low SES log(search intensity)", height(5)) ///
				title("(A) School-Centered Resources") ///
				coeflabels(, labsize(medsmall)) yline(0) ylabel(-0.3 0 0.3 0.6) /// 
				mcolor(gs0) msymbol(O) ciopts(lcolor(gs0) lw(vvthin)) ///
				saving(a.gph, replace)	
	coefplot generic, omit ///
				keep(preweek*_high zero postweek*_high) drop(postweek11_high postweek12_high) ///
				vertical legend(off) nooffset msize(medium)  ///
				xline(26, lp(dash) lwidth(thin)) ///
				xtitle("Week relative to 3/1/20", height(4)) ///
				ytitle("High - low SES log(search intensity)", height(5)) ///
				title("(B) Parent-Centered Resources") ///
				coeflabels(, labsize(medsmall)) yline(0) ylabel(-0.3 0 0.3 0.6) /// 
				mcolor(gs0) msymbol(O) ciopts(lcolor(gs0) lw(vvthin)) ///
				saving(b.gph, replace)		
	
	graph combine a.gph b.gph, rows(2) xsize(8.5) ysize(11)
	rm a.gph
	rm b.gph
	cd "$paper"
	graph export f_eventses.pdf, replace
	estimates clear
		
	}
	end
		
// Table 2 - Regressions, using log(search intensity)
	
	program define t_ses
	{
	cd "$data"
	use dma_merged_analysis_file.dta if dma_name!="US", clear
	foreach y of varlist $outcomes {
		replace `y'=1 if `y'==.
		replace `y'=ln(`y')
	}	

	pca ba_plus hh_med_inc hh_mean_inc hh_broadband hh_computer [aw=population]
	predict ses
	xtile sesq2 = ses [aw=population], n(2)
		
	g weeknum = week(week)
	g schoolyear = year(week)+(month(week)>=6)
	g weekt = (week-21975)/7
	drop if weekt>12
	drop if inrange(weekt,0,3)
	
	g thxgiv = inlist(week,20414,20778,21142,21506,21877)
	g xmas	 = inlist(week,20442,20813,21177,21541,21905)	
	g newyrs = inlist(week,20449,20814,21184,21548,21912)
	drop if thxgiv|xmas|newyrs
	
	g post = (weekt>=0)
	g high = (sesq2==2)
	g post_high = post*high
	g post_low  = post*!high
	lab var post_high "Post-Covid * High SES"
	lab var post_low  "Post-Covid * Low SES"
	lab var post "Post-Covid"
	lab var high "High SES"

	g inc = hh_mean_inc/10000
	g post_inc = post*inc
	lab var post_inc "Post-Covid * Household income"
	
	replace hh_broadband = hh_broadband/10
	g post_broadband = post*hh_broadband
	lab var post_broadband "Post-Covid * Broadband penetration rate"

	replace hh_computer = hh_computer/10
	g post_computer = post*hh_computer
	lab var post_computer "Post-Covid * Computer penetration rate"
	
	replace sch_rural = sch_rural*10
	g post_rural = post*sch_rural
	lab var post_rural "Post-Covid * Fraction of schools in rural areas"
	
	g b = (stu_black)*10
	g post_b = post*b
	lab var post_b "Post-Covid * Fraction of students who are Black"
	
	
	foreach y of varlist specific1 generic google_classroom khan_academy {
		reghdfe `y' post					[aw=population], a(weeknum schoolyear) vce(cluster dma_json_id)	
		est sto a`y'		
		reghdfe `y' post_high post_low high	[aw=population], a(weeknum schoolyear) vce(cluster dma_json_id)		
		est sto b`y'
		reghdfe `y' post_high post high		[aw=population], a(weeknum schoolyear) vce(cluster dma_json_id)		
		est sto z`y'
		reghdfe `y' post_inc post inc 		[aw=population], a(weeknum schoolyear) vce(cluster dma_json_id)	
		est sto c`y'
		reghdfe `y' post_bro post hh_broadb	[aw=population], a(weeknum schoolyear) vce(cluster dma_json_id)	
		est sto d`y'
		reghdfe `y' post_com post hh_compu	[aw=population], a(weeknum schoolyear) vce(cluster dma_json_id)	
		est sto e`y'
		reghdfe `y' post_rur post sch_rural	[aw=population], a(weeknum schoolyear) vce(cluster dma_json_id)	
		est sto f`y'
		reghdfe `y' post_b  post b			[aw=population], a(weeknum schoolyear) vce(cluster dma_json_id)	
		est sto g`y'
	}
	
	cd "$paper"
	file open  t	using t_ses.tex, replace write
	file write t	"\begin{table}[htbp] \centering" _n "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
					"\caption{Changes in Search Intensity by Socioeconomic Status and Other Measures}" _n "\label{t_ses}" _n ///
					"\begin{tabular*}{1\textwidth}{@{\extracolsep{\fill}}l*{4}{c}}" _n "\midrule" _n ///
					"&School-	&Parent-	&			&		\\" _n ///
					"&centered	&centered	&Google		&Khan	\\" _n ///
					"&resources	&resources	&Classroom	&Academy\\" _n ///
					"&(1)&(2)&(3)&(4)\\" _n ///
					"\midrule" _n 
	file close t
	file open  t 	using t_ses.tex, append write
	file write t 	"(A) Nationwide \\" _n "\cmidrule{1-1}" _n
	file close t
	esttab a* using t_ses.tex, keep(post) l s(,) $opts     
	file open  t 	using t_ses.tex, append write
	file write t 	"\cmidrule{1-1}" _n "(B) By median SES\\" _n "\cmidrule{1-1}" _n
	file close t
	esttab b* 	using t_ses.tex, l keep(post* high) s(, lay(`""')) $opts  
	esttab z* using t_ses.tex, coef(post_high "High-Low SES Change") keep(post_high) s(,) $opts  
	file open  t 	using t_ses.tex, append write
	file write t 	"\cmidrule{1-1}" _n "(C) By income, online access, race\\" _n "\cmidrule{1-1}" _n
	file close t
	esttab c* using t_ses.tex, l keep(post_*) s(,lay(`""')) $opts     
	esttab d* using t_ses.tex, l keep(post_*) s(,lay(`""')) $opts     
	esttab e* using t_ses.tex, l keep(post_*) s(,lay(`""')) $opts     
	esttab f* using t_ses.tex, l keep(post_*) s(,lay(`""')) $opts     
	esttab g* using t_ses.tex, l keep(post_*) s(N, l("" "N") f(%6.0fc) lay(`""' @)) $opts     
	file open  t 	using t_ses.tex, append write
	file write t 	"\midrule" _n "\end{tabular*}" _n ///
					"\begin{tabular*}{1\textwidth}{p{6.4in}}" _n ///
					"\footnotesize Notes: Heteroskedasticity robust standard errors clustered by DMA are in parentheses (* p$<$.10 ** p$<$.05 *** p$<$.01). " ///
					"In panel A, each column regresses the logarithm of the listed topic's search intensity on a post-March 1 indicator. " ///
					"Panel B interacts the post indicator with above and below median SES indicators. " ///
					"Below each pair of interaction coefficients in panel B is the estimated difference between the high and low SES interaction coefficients. " ///
					"Each coefficient in Panel C comes from a separate regression that interacts the post indicator with DMA-level household mean income or the other listed resource and demographic measures. " ///
					"Income is measured per \textdollar 10,000, while the remaining variables are scaled to interpret coefficients as predicted effects of a 10 percentage point increase in the interacted characteristic. " ///
					"The regressions are weighted by DMA population and include fixed effects for week of year (1-52) and school year (2015-2020). " ///
					"The sample contains search data from June 2015 through May 2020, excluding March 2020 and weeks with major school holidays." ///
					"\end{tabular*}" _n "\end{table}" _n 
	file close t				
	estimates clear
		
	}
	end
	
// APPENDIX

// Figure A.1 - Nationwide event study regression coefficients
	
	program define f_eventusa
	{
	cd "$data"
	use dma_merged_analysis_file.dta if dma_name=="US", clear
	foreach y of varlist $outcomes {
		replace `y'=ln(`y')
		g tempref`y' = `y' if week==21975
		egen ref`y' = max(tempref`y')
		replace `y'=`y'-ref`y'
	}		
	
	g weeknum = week(week)
	g schoolyear = year(week)+(month(week)>=6)
	g weekt = (week-21975)/7
	drop if weekt>12
	
	g thxgiv = inlist(week,20414,20778,21142,21506,21877)
	g xmas	 = inlist(week,20442,20813,21177,21541,21905)	
	g newyrs = inlist(week,20449,20814,21184,21548,21912)
	drop if thxgiv|xmas|newyrs
	
	g preperiod = (weekt<-25)
	forval j = 25(-1)1 {
		g preweek`j' = (weekt==(- 1*`j'))
		lab var preweek`j' " "
	}
	forval j = 1/12 {
		g postweek`j' = (weekt==(`j'))
		lab var postweek`j' " "
	}	
	g zero = 0
	lab var preweek20 "-20"
	lab var preweek10 "-10"	
	lab var zero "0"
	lab var postweek10 "10"
	
	reghdfe specific1 preperiod preweek* zero postweek*, a(weeknum schoolyear) vce(robust) 
	est sto specific1
	reghdfe generic preperiod preweek* zero postweek*, a(weeknum schoolyear) vce(robust) 
	est sto generic
	
	coefplot specific1, omit ///
				keep(preweek* zero postweek*) drop(postweek11 postweek12) ///
				vertical legend(off) nooffset msize(small)  ///
				xline(26, lp(dash) lwidth(thin)) ///
				xtitle("Week relative to 3/1/20", height(4)) ///
				ytitle("Log(search intensity)", height(5)) ///
				title("(A) School-Centered Resources") ///
				coeflabels(, labsize(medsmall)) yline(0) ylabel(-0.3 0 .3 .6 .9 1.2) /// 
				mcolor(gs0) msymbol(O) ciopts(lcolor(gs0) lw(vvthin)) ///
				saving(a.gph, replace)	
	coefplot generic, omit ///
				keep(preweek* zero postweek*) drop(postweek11 postweek12) ///
				vertical legend(off) nooffset msize(small)  ///
				xline(26, lp(dash) lwidth(thin)) ///
				xtitle("Week relative to 3/1/20", height(4)) ///
				ytitle("Log(search intensity)", height(5)) ///
				title("(B) Parent-Centered Resources") ///
				coeflabels(, labsize(medsmall)) yline(0) ylabel(-0.3 0 .3 .6 .9 1.2) /// 
				mcolor(gs0) msymbol(O) ciopts(lcolor(gs0) lw(vvthin)) ///
				saving(b.gph, replace)		
	
	graph combine a.gph b.gph, rows(2) xsize(8.5) ysize(11)
	rm a.gph
	rm b.gph
	cd "$paper"
	graph export f_eventusa.pdf, replace
	estimates clear
		
	}
	end
	
// Figure A.2 - Log(Search) over time by DMA SES		

	program define f_ses
	{
	cd "$data"
	use dma_merged_analysis_file.dta if dma_name!="US", clear
	foreach y of varlist $outcomes {
		replace `y'=1 if `y'==.
		replace `y'=ln(`y')
		g tempref`y' = `y' if week==21975
		bys dma_json_id: egen ref`y' = max(tempref`y')
		replace `y'=`y'-ref`y'
		replace `y'=-.75 if `y'<-.75
	}		

	pca ba_plus hh_med_inc hh_mean_inc hh_broadband hh_computer [aw=population]
	predict ses
	xtile sesq = ses [aw=population], n(2)
	
	collapse specific1 generic [aw=population], by(sesq week)
	drop if week<21063 // Limit to 3 years
	g weekt = (week-21975)/7
	drop if weekt>12
	
	reshape wide specific1 generic, i(week) j(sesq)
	lab var specific11 "Low SES"
	lab var specific12 "High SES"
	lab var generic1 "Low SES"
	lab var generic2 "High SES"
	
	scatter specific12 specific11 week, 	///
		connect(none) msym(O O) msize(small small) mcolor(black black) mfcolor(black white) xline(21975, lw(thin) lp(dash))	///
		xlabel(21063 "9/1/17" 21245 "3/1/18" 21428 "9/1/18" 21610 "3/1/19" 21793 "9/1/19" 21975 "3/1/20") ///
		xtitle("Week", height(3)) ytitle("Log(search intensity)", height(5)) ///
		ylabel(-.75 0 .75) yline(0) ///
		title("(A) School-Centered Resources") ///
		legend(ring(0) pos(10) rows(2) symx(2)) ///
		saving(a.gph, replace)
				
	scatter generic2 generic1 week, 	///
		connect(none) msym(O O) msize(small small) mcolor(black black) mfcolor(black white) xline(21975, lw(thin) lp(dash))	///
		xlabel(21063 "9/1/17" 21245 "3/1/18" 21428 "9/1/18" 21610 "3/1/19" 21793 "9/1/19" 21975 "3/1/20") ///
		xtitle("Week", height(3)) ytitle("Log(search intensity)", height(5)) ///
		ylabel(-.75 0 .75) yline(0) ///
		title("(B) Parent-Centered Resources") ///
		legend(ring(0) pos(10) rows(2) symx(2)) ///
		saving(b.gph, replace)		
		
	graph combine a.gph b.gph, rows(2) xsize(8.5) ysize(11)
	rm a.gph
	rm b.gph
	cd "$paper"
	graph export f_ses.pdf, replace

	}
end

// Table A.2 - Sum stats
	
	program define t_sum
	{
	cd "$data"
	use dma_merged_analysis_file.dta if dma_name!="US", clear
	foreach y of varlist $outcomes {
		replace `y'=1 if `y'==.
		replace `y'=ln(`y')
	}				
	
	replace hh_mean_inc = hh_mean_inc/1000
	replace hh_med_inc = hh_med_inc/1000
	
	collapse hh_mean_inc hh_med_inc ba_plus hh_broadband hh_computer sch_rural stu_black [aw=population], by(dma_json_id) 

	lab var ba_plus 		"Fraction of adults with a B.A."
	lab var hh_mean_inc 	"Mean household income (1,000s)"
	lab var hh_med_inc 		"Median household income (1,000s)"
	lab var hh_broadband 	"Fraction of households with broadband internet"
	lab var hh_computer 	"Fraction of households with a computer"
	lab var sch_rural 		"Fraction of schools in rural areas"
	lab var stu_black 		"Fraction of students who are Black"
	
	eststo clear
	estpost sum hh_mean_inc hh_med_inc ba_plus hh_broadband hh_computer 
	est store a	

	eststo clear
	estpost sum sch_rural stu_black
	est store b
	
	qui sum sch_rural
	local count = r(N)
	disp `count'
	
	cd "$paper"
	file open  t	using t_sum.tex, replace write
	file write t	"\begin{table}[htbp] \centering" _n "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
					"\caption{Summary Statistics of DMAs}" _n "\label{t_sum}" _n ///
					"\begin{tabular*}{1\textwidth}{@{\extracolsep{\fill}}l*{2}{c}}" _n "\midrule" _n ///
					"&Mean	&SD \\" _n ///
					"\midrule" _n 
	file close t			
	file open  t 	using t_sum.tex, append write
	file write t 	"(A) 2016 American Community Survey\\" _n "\cmidrule{1-1}" _n
	file close t
	esttab a using t_sum.tex, a f plain coll(none) nodep nomti noobs cells("mean(fmt(2)) sd(fmt(2))") label 
	file open  t 	using t_sum.tex, append write
	file write t 	"\cmidrule{1-1}" _n "(B) 2016 Stanford Education Data Archive\\" _n "\cmidrule{1-1}" _n
	file close t
	esttab b using t_sum.tex, a f plain coll(none) nodep nomti cells("mean(fmt(2)) sd(fmt(2))") label s(N, l("" "N") f(%6.0fc) lay(`""' @))
	file open  t 	using t_sum.tex, append write
	file write t 	"\midrule" _n "\end{tabular*}" _n ///
					"\begin{tabular*}{1\textwidth}{p{6.4in}}" _n ///
					"\footnotesize Notes: Panel A presents summary statistics of data from the 2016 American Community Survey (ACS). " ///
					"We use county-level ACS data and generate DMA-level measures as the population-weighted average across the counties within each DMA. " ///
					"Panel B presents summary statistics of 2016 data from the Stanford Education Data Archive (SEDA). " ///
					"We again use county-level SEDA data and generate DMA-level measures as the population-weighted average. " ///					
					"\end{tabular*}" _n "\end{table}" _n 
	file close t				
	estimates clear
		
	}
	end

// Table A.4 - Horse race
	
	program define t_horserace
	{
	cd "$data"
	use dma_merged_analysis_file.dta if dma_name!="US", clear
	foreach y of varlist $outcomes {
		replace `y'=1 if `y'==.
		replace `y'=ln(`y')
	}	

	pca ba_plus hh_med_inc hh_mean_inc hh_broadband hh_computer [aw=population]
	predict ses
	xtile sesq2 = ses [aw=population], n(2)
		
	g weeknum = week(week)
	g schoolyear = year(week)+(month(week)>=6)
	g weekt = (week-21975)/7
	drop if weekt>12
	drop if inrange(weekt,0,3)
	
	g thxgiv = inlist(week,20414,20778,21142,21506,21877)
	g xmas	 = inlist(week,20442,20813,21177,21541,21905)	
	g newyrs = inlist(week,20449,20814,21184,21548,21912)
	drop if thxgiv|xmas|newyrs
	
	g post = (weekt>=0)

	g inc = hh_mean_inc/10000
	g post_inc = post*inc
	lab var post_inc "Post-Covid * Household income"
		
	replace sch_rural = sch_rural*10
	g post_rural = post*sch_rural
	lab var post_rural "Post-Covid * Fraction of schools in rural areas"
	
	g b = (stu_black)*10
	g post_b = post*b
	lab var post_b "Post-Covid * Fraction of students who are Black"
	
	foreach y of varlist specific1 generic google_classroom khan_academy {
		reghdfe `y' post_inc post_rur post_b post hh_mean_inc sch_rural b		[aw=population], a(weeknum schoolyear) vce(cluster dma_json_id)	
		est sto a`y'
	}
	
	cd "$paper"
	file open  t	using t_horserace.tex, replace write
	file write t	"\begin{table}[htbp] \centering" _n "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
					"\caption{Income, School Rurality and Race}" _n "\label{t_horserace}" _n ///
					"\begin{tabular*}{1\textwidth}{@{\extracolsep{\fill}}l*{4}{c}}" _n "\midrule" _n ///
					"&School-	&Parent-	&			&		\\" _n ///
					"&centered	&centered	&Google		&Khan	\\" _n ///
					"&resources	&resources	&Classroom	&Academy\\" _n ///
					"&(1)&(2)&(3)&(4)\\" _n ///
					"\midrule" _n 
	file close t
	esttab a* using t_horserace.tex, l keep(post_*) s(N, l("" "N") f(%6.0fc) lay(`""' @)) $opts     
	file open  t 	using t_horserace.tex, append write
	file write t 	"\midrule" _n "\end{tabular*}" _n ///
					"\begin{tabular*}{1\textwidth}{p{6.4in}}" _n ///
					"\footnotesize Notes: Heteroskedasticity robust standard errors clustered by DMA are in parentheses (* p$<$.10 ** p$<$.05 *** p$<$.01). " ///
					"Each column is a separate regression of the logarithm of the listed topic's search intensity on a post-March 1 indicator interacted with the DMA-level characteristics shown (and includes their direct effects). " ///
					"Income is measured per \textdollar 10,000, while the remaining variables are scaled to interpret coefficients as predicted effects of a 10 percentage point increase in the interacted characteristic. " ///
					"The regressions are weighted by DMA population and include fixed effects for week of year (1-52) and school year (2015-2020). " ///
					"The sample contains search data from June 2015 through May 2020, excluding March 2020 and weeks with major school holidays." ///
					"\end{tabular*}" _n "\end{table}" _n 
	file close t				
	estimates clear
		
	}
	end

// Table A.5 - Regressions, using log(search intensity), by quartile
	
	program define t_sesq
	{
	cd "$data"
	use dma_merged_analysis_file.dta if dma_name!="US", clear
	foreach y of varlist $outcomes {
		replace `y'=1 if `y'==.
		replace `y'=ln(`y')
	}	

	pca ba_plus hh_med_inc hh_mean_inc hh_broadband hh_computer [aw=population]
	predict ses
	xtile sesq4 = ses [aw=population], n(4)
		
	g weeknum = week(week)
	g schoolyear = year(week)+(month(week)>=6)
	g weekt = (week-21975)/7
	drop if weekt>12
	drop if inrange(weekt,0,3)
	
	g thxgiv = inlist(week,20414,20778,21142,21506,21877)
	g xmas	 = inlist(week,20442,20813,21177,21541,21905)	
	g newyrs = inlist(week,20449,20814,21184,21548,21912)
	drop if thxgiv|xmas|newyrs
	
	g post = (weekt>=0)
	lab var post "Post-Covid"

	g high_4 = (sesq4==4)
	g high_3 = (sesq4==3)
	g high_2 = (sesq4==2)
	g high_1 = (sesq4==1)
	g post_high_4 = post*high_4
	g post_high_3 = post*high_3
	g post_high_2 = post*high_2
	g post_high_1 = post*high_1
	lab var post_high_4 "Post-Covid * Top SES quartile"
	lab var post_high_3 "Post-Covid * 3rd SES quartile"
	lab var post_high_2 "Post-Covid * 2nd SES quartile"
	lab var post_high_1 "Post-Covid * Bottom SES quartile"
	lab var high_4 "Top SES quartile"
	lab var high_3 "3rd SES quartile"
	lab var high_2 "2nd SES quartile"
	
	foreach y of varlist specific1 generic google_classroom khan_academy {	
		reghdfe `y' post_high_* high_4-high_2 [aw=population], a(weeknum schoolyear) cluster(dma_json_id)	
		est sto a`y'
	}
	
	cd "$paper"
	file open  t	using t_sesq.tex, replace write
	file write t	"\begin{table}[htbp] \centering" _n "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
					"\caption{Changes in Search Intensity, by Socioeconomic Status Quartile}" _n "\label{t_sesq}" _n ///
					"\begin{tabular*}{1\textwidth}{@{\extracolsep{\fill}}l*{4}{c}}" _n "\midrule" _n ///
					"&School-	&Parent-	&			&		\\" _n ///
					"&centered	&centered	&Google		&Khan	\\" _n ///
					"&resources	&resources	&Classroom	&Academy\\" _n ///
					"&(1)&(2)&(3)&(4)\\" _n ///
					"\midrule" _n 
	file close t
	esttab a* using t_sesq.tex, keep(*high*) l s(N, l("" "N") f(%6.0fc) lay(`""' @)) $opts      
	file open  t 	using t_sesq.tex, append write
	file write t 	"\midrule" _n "\end{tabular*}" _n ///
					"\begin{tabular*}{1\textwidth}{p{6.4in}}" _n ///
					"\footnotesize Notes: Heteroskedasticity robust standard errors clustered by DMA are in parentheses (* p$<$.10 ** p$<$.05 *** p$<$.01). " ///
					"Each column regresses the logarithm of DMA-level search intensity for the listed topic on a post-March 1 indicator interacted with indicators for each DMA's SES quartile, as well as directly including those SES quartile indicators. " ///
					"The regressions are weighted by DMA population and include fixed effects for week of year (1-52) and school year (2015-2020). " ///
					"The sample contains search data from June 2015 through May 2020, excluding March 2020 and weeks with major school holidays." ///					
					"\end{tabular*}" _n "\end{table}" _n 
	file close t				
	estimates clear
		
	}
	end
	
// Table A.6 - Regressions by SES and Census region, log(search)
	
	program define t_region
	{
	cd "$data"
	use dma_merged_analysis_file.dta if dma_name!="US", clear
	foreach y of varlist $outcomes {
		replace `y'=1 if `y'==.
		replace `y'=ln(`y')
	}	

	pca ba_plus hh_med_inc hh_mean_inc hh_broadband hh_computer [aw=population]
	predict ses
	forval x=1/4 {
		xtile sesq_`x' = ses if region==`x' [aw=population], n(2)
	}	
	egen sesq2 = rowmax(sesq_*)
	drop sesq_*
	
	g weeknum = week(week)
	g schoolyear = year(week)+(month(week)>=6)
	g weekt = (week-21975)/7
	drop if weekt>12
	drop if inrange(weekt,0,3)
	
	g thxgiv = inlist(week,20414,20778,21142,21506,21877)
	g xmas	 = inlist(week,20442,20813,21177,21541,21905)	
	g newyrs = inlist(week,20449,20814,21184,21548,21912)
	drop if thxgiv|xmas|newyrs
	
	g post = (weekt>=0)
	g high = (sesq2==2)
	g post_high = post*high
	g post_low  = post*!high
	lab var post_high "Post-Covid * High SES"
	lab var post_low  "Post-Covid * Low SES"
	lab var post "Post-Covid"
	lab var high "High SES"
	
	foreach y of varlist specific1 generic google_classroom khan_academy {
	forval x=1/4 {
		reghdfe `y' post_high post_low high	if region==`x' [aw=population], a(weeknum schoolyear) cluster(dma_json_id)	
		est sto `y'`x'
		reghdfe `y' post_high post high		if region==`x' [aw=population], a(weeknum schoolyear) cluster(dma_json_id)	
		est sto diff`y'`x'
	}
	}
	
	cd "$paper"
	file open  t	using t_region.tex, replace write
	file write t	"\begin{table}[htbp] \centering" _n "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
					"\caption{Changes in Search Intensity by Socioeconomic Status and Census Region}" _n "\label{t_region}" _n ///
					"\begin{tabular*}{1\textwidth}{@{\extracolsep{\fill}}l*{4}{c}}" _n "\midrule" _n ///
					"&Northeast&Midwest&South&West\\" _n ///
					"&(1)&(2)&(3)&(4)\\" _n ///
					"\midrule" _n 
	file close t
	file open  t 	using t_region.tex, append write
	file write t 	"(A) School-centered resources \\" _n "\cmidrule{1-1}" _n
	file close t
	esttab specific1* using t_region.tex, l keep(post*) s(,) $opts     
	esttab diffspec* using t_region.tex, coef(post_high "High-Low SES Change") keep(post_high) s(,) $opts     
	file open  t 	using t_region.tex, append write
	file write t 	"\cmidrule{1-1}" _n "(B) Parent-centered resources\\" _n "\cmidrule{1-1}" _n
	file close t
	esttab generic* using t_region.tex, l keep(post*) s(,) $opts    
	esttab diffgen* using t_region.tex, coef(post_high "High-Low SES Change") keep(post_high) s(,) $opts     
	file open  t 	using t_region.tex, append write
	file write t 	"\cmidrule{1-1}" _n "(C) Google Classroom\\" _n "\cmidrule{1-1}" _n
	file close t
	esttab google* using t_region.tex, l keep(post*) s(,) $opts        
	esttab diffgoog* using t_region.tex, coef(post_high "High-Low SES Change") keep(post_high) s(,) $opts     
	file open  t 	using t_region.tex, append write
	file write t 	"\cmidrule{1-1}" _n "(D) Khan Academy\\" _n "\cmidrule{1-1}" _n
	file close t
	esttab khan* using t_region.tex, l keep(post*) s(,) $opts     
	esttab diffkhan* using t_region.tex, coef(post_high "High-Low SES Change") keep(post_high) s(N, l("" "N") f(%6.0fc) lay(`""' @)) $opts     
	file open  t 	using t_region.tex, append write
	file write t 	"\midrule" _n "\end{tabular*}" _n ///
					"\begin{tabular*}{1\textwidth}{p{6.4in}}" _n ///
					"\footnotesize Notes: Heteroskedasticity robust standard errors clustered by DMA are in parentheses (* p$<$.10 ** p$<$.05 *** p$<$.01). " ///
					"Each column in each panel regresses the logarithm of DMA-level search intensity for the listed topic on a post-March 1 indicator interacted with indicators for above and below median DMA-level SES in that Census region, as well as directly including an above median SES indicator. " ///
					"Below each pair of interaction coefficients is the estimated difference between the high and low SES interaction coefficients. " ///
					"The regressions are weighted by DMA population and include fixed effects for week of year (1-52) and school year (2015-2020). " ///
					"The sample contains search data from June 2015 through May 2020, excluding March 2020 and weeks with major school holidays." ///
					"\end{tabular*}" _n "\end{table}" _n 
	file close t				
	estimates clear
		
	}
	end
	
// Table A.7 - Regressions, using log(search intensity)
	
	program define t_sesmiss
	{
	cd "$data"
	use dma_merged_analysis_file.dta if dma_name!="US", clear
	foreach y of varlist $outcomes {
		replace `y'=ln(`y')
	}	

	pca ba_plus hh_med_inc hh_mean_inc hh_broadband hh_computer [aw=population]
	predict ses
	xtile sesq2 = ses [aw=population], n(2)
		
	g weeknum = week(week)
	g schoolyear = year(week)+(month(week)>=6)
	g weekt = (week-21975)/7
	drop if weekt>12
	drop if inrange(weekt,0,3)
	
	g thxgiv = inlist(week,20414,20778,21142,21506,21877)
	g xmas	 = inlist(week,20442,20813,21177,21541,21905)	
	g newyrs = inlist(week,20449,20814,21184,21548,21912)
	drop if thxgiv|xmas|newyrs
	
	g post = (weekt>=0)
	g high = (sesq2==2)
	g post_high = post*high
	g post_low  = post*!high
	lab var post_high "Post-Covid * High SES"
	lab var post_low  "Post-Covid * Low SES"
	lab var post "Post-Covid"
	lab var high "High SES"

	g inc = hh_mean_inc/10000
	g post_inc = post*inc
	lab var post_inc "Post-Covid * Household income"
	
	replace hh_broadband = hh_broadband/10
	g post_broadband = post*hh_broadband
	lab var post_broadband "Post-Covid * Broadband penetration rate"

	replace hh_computer = hh_computer/10
	g post_computer = post*hh_computer
	lab var post_computer "Post-Covid * Computer penetration rate"
	
	replace sch_rural = sch_rural*10
	g post_rural = post*sch_rural
	lab var post_rural "Post-Covid * Fraction of schools in rural areas"
	
	g b = (stu_black)*10
	g post_b = post*b
	lab var post_b "Post-Covid * Fraction of students who are Black"
	
	
	foreach y of varlist specific1 generic google_classroom khan_academy {
		reghdfe `y' post					[aw=population], a(weeknum schoolyear) vce(cluster dma_json_id)	
		est sto a`y'		
		reghdfe `y' post_high post_low high	[aw=population], a(weeknum schoolyear) vce(cluster dma_json_id)		
		est sto b`y'
		reghdfe `y' post_high post high		[aw=population], a(weeknum schoolyear) vce(cluster dma_json_id)		
		est sto z`y'
		reghdfe `y' post_inc post inc 		[aw=population], a(weeknum schoolyear) vce(cluster dma_json_id)	
		est sto c`y'
		reghdfe `y' post_bro post hh_broadb	[aw=population], a(weeknum schoolyear) vce(cluster dma_json_id)	
		est sto d`y'
		reghdfe `y' post_com post hh_compu	[aw=population], a(weeknum schoolyear) vce(cluster dma_json_id)	
		est sto e`y'
		reghdfe `y' post_rur post sch_rural	[aw=population], a(weeknum schoolyear) vce(cluster dma_json_id)	
		est sto f`y'
		reghdfe `y' post_b  post b			[aw=population], a(weeknum schoolyear) vce(cluster dma_json_id)	
		est sto g`y'
	}
	
	cd "$paper"
	file open  t	using t_sesmiss.tex, replace write
	file write t	"\begin{table}[htbp] \centering" _n "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
					"\caption{Changes in Search Intensity, Excluding Low Search Areas}" _n "\label{t_sesmiss}" _n ///
					"\begin{tabular*}{1\textwidth}{@{\extracolsep{\fill}}l*{4}{c}}" _n "\midrule" _n ///
					"&School-	&Parent-	&			&		\\" _n ///
					"&centered	&centered	&Google		&Khan	\\" _n ///
					"&resources	&resources	&Classroom	&Academy\\" _n ///
					"&(1)&(2)&(3)&(4)\\" _n ///
					"\midrule" _n 
	file close t
	file open  t 	using t_sesmiss.tex, append write
	file write t 	"(A) Nationwide \\" _n "\cmidrule{1-1}" _n
	file close t
	esttab a* using t_sesmiss.tex, keep(post) l s(,) $opts     
	file open  t 	using t_sesmiss.tex, append write
	file write t 	"\cmidrule{1-1}" _n "(B) By median SES\\" _n "\cmidrule{1-1}" _n
	file close t
	esttab b* 	using t_sesmiss.tex, l keep(post* high) s(, lay(`""')) $opts  
	esttab z* using t_sesmiss.tex, coef(post_high "High-Low SES Change") keep(post_high) s(,) $opts  
	file open  t 	using t_sesmiss.tex, append write
	file write t 	"\cmidrule{1-1}" _n "(C) By income, online access, race\\" _n "\cmidrule{1-1}" _n
	file close t
	esttab c* using t_sesmiss.tex, l keep(post_*) s(,lay(`""')) $opts     
	esttab d* using t_sesmiss.tex, l keep(post_*) s(,lay(`""')) $opts     
	esttab e* using t_sesmiss.tex, l keep(post_*) s(,lay(`""')) $opts     
	esttab f* using t_sesmiss.tex, l keep(post_*) s(,lay(`""')) $opts     
	esttab g* using t_sesmiss.tex, l keep(post_*) s(N, l("" "N") f(%6.0fc) lay(`""' @)) $opts     
	file open  t 	using t_sesmiss.tex, append write
	file write t 	"\midrule" _n "\end{tabular*}" _n ///
					"\begin{tabular*}{1\textwidth}{p{6.4in}}" _n ///
					"\footnotesize Notes: Heteroskedasticity robust standard errors clustered by DMA are in parentheses (* p$<$.10 ** p$<$.05 *** p$<$.01). " ///
					"In panel A, each column regresses the logarithm of the listed topic's search intensity on a post-March 1 indicator. " ///
					"Panel B interacts the post indicator with above and below median SES indicators. " ///
					"Below each pair of interaction coefficients in panel B is the estimated difference between the high and low SES interaction coefficients. " ///
					"Each coefficient in Panel C comes from a separate regression that interacts the post indicator with DMA-level household mean income or the other listed resource and demographic measures. " ///
					"Income is measured per \textdollar 10,000, while the remaining variables are scaled to interpret coefficients as predicted effects of a 10 percentage point increase in the interacted characteristic. " ///
					"The regressions are weighted by DMA population and include fixed effects for week of year (1-52) and school year (2015-2020). " ///
					"The sample contains search data from June 2015 through May 2020, excluding March 2020 and weeks with major school holidays, as well as DMAs with low search intensity in any given week." ///
					"\end{tabular*}" _n "\end{table}" _n 
	file close t				
	estimates clear
		
	}
	end
		
	
// Main paper sub-programs

*f_usa
*f_map
*f_eventses
*t_ses

// Appendix sub-programs

*f_eventusa
*f_ses
*t_sum
*t_horserace
*t_sesq
*t_region
*t_sesmiss