********************************************************************************
* OVERVIEW OF FILE (total lines 2551)
*STEP 1: NCSES BUILD DATASET
*STEP 2: NCSES MATCH TO IPEDS
*STEP 3: NCSES IPEDS TO NRC
*STEP 4: DELTA COST CLEAN
*STEP 5: NCSES IPEDS NRC MERGE TO DELTA COST & VET WITH NCES
*STEP 6: NCSES ANALYSIS
	*Sample definition begins line 2145
	*Descriptive Analysis begins line 2185
	*Econometric Analysis begins line 2246
*Note: Final dataset only includes variables used for analysis are results in paper.
********************************************************************************

********************************************************************************
*STEP 1: NCSES BUILD DATASET
********************************************************************************

/** University Department Funding **
data pulled from: https://ncsesdata.nsf.gov/webcaspar/
NSF Survey of R&D Expenditures at Universities and Colleges/Higher Education R&D Survey

********************************************************************************
********************************************************************************
** Need to set up in order to merge Non-Federal and Federal university department funding
********************************************************************************
********************************************************************************
** Step 1.A: Set up Non-federal data
* Data pulled from: R&D Expenditures, By Source of Funds (2010 - 2014)
	ARRA Financed R&D Expenditures 
	State/Local Govt. Financed R&D Expenditures 
	Business Financed R&D Expenditures 
	Nonprofit Financed R&D Expenditures 
	Institutionally Financed R&D Expenditures 
	R&D Expenditures funded by All Other Sources
import excel "/Users/llanahan/Dropbox/Uni Funding Sources/Data/webcaspar_table2010_2013/webcaspar_R&R data pull 10_14/original data 10_14.xlsx", sheet("webcaspar_table20151124140026.c") firstrow clear
save "/Users/llanahan/Dropbox/Uni Funding Sources/Data/webcaspar_table2010_2013/webcaspar_R&R data pull 10_14/original data 10_14.dta"
*******************************************************************************/
clear all 
global dir "/Users/llanahan/Dropbox/Uni Funding Sources/Data"
use "$dir/webcaspar_table2010_2013/webcaspar_R&R data pull 10_14/original data 10_14.dta"
* 122,688 observations (5 year panel)

set more off
** create unique id
rename AcademicDisciplineDetaileds dept
gen double dept_id = .
br dept dept_id
foreach x in dept {
foreach y in dept_id {
set more off
replace `y' = 1 if `x' == "Aerospace Engineering" 
replace `y' = 2 if `x' == "Agricultural Sciences"
replace `y' = 3 if `x' == "Arts and Music"
replace `y' = 4 if `x' == "Astronomy"
replace `y' = 5 if `x' == "Atmospheric Sciences" 
replace `y' = 6 if `x' == "Biological Sciences" 
replace `y' = 7 if `x' == "Business and Management" 
replace `y' = 8 if `x' == "Chemical Engineering"
replace `y' = 9 if `x' == "Chemistry"
replace `y' = 10 if `x' == "Civil Engineering" 
replace `y' = 11 if `x' == "Communication and Librarianship"
replace `y' = 12 if `x' == "Computer Science"
replace `y' = 13 if `x' == "Earth Sciences" 
replace `y' = 14 if `x' == "Economics"
replace `y' = 15 if `x' == "Education"
replace `y' = 16 if `x' == "Electrical Engineering" 
replace `y' = 17 if `x' == "Humanities"
replace `y' = 18 if `x' == "Interdisciplinary or Other Sciences" 
replace `y' = 19 if `x' == "Law" 
replace `y' = 20 if `x' == "Materials Engineering"  
replace `y' = 21 if `x' == "Mathematics and Statistics" 
replace `y' = 22 if `x' == "Mechanical Engineering"
replace `y' = 23 if `x' == "Medical Sciences"
replace `y' = 24 if `x' == "Not Available"
replace `y' = 25 if `x' == "Oceanography"
replace `y' = 26 if `x' == "Other Engineering"
replace `y' = 27 if `x' == "Other Geosciences" 
replace `y' = 28 if `x' == "Other Life Sciences" 
replace `y' = 29 if `x' == "Other Non-sciences or Unknown Disciplines"
replace `y' = 30 if `x' == "Other Physical Sciences" 
replace `y' = 31 if `x' == "Other Social Sciences"
replace `y' = 32 if `x' == "Physics"
replace `y' = 33 if `x' == "Political Science and Public Administration"
replace `y' = 34 if `x' == "Psychology"
replace `y' = 35 if `x' == "Social Service Professions"
replace `y' = 36 if `x' == "Sociology"
}
}
gen double dept_to_id = dept_id * 0.01
gen double yr_to_id = .
gen double year = Year
foreach y in year {
foreach x in yr_to_id {
set more off
replace `x' = 1000000 if `y'==2010
replace `x' = 2000000 if `y'==2011
replace `x' = 3000000 if `y'==2012
replace `x' = 4000000 if `y'==2013
replace `x' = 5000000 if `y'==2014
}
}
gen double fice = FICE
destring fice, replace
gen double unique_id = fice+yr_to_id+dept_to_id
format unique_id %10.2f
lab var unique_id "Yr id (1-5) + FICE . Dept (0.01 - 0.36)"
save "/Users/llanahan/Dropbox/Uni Funding Sources/Data/webcaspar_table2010_2013/webcaspar_R&R data pull 10_14/non-federal original data 10_14 to merge.dta", replace

********************************************************************************
** Step 1.B: Set up Federal data
/* Data pulled from: Total R&D Expenditures (Years Available: 2003-12)--
	Total R&D Expenditures in All Fields
	Federally Financed R&D Expenditures in All Fields
	* Adding in 5th panel
import excel "/Users/llanahan/Dropbox/Uni Funding Sources/Data/webcaspar_table2010_2013_federal/webcaspar_R&R data pull 10_14/original data 10_14 federal.xlsx", sheet("webcaspar_table20151124143402.c") firstrow
*******************************************************************************/
clear all 
global dir "/Users/llanahan/Dropbox/Uni Funding Sources/Data"
use "$dir/webcaspar_table2010_2013/webcaspar_R&R data pull 10_14/original data 10_14 federal.dta"
* 122,688 observations (5 year panel)
set more off

** create unique id
rename AcademicDisciplineDetaileds dept
gen double dept_id = .
br dept dept_id
foreach x in dept {
foreach y in dept_id {
set more off
replace `y' = 1 if `x' == "Aerospace Engineering" 
replace `y' = 2 if `x' == "Agricultural Sciences"
replace `y' = 3 if `x' == "Arts and Music"
replace `y' = 4 if `x' == "Astronomy"
replace `y' = 5 if `x' == "Atmospheric Sciences" 
replace `y' = 6 if `x' == "Biological Sciences" 
replace `y' = 7 if `x' == "Business and Management" 
replace `y' = 8 if `x' == "Chemical Engineering"
replace `y' = 9 if `x' == "Chemistry"
replace `y' = 10 if `x' == "Civil Engineering" 
replace `y' = 11 if `x' == "Communication and Librarianship"
replace `y' = 12 if `x' == "Computer Science"
replace `y' = 13 if `x' == "Earth Sciences" 
replace `y' = 14 if `x' == "Economics"
replace `y' = 15 if `x' == "Education"
replace `y' = 16 if `x' == "Electrical Engineering" 
replace `y' = 17 if `x' == "Humanities"
replace `y' = 18 if `x' == "Interdisciplinary or Other Sciences" 
replace `y' = 19 if `x' == "Law" 
replace `y' = 20 if `x' == "Materials Engineering"  
replace `y' = 21 if `x' == "Mathematics and Statistics" 
replace `y' = 22 if `x' == "Mechanical Engineering"
replace `y' = 23 if `x' == "Medical Sciences"
replace `y' = 24 if `x' == "Not Available"
replace `y' = 25 if `x' == "Oceanography"
replace `y' = 26 if `x' == "Other Engineering"
replace `y' = 27 if `x' == "Other Geosciences" 
replace `y' = 28 if `x' == "Other Life Sciences" 
replace `y' = 29 if `x' == "Other Non-sciences or Unknown Disciplines"
replace `y' = 30 if `x' == "Other Physical Sciences" 
replace `y' = 31 if `x' == "Other Social Sciences"
replace `y' = 32 if `x' == "Physics"
replace `y' = 33 if `x' == "Political Science and Public Administration"
replace `y' = 34 if `x' == "Psychology"
replace `y' = 35 if `x' == "Social Service Professions"
replace `y' = 36 if `x' == "Sociology"
}
}
gen double dept_to_id = dept_id * 0.01
gen double yr_to_id = .
gen double year = Year
foreach y in year {
foreach x in yr_to_id {
set more off
replace `x' = 1000000 if `y'==2010
replace `x' = 2000000 if `y'==2011
replace `x' = 3000000 if `y'==2012
replace `x' = 4000000 if `y'==2013
replace `x' = 5000000 if `y'==2014
}
}
gen double fice = FICE
destring fice, replace
gen double unique_id = fice+yr_to_id+dept_to_id
format unique_id %10.2f
lab var unique_id "Yr id (1-5) + FICE . Dept (0.01 - 0.36)"
keep unique_id DeflatedFederallyFinancedRD FederallyFinancedRDExpenditu DeflatedTotalRDExpenditures TotalRDExpendituresinAllFi
sort unique_id
save "/Users/llanahan/Dropbox/Uni Funding Sources/Data/webcaspar_table2010_2013/webcaspar_R&R data pull 10_14/federal original data 10_14 to merge.dta", replace

********************************************************************************
** Step 1.C: Merge Federal and Non-Federal Datasets
********************************************************************************
clear all 
global dir "/Users/llanahan/Dropbox/Uni Funding Sources/Data"
*use "$dir/webcaspar_table2010_2013/non-federal original data to merge.dta"
use "$dir/webcaspar_table2010_2013/webcaspar_R&R data pull 10_14/non-federal original data 10_14 to merge.dta"
set more off
sort unique_id
merge 1:1 unique_id using "/Users/llanahan/Dropbox/Uni Funding Sources/Data/webcaspar_table2010_2013/webcaspar_R&R data pull 10_14/federal original data 10_14 to merge.dta"
drop _merge
** 122688 observations matched 1:1
destring, replace
** Note: Those with ARRA have no other funding. Given that this is a not a source that we are directly interested in, I will drop them.
br Deflated* year if DeflatedA >=0 & DeflatedA !=.
keep if DeflatedARRAFinancedRDExpen ==.
	* 3384 dept observations dropped. 
drop ARRAFinancedRDExpendituresS DeflatedARRAFinancedRDExpen
** Clean up variable names
lab var year "University Fiscal Year"
rename AcademicInstitutionstandardiz university
lab var fice "FICE id"
rename Citylocationofinstitution city
rename Zip zipcode
replace zipcode = "21613" if fice == 19039
rename Control control
lab var control "Public or Private Academic Institution"
gen public = 1 if control == "Public"
replace public = 0 if control == "Private"
lab var hdg "Highest Degree Granted"
rename Carnegie94 carnegie94
lab var carnegie94 "1994 Carnegie Code"
rename Carnegie2005 carnegie05
lab var carnegie05 "2005 Carnegie Code"
rename WC_ccbasic carnegie05_basic 
lab var carnegie05_basic "2005 Carnegie Code (Basic)"
rename Carnegie2010 carnegie10
lab var carnegie10 "2010 Carnegie Code"
rename WC_CC2010 carnegie10_basic
lab var carnegie10_basic "2010 Carnegie Code (Basic)"
** NOTE DOLLAR AMTS ARE IN THOUSANDS **
rename StateLocalGovtFinancedRDE state_local_exp
rename DeflatedStateLocalGovtFinan state_local_exp_df
rename BusinessFinancedRDExpenditur business_exp
rename DeflatedBusinessFinancedRDE business_exp_df
rename NonprofitFinancedRDExpenditu nonprofit_exp
rename DeflatedNonprofitFinancedRD nonprofit_exp_df
rename InstitutionallyFinancedRDExp university_exp
rename DeflatedInstitutionallyFinance university_exp_df
rename RDExpendituresfundedbyAllO other_exp
rename DeflatedRDExpendituresfunded other_exp_df
lab var public "Public University (1)"
rename TotalRDExpendituresinAllFi total_exp
rename DeflatedTotalRDExpenditures total_exp_df
rename FederallyFinancedRDExpenditu fed_exp
rename DeflatedFederallyFinancedRD fed_exp_df
drop dept_to_id yr_to_id

** Adjust expenditures to log form
foreach x in state_local_exp state_local_exp_df business_exp business_exp_df nonprofit_exp nonprofit_exp_df university_exp university_exp_df other_exp other_exp_df total_exp total_exp_df fed_exp fed_exp_df {
set more off
** there are a number of missing values (584 observations)
	*br *df if state_local_exp_df ==.|business_exp_df==.| nonprofit_exp_df==.| university_exp_df==.| other_exp_df==.| total_exp_df==.| fed_exp_df==.
replace `x' = 0 if `x' ==.
gen `x'_log = `x'*1000
replace `x'_log = 1 if `x'_log==0
replace `x'_log = ln(`x'_log)
}
*!* Confirm log coding is correct (compare number of observations from this output) *!*
foreach x in state_local_exp total_exp university_exp nonprofit_exp other_exp fed_exp business_exp {
sum `x' `x'_log `x'_df `x'_df_log
}

** BROAD DEPARTMENT CLASSIFICATION ** based broad classification on survey: http://www.nsf.gov/statistics/srvyherd/surveys/srvyherd_2013.pdf
br dept dept_id
gen dept_broad = .
foreach x in dept_broad {
foreach y in dept_id {
** Engineering == 1
replace `x' = 1 if `y'==1|`y'==8|`y'==10|`y'==16|`y'==20|`y'==22|`y'==26
** Physical Sciences == 2
replace `x' = 2 if `y'==4|`y'==9|`y'==30|`y'==32
** Environmental Sciences == 3
replace `x' = 3 if `y'==5|`y'==13|`y'==25|`y'==27
** Mathematical Sciences == 4
replace `x' = 4 if `y'==21
** Computer Sciences == 5
replace `x' = 5 if `y'==12
** Life Sciences == 6
replace `x' = 6 if `y'==2|`y'==6|`y'==23|`y'==28
** Psychology == 7
replace `x' = 7 if `y'==34
** Social Sciences == 8
replace `x' = 8 if `y'==14|`y'==33|`y'==36|`y'==31
** Other Sciences == 9
replace `x' = 9 if `y'==18
** Other Non-Sciences (e.g. Humanities) == 10
replace `x' = 10 if `y'==7|`y'==35|`y'==29|`y'==24|`y'==19|`y'==17|`y'==15|`y'==11|`y'==3
}
}
lab var dept_broad "Eng1;Phys2;Env3;Math4;CS5;LS6;Psy7;SS8;OS9;non-S10"
save "/Users/llanahan/Dropbox/Uni Funding Sources/Data/webcaspar_table2010_2013/NCSES data 10_14.dta", replace

********************************************************************************
*STEP 2: NCSES MATCH TO IPEDS
********************************************************************************
********************************************************************************
** Step 2.1: Match NCSES to IPEDS ID; clean up university string variable
********************************************************************************
clear all 
global dir "/Users/llanahan/Dropbox/Uni Funding Sources/Data"
use "$dir/webcaspar_table2010_2013/NCSES data 10_14.dta"
	* 5-year panel: 119,304 observations
set more off

gen university_clean_full = trim(itrim(lower(university)))
gen university_to_clean = university_clean_full
** Clean up name of institution
foreach x in university_to_clean {
set more off
replace `x' = subinstr(`x', " of ", "", .)
replace `x' = subinstr(`x', " at ", "", .)
replace `x' = subinstr(`x', "coll ", "college", .)
replace `x' = subinstr(`x', "col ", "college", .)
replace `x' = subinstr(`x', " sci ", "science", .)
replace `x' = subinstr(`x', " the ", "", .)
replace `x' = subinstr(`x', " and ", "", .)
replace `x' = subinstr(`x', "&", "", .)
replace `x' = subinstr(`x', "university", "u", .)
replace `x' = subinstr(`x', "univ", "u", .)
replace `x' = subinstr(`x', "uni", "u", .)
replace `x' = subinstr(`x', "institute", "i", .)
replace `x' = subinstr(`x', "institution", "i", .)
replace `x' = subinstr(`x', "insts", "i", .)
replace `x' = subinstr(`x', "inst", "i", .)
replace `x' = subinstr(`x', " ins", "i", .)
replace `x' = subinstr(`x', "technology", "tech", .)
replace `x' = subinstr(`x', "-", "", .)
replace `x' = subinstr(`x', ",", "", .)
replace `x' = subinstr(`x', "/", "", .)
replace `x' = subinstr(`x', "(", "", .)
replace `x' = subinstr(`x', ")", "", .)
replace `x' = subinstr(`x', ".", "", .)
replace `x' = subinstr(`x', "state", "st", .)
replace `x' = subinstr(`x', " s ", "st", .)
replace `x' = subinstr(`x', "school", "sch", .)
replace `x' = subinstr(`x', "schl", "sch", .)
replace `x' = subinstr(`x', "centr", "center", .)
replace `x' = subinstr(`x', "ctr", "center", .)
replace `x' = subinstr(`x', "calif", "california", .)
replace `x' = subinstr(`x', "rsrve", "reserve", .)
replace `x' = subinstr(`x', "medical", "med", .)
replace `x' = subinstr(`x', "medicine", "med", .)
replace `x' = subinstr(`x', "ga ", "georgia", .)
replace `x' = subinstr(`x', "hlth", "health", .)
replace `x' = subinstr(`x', "laboratory", "lab", .)
replace `x' = subinstr(`x', "labs", "lab", .)
replace `x' = subinstr(`x', "physicians", "phys", .)
replace `x' = subinstr(`x', "physician", "phys", .)
replace `x' = subinstr(`x', "agriculture", "ag", .)
replace `x' = subinstr(`x', "agricultural", "ag", .)
replace `x' = subinstr(`x', " sci ", "science", .)
replace `x' = subinstr(`x', "sciences", "science", .)
replace `x' = subinstr(`x', "int'l", "international", .)
replace `x' = subinstr(`x', "intl", "international", .)
replace `x' = subinstr(`x', " engineering ", "eng", .)
replace `x' = subinstr(`x', " cnty ", "county", .)
replace `x' = subinstr(`x', " okla ", "oklahoma", .)
replace `x' = subinstr(`x', " va ", "virginia", .)
replace `x' = subinstr(`x', " ill ", "illinois", .)
replace `x' = subinstr(`x', "@", "", .)
replace `x' = subinstr(`x', "canada", "", .)
replace `x' = subinstr(`x', "polytechnic", "poly", .)
replace `x' = subinstr(`x', "polytech", "poly", .)
replace `x' = subinstr(`x', "saint", "st", .)
* remove spaces
gen `x'_nospace = subinstr(`x', " ","", .)
sort `x'_nospace
egen university_group_clean = group(`x')
egen university_group_clns = group(`x'_nospace)
}
rename university_to_clean university_cleaned
rename university_to_clean_nospace uni_clns
egen university_group_original = group(university_clean_full)
lab var university_group_original "Unique uni group ID, based on full NCSES university name"
lab var university_group_clean "Unique uni group ID, based on clean NCSES university name"
lab var university_group_clns "Unique uni group ID, based on clean NCSES university name with no spaces"
gen uni_clns_to_match = uni_clns
sort uni_clns_to_match
save "$dir/webcaspar_table2010_2013/NCSES data set 10_14 for IPEDS.dta", replace

********************************************************************************
** Step 2.2: Match IPEDS ID NCSES ** 
********************************************************************************
clear all
global dir "/Users/llanahan/Dropbox/Uni Funding Sources/Data"
use "$dir/webcaspar_table2010_2013/NCSES data set 10_14 for IPEDS.dta"
merge m:m uni_clns_to_match using "/Users/llanahan/Dropbox/Emerging Scholars/Data/IPEDS/IPEDS list clean round2.dta"
* matched 85,174 observations; 34,130 observatons remain to be matched
drop if _merge == 2
gen iped_match = 1 if _merge == 3
lab var iped_match "1: iped match w first merge"
gen ipeds_handmatch = .
foreach x in ipeds_handmatch {
foreach y in uni_clns_to_match {
set more off
replace `x' = 178402 if `y' == "umissourikansascity"
replace `x' = 178411 if `y' == "umissourirolla"|`y' == "missouriusciencetech"|`y' == "missouriurolla"
replace `x' = 178420 if `y' == "umissouristlouis"
replace `x' = 178396 if `y' == "missouriucolumbia"
replace `x' = 103112 if `y' == "alaskauof"|`y'=="ualaska"|`y'=="ualaskaanchorage"
replace `x' = 210562 if `y' == "alberteieincollegemed"
replace `x' = 201007 if `y' == "antiochnewenglandgradsch"
replace `x' = 104151 if `y' == "arizonastuphoenix"|`y'=="larizonastu"
replace `x' = 104179 if `y' == "arizonauof"|`y'=="uarizonadeptagbiosystemsengineering"
replace `x' = 106458 if `y' == "arkansasstu"|`y'=="arizonastu"
replace `x' = 100858 if `y' == "auburnu"
replace `x' = 164988 if `y' == "bostonugraduatesch"|`y'=="bostonucharlesrivercampus"|`y'=="bostonueristy"|`y'=="bostonugraduateschartsscience"|`y'=="bostonuschmed"|`y'=="trusteesbostonu"
replace `x' = 201441 if `y' == "bowlinggreenstu"
replace `x' = 190567 if `y' == "brooklyncollege"|`y'=="cityunewyork"|`y'=="cunygraduateschucenter"|`y'=="cunyhuntercollege"|`y'=="cunymtsinaischmed"|`y'=="cunyqueenscollege"|`y'=="huntercollegecuny"|`y'=="thegraduatecentercuny"
replace `x' = 110714 if `y' == "californiacruzlick"
replace `x' = 110422 if `y' == "californiaorniapolystufoundation"|`y'=="californiaorniapolystupomona"|`y'=="californiaorniapolystusanluisobi"
replace `x' = 112765 if `y' == "californiaorniastufullertonfoundation"|`y'=="californiaorniastuhayward"
replace `x' = 110635 if `y' == "ucberkeleyucsfjointbioengineeringprogram"|`y'=="ucberkeleyucsanfranciscojointbioengineeringprogram"|`y'=="ucberkeley"|`y'=="ucb"|`y'=="californiauberkeley"|`y'=="gyucaliforniaorniaberkeley"|`y'=="theucaliforniaorniaberkeley"|`y'=="ucaliforniaornia"|`y'=="ucaliforniaorniaberkeleyucsanfranciscojointpr"
replace `x' = 110644 if `y' == "californiaudavis"|`y'=="eucaliforniaorniadavis"|`y'=="theucaliforniaorniadavis"
replace `x' = 110653 if `y' == "californiauirvine"
replace `x' = 110662 if `y' == "californiaulosangeles"|`y'=="ucaliforniaorniaa"|`y'=="uclahighereducationresearchi"
replace `x' = 110671 if `y' == "californiauriverside"|`y'=="ucaliforniaorniariversidefoundation"
replace `x' = 110680 if `y' == "californiausandiego"|`y'=="gyucaliforniaorniasandiego"|`y'=="oucaliforniaorniasandiego"|`y'=="scrippsioceanographyucaliforniaorniasandiego"|`y'=="scrippsioceanographyucsandiego"|`y'=="scrippsioceanographyucsd"|`y'=="scrippsioceanography"|`y'=="theucaliforniaorniasandiego"|`y'=="ucaliforniaorniasandiegorevellecollege"|`y'=="ucaliforniaorniasandiegoschmed"|`y'=="ucaliforniaorniasandiegoscrippsiocean"|`y'=="ucaliforniaorniasandiegoscrippsioceanography"|`y'=="ucaliforniaorniasandiegosio"
replace `x' = 110705 if `y' == "californiausantabarb"|`y'=="ucaliforniaorniasantabarb"
replace `x' = 110714 if `y' == "californiausantacruz"|`y'=="theucaliforniaorniasantacruz"
replace `x' = 110404 if `y' == "calitech"|`y'=="ecaliforniaorniaitech"|`y'=="gycaliforniaorniaitech"|`y'=="californiaorniastpolyu"
replace `x' = 110699 if `y' == "calusanfrancisco"|`y'=="gyucaliforniaorniasanfrancisco"|`y'=="ucaliforniaorniacenterhealthscila"|`y'=="ucaliforniaorniamedcentsanfran"|`y'=="ucsf"
replace `x' = 173258 if `y' == "carletonu"
replace `x' = 211440 if `y' == "carnegiemellonitech"|`y'=="ucarnegiemellonu"
replace `x' = 201645 if `y' == "casewestrnreserve"
replace `x' = 144050 if `y' == "chicagouof"|`y'=="uchicagopritzkerschmed"
replace `x' = 153603 if `y' == "chiowastu"
replace `x' = 206260 if `y' == "cincinnatiuof"|`y'=="ucincinnati"|`y'=="ucincinnaticollegemed"
replace `x' = 112251 if `y' == "claremontgraduatesch"|`y'=="ranchosantaanabotanicgardenclaremontgraduateu"
replace `x' = 436377 if `y' == "coldspringharborlab"
replace `x' = 231624 if `y' == "collegewilliammaryvirginiaimarinescience"|`y'=="williammaryc"
replace `x' = 126614 if `y' == "coloradouboulder"|`y'=="ucolorado"|`y'=="uucolorado"
replace `x' = 126562 if `y' == "coloudenvercenter"|`y'=="ucoloradodenverhealthsciencecenter"
replace `x' = 190150 if `y' == "columbaphyssurg"|`y'=="columbiabarnard"|`y'=="columbiau"|`y'=="columbiaucollegephysurg"|`y'=="columbiaucolphyssurgeons"|`y'=="columbiaucphys"|`y'=="columbiaulamontdohertyearthobservatory"|`y'=="columbiaupresbymedcenter"|`y'=="columbiauteacherscollege"|`y'=="teacherscollegecolumbiau"
replace `x' = 129020 if `y' == "uconnecticutmarinesciencei"|`y'=="connecticutuof"|`y'=="connuhealthcenter"|`y'=="uconnecticutaverypoint"|`y'=="uconnecticuthealthcenter"|`y'=="uconnecticutstorrs"
replace `x' = 190406 if `y' == "cornell"|`y'=="ecornellu"|`y'=="gycornellu"
replace `x' = 190424 if `y' == "weillcornellmedsch"|`y'=="weillgraduatecollegemedsciencecornellu"|`y'=="weillgraduateschmedsciencecornellu"|`y'=="cornelluweillcornellmedcollege"|`y'=="cornellweillgraduateschmedscience"|`y'=="joansanfordiweillmedcollegecornellu"
replace `x' = 190433 if `y' == "cornelluendowed"|`y'=="cornellumedcampus"|`y'=="cornellumedcol"|`y'=="cornellust"
replace `x' = 193900 if `y' == "courantimathscience"
replace `x' = 182670 if `y' == "dartmouthcollegethayerschengineering"|`y'=="dartmouthmedsch"
replace `x' = 130943 if `y' == "delawareuof"|`y'=="udelawarecollegemarinestudies"
replace `x' = 198419 if `y' == "dukeumedcenter"|`y'=="dukeuschmed"
replace `x' = 166027 if `y' == "harvardcollegepresidentfellowsof"|`y'=="eharvardu"|`y'=="gyharvardu"|`y'=="gyharvardumedsch"|`y'=="harvardmassachusettsitech"|`y'=="harvardmedsch"|`y'=="harvardmitdivhealthsci"|`y'=="harvardmitdivisionhealthsciencetech"|`y'=="harvardmitprog"|`y'=="harvardmitproghealthsci"|`y'=="harvardumedsch"|`y'=="harvarduschpublichealth"|`y'=="harvarduunk"|`y'=="lharvardu"|`y'=="paharvardu"|`y'=="sharvardu"
replace `x' = 139658 if `y' == "emoryuyerkesregionalprimateresearchcenter"
replace `x' = 240444 if `y' == "euwisconsinmadison"
replace `x' = 221999 if `y' == "evanderbiltu"|`y'=="vanderbiltuschmed"|`y'=="vanderbiltumedcenter"
replace `x' = 133650 if `y' == "floridaagmechu"
replace `x' = 134130 if `y' == "floridauof"|`y'=="gyuflorida"|`y'=="ufloridaifoodagric"
replace `x' = 139755 if `y' == "geogiaitech"|`y'=="georgiaiitutetech"|`y'=="georgiaitech"|`y'=="georgiaitechgeorgiatech"|`y'=="georgiatechresearchcorporation"|`y'=="georgiatechresearchcorporationgeorgiaitech"|`y'=="gygeorgiaitech"|`y'=="jointgeorgiaitech"|`y'=="medcollegegeorgiaresearchiinc"|`y'=="thegeorgiaitech"|`y'=="ugeorgiaitech"
replace `x' = 139959 if `y' == "georgiauof"
replace `x' = 131469 if `y' == "geowashingtonu"|`y'=="georgewashingtonuthe"
replace `x' = 166683 if `y' == "gymassachusettsitech"|`y'=="lmassachusettsitech"|`y'=="massachussettsitech"|`y'=="massitech"|`y'=="mit"|`y'=="omassachusettsitech"|`y'=="umassachusettsitech"
replace `x' = 186131 if `y' == "gyprincetonu"
replace `x' = 243744 if `y' == "ystanfordu"|`y'=="gystanfordu"|`y'=="lstanfordu"|`y'=="ostanfordu"|`y'=="stanfordfdresi"|`y'=="stanfordschmed"|`y'=="stanforduhopkinsmarinestation"|`y'=="stanforduscheng"|`y'=="stanforduschmed"
replace `x' = 104179 if `y' == "gyuarizona"
replace `x' = 174066 if `y' == "gyuminnesotaminneapolis"|`y'=="minnesotauminneapl"|`y'=="uminnesota"|`y'=="uminnesotaitech"|`y'=="uminnesotaminneapolis"
replace `x' = 174066 if `y' == "uminnesotatwincit"
replace `x' = 215062 if `y' == "gyupennsylvania"|`y'=="pennsylvaniauof"
replace `x' = 236948 if `y' == "gyuwashington"|`y'=="uwgraduatesch"
replace `x' = 130794 if `y' == "gyyaleu"|`y'=="yaleuschforestryenvironmentalstudies"|`y'=="yaleuschmed"|`y'=="yaleuyaleschmed"
replace `x' = 141963 if `y' == "hawaiiuof"|`y'=="hawaiiusystemoff"|`y'=="uhawaii"|`y'=="uhawaiihonolulucc"
replace `x' = 225511 if `y' == "houstonuof"
replace `x' = 115755 if `y' == "humboldtstufoundation"
replace `x' = 142285 if `y' == "idahouof"
replace `x' = 145813 if `y' == "illinoisstunormal"|`y'=="illinoisuunknown"
replace `x' = 145637 if `y' == "illuurbanachamp"|`y'=="luillinoisurbanachampaign"|`y'=="uillinoisurbanachampaign"
replace `x' = 151351 if `y' == "indianau"|`y'=="indianaubloomngton"
replace `x' = 151102 if `y' == "indianaupurduefortwayne"
replace `x' = 151111 if `y' == "indianauschmed"
replace `x' = 153658 if `y' == "iowauof"|`y'=="uiowacollegemed"
replace `x' = 162928 if `y' == "johnshopkinsbloombergschpublichealth"|`y'=="jhopkinsmedi"|`y'=="johnshopkinsmedi"|`y'=="johnshopkinsschmed"|`y'=="johnshopkinsukriegerschartsscience"|`y'=="johnshopkinsumedi"|`y'=="johnshopkinsupeabodyi"|`y'=="johnshopkinsuschhygienepublichealth"|`y'=="johnshopkinsuschmed"
replace `x' = 155256 if `y' == "kansasuof"|`y'=="ukansasmaincampus"
replace `x' = 203517 if `y' == "kentstu"
replace `x' = 157085 if `y' == "kentuckyuof"|`y'=="ukentuckylexington"|`y'=="ukentuckyresearchfoundation"
replace `x' = 165334 if `y' == "lclarku"
replace `x' = 146676 if `y' == "lincolnc"
replace `x' = 159391 if `y' == "louisianastuamcollege"
replace `x' = 159373 if `y' == "louisianastuschmedneworl"
replace `x' = 435000 if `y' == "louisianastuschmedshrevep"
replace `x' = 159647 if `y' == "louisianatechunversity"
replace `x' = 157289 if `y' == "louisvilleuof"|`y'=="ulouisvilleresearchfoundationinc"|`y'=="ulouisvilleschmed"
replace `x' = 141574 if `y' == "hawaiiimarinebiologyuhawaiimanoa"
replace `x' = 435338 if `y' == "lscrippsresearchi"
replace `x' = 170976 if `y' == "lumichigan"|`y'=="michiganuof"|`y'=="umichigan"|`y'=="umichiganmedsch"
replace `x' = 161253 if `y' == "maineuorono"|`y'=="umaineorono"
replace `x' = 163268 if `y' == "marylandubaltcnty"
replace `x' = 163286 if `y' == "marylanduof"|`y'=="umaryland"
replace `x' = 232681 if `y' == "marywashingtoncollege"
replace `x' = 166629 if `y' == "massuofamherst"|`y'=="umassachusettsamherst"|`y'=="umassachusettsmedsch"
replace `x' = 167987 if `y' == "umassachusettsdartmouth"
replace `x' = 166638 if `y' == "umassachusettsharborcampus"
replace `x' = 166513 if `y' == "umassachusettslowell"
replace `x' = 166708 if `y' == "umassachusettsworcester"
replace `x' = 173948 if `y' == "mayoclinicrochester"|`y'=="mayograduatesch"
replace `x' = 204024 if `y' == "miamiu"|`y'=="miamiuoxfordcampus"
replace `x' = 135726 if `y' == "miamiuof"|`y'=="umiamirosenstielschmarineatmosphericsci"|`y'=="umiamischmed"
replace `x' = 171128 if `y' == "michigantechu"
replace `x' = 171137 if `y' == "michiganudearborn"
replace `x' = 180489 if `y' == "montanauof"|`y'=="umontana"|`y'=="umontanawestern"
replace `x' = 199102 if `y' == "ncagtechstu"|`y'=="northcarolinaagtechnicalstu"
replace `x' = 199193 if `y' == "ncsturaleigh"|`y'=="northcarolinastu"
replace `x' = 199120 if `y' == "ncuofchapelhill"|`y'=="unorthcarolina"|`y'=="theunorthcarolinachapelhill"
replace `x' = 199218 if `y' == "ncuofwilmington"
replace `x' = 181464 if `y' == "nebraskaulincoln"
replace `x' = 181428 if `y' == "nebraskaumedcenter"
replace `x' = 182290 if `y' == "nevadauof"|`y'=="unevada"|`y'=="unevadarenofleischmannplanetarium"
replace `x' = 183044 if `y' == "newhampshireuof"|`y'=="unewhampshire"|`y'=="unewhampshiredurham"
replace `x' = 188012 if `y' == "newmexicostu"
replace `x' = 187985 if `y' == "newmexicouof"|`y'=="unewmexico"
replace `x' = 193654 if `y' == "newschforsocialresearch"|`y'=="newschu"
replace `x' = 194152 if `y' == "newyorkstcollegeceramicsalfredu"
replace `x' = 193900 if `y' == "newyorkucourantimathematicalscience"|`y'=="newyorkumedcenter"|`y'=="newyorkuschmed"
replace `x' = 187967 if `y' == "nmeximiningtech"
replace `x' = 200332 if `y' == "northdakotastu"|`y'=="northdakotastufargo"
replace `x' = 147749 if `y' == "northwesternuchicagocampus"|`y'=="northwesternuchicago"
replace `x' = 152080 if `y' == "notredameuof"
replace `x' = 136215 if `y' == "novaseuoceancenter"
replace `x' = 204796 if `y' == "ohiostu"|`y'=="ohiosturesearchfoundation"|`y'=="theohiostu"
replace `x' = 204857 if `y' == "ohiou"
replace `x' = 207388 if `y' == "oklahomastu"
replace `x' = 207430 if `y' == "oklahomauof"|`y'=="oklauhealthsci"|`y'=="uoklahoma"
replace `x' = 209551 if `y' == "oregonuof"|`y'=="uoregoneugene"
replace `x' = 228778 if `y' == "outexasaustin"|`y'=="texasuaustin"|`y'=="utexasaustin"
replace `x' = 214777 if `y' == "pennsylvaniastu"|`y'=="thepennsylvaniastu"|`y'=="pennsylvaniastuupark"|`y'=="pennstcollegemed"|`y'=="pennstuupark"|`y'=="pennstuhershymed"|`y'=="pennstuhersheymedcntr"|`y'=="pennstugreatvalley"|`y'=="pennstucapitalcollege"|`y'=="pennstu"|`y'=="pennsylvaniastucommonwealthcollege"|`y'=="schreyerhonorscollegepennsylvaniastu"
replace `x' = 214227 if `y' == "pennsylvaniastberkslehighvalleycollege"
replace `x' = 215293 if `y' == "pittsburghuof"|`y'=="upittsburgh"
replace `x' = 194541 if `y' == "polyunewyork"
replace `x' = 152266 if `y' == "purdueunorthcenteral"
replace `x' = 243780 if `y' == "purdueuscheng"
replace `x' = 217484 if `y' == "rhodeislanduof"|`y'=="urhodeislandschoceanograph"
replace `x' = 195030 if `y' == "rochesteruof"|`y'=="urochestermedsch"
replace `x' = 186371 if `y' == "rutgersstunewark"|`y'=="rutgersstunewbrunswick"|`y'=="rutgersu"|`y'=="rutgersunewark"|`y'=="rutgersunewbrunswick"
replace `x' = 122436 if `y' == "sandiegouof"
replace `x' = 122409 if `y' == "sandiegostufoundation"|`y'=="sandiegostuucaliforniaorniadavis"
replace `x' = 435338 if `y' == "lscrippsresearchi"|`y'=="thescrippsresearchi"|`y'=="scrippsresi"|`y'=="thescrippsresearchiflorida"|`y'=="thescrippsresearchikelloggschsciencetech"
replace `x' = 219347 if `y' == "sdschminestech"
replace `x' = 218663 if `y' == "southcarolinauof"|`y'=="usouthcarolina"|`y'=="usouthcarolinaschmed"
replace `x' = 123961 if `y' == "southerncaliforniauof"|`y'=="usoutherncallosangeles"
replace `x' = 149222 if `y' == "southernillinoisu"
replace `x' = 228246 if `y' == "sthrnmethodistu"
replace `x' = 179159 if `y' == "stlouisu"
replace `x' = 196255 if `y' == "stunewyork"|`y'=="theuthestnewyork"
replace `x' = 196130 if `y' == "sunybuffalo"
replace `x' = 196282 if `y' == "stunystonybrook"|`y'=="sunystbrkhthsci"|`y'=="sunystonybrook"
replace `x' = 196103 if `y' == "sunyenvironscienceforestry"
replace `x' = 196307 if `y' == "sunyupstmedu"
replace `x' = 221759 if `y' == "tennuknoxville"
replace `x' = 228723 if `y' == "texasamumaincampus"|`y'=="ustexasamu"
replace `x' = 229090 if `y' == "texasudallas"|`y'=="texasuelpaso"
replace `x' = 195049 if `y' == "therockefelleru"
replace `x' = 199139 if `y' == "theunorthcarolinacharlotte"
replace `x' = 221759 if `y' == "theutennesseememphishealthsciencecenter"
replace `x' = 216366 if `y' == "thomasjeffrsonu"
replace `x' = 168148 if `y' == "tuftsuschmed"
replace `x' = 160755 if `y' == "tulaneu"|`y'=="tulaneula"|`y'=="tulaneuschmed"
replace `x' = 200800 if `y' == "uakron"
replace `x' = 100733 if `y' == "ualabama"|`y'=="ualabamatuscaloosa"
replace `x' = 100706 if `y' == "ualabamahuntsville"|`y'=="theualabamainhuntsville"
replace `x' = 102614 if `y' == "ualaskafairbankscampus"|`y'=="ualaskafairbanksjuneaucampus"
replace `x' = 106397 if `y' == "uarkansasfayetteville"|`y'=="uarkansasmedscience"|`y'=="uarkansasmedsciencecampus"
replace `x' = 445188 if `y' == "ucmerced"
replace `x' = 145600 if `y' == "uillinois"
replace `x' = 176017 if `y' == "umississippi"
replace `x' = 181428 if `y' == "unebraskaomaha"
replace `x' = 200280 if `y' == "unorthdakotamaincampus"
replace `x' = 215062 if `y' == "upaschmed"|`y'=="upennsylvaniaschmed"|`y'=="upennsylvaniawhartonschfinancecom"
replace `x' = 251154 if `y' == "upuertoricomayaguez"|`y'=="prumayaguez"|`y'=="upuertoricoriopiedras"|`y'=="upuertoricomedsci"
replace `x' = 181002 if `y' == "uscreightonu"
replace `x' = 448840 if `y' == "usouthfloridastpetersburg"
replace `x' = 230764 if `y' == "utahuof"
replace `x' = 221759 if `y' == "utennesseehealthsciencecenter"|`y'=="utennesseeknoxville"|`y'=="utennesseespacei"
replace `x' = 229090 if `y' == "utexasarlington"|`y'=="utexasdallas"|`y'=="utexaselpaso"|`y'=="utexasmedbrchgalveston"
replace `x' = 416801 if `y' == "utexascancercenterhouston"|`y'=="utexashealthsciencecenterhouston"|`y'=="utexashealthscihouston"|`y'=="utexashealthsciencecentersanantonio"|`y'=="utexashealthscisanantonio"|`y'=="utexassanantonio"|`y'=="utexassouthwesternmedcenter"|`y'=="utexassouthwesternmedcenterdallas"|`y'=="utsouthwesternmedcenter"
replace `x' = 231174 if `y' == "uvermontburlington"|`y'=="uvermontstagcollege"|`y'=="vermontuof"
replace `x' = 234076 if `y' == "uvirginia"|`y'=="uvirginiacharlottesville"|`y'=="virginiauof"
replace `x' = 233921 if `y' == "uvirginiapolyistu"|`y'=="vapolyistu"|`y'=="vapolyitateu"|`y'=="virginiatech"|`y'=="virginiapolyitateun"
replace `x' = 236948 if `y' == "uwashington"|`y'=="uwashingtonschmed"|`y'=="washingtonuof"
replace `x' = 240055 if `y' == "uwisconsin"|`y'=="uwisconsinschmed"|`y'=="wisconsinumadison"
replace `x' = 234030 if `y' == "virginiacommonwealthumedcoll"
replace `x' = 199847 if `y' == "wakeforestubowmangrayschmed"
replace `x' = 236939 if `y' == "washingtonstuspokane"|`y'=="washingtonstuvancouver"
replace `x' = 179867 if `y' == "washingtonuinstlouisschmed"|`y'=="washingtonu"|`y'=="washingtonuschmed"
replace `x' = 238032 if `y' == "westvirginiauresearchcorporation"
replace `x' = 227757 if `y' == "williammarshriceu"
replace `x' = 166610 if `y' == "woodsholeoceanographic"|`y'=="woodsholeoceanographici"|`y'=="woodsholeoceanographici+mitjointprogram"|`y'=="woodsholeoceanographicibiologicaloceanography"|`y'=="woodsholeocni"
replace `x' = 206598 if `y' == "wrightstu"
replace `x' = 240727 if `y' == "wyominguof"
replace `x' = 197708 if `y' == "yeshivauaeieincollegemed"
replace `x' = 219471 if `y' == "usouthdakotamaincampus"
replace `x' = 194310 if `y' == "paceu"
replace `x' = 213020 if `y' == "indianaupennsylvania"
replace `x' = 206589 if `y' == "collegewooster"
replace `x' = 110422 if `y' == "californiaorniapolystu"
replace `x' = 206084 if `y' == "utoledo"
replace `x' = 121628 if `y' == "pardeerandgraduatesch"
}
}
sum ipeds_handmatch if iped_match == 0
br ipeds_handmatch if iped_match == 0
** 6,966 handmatched using GRFP code, 27,164 remain to be matched
replace iped_match = 2 if ipeds_handmatch  > 0 & ipeds_handmatch !=.
lab var iped_match "1: match w first merge; 2: GRFP handmatch"
********************************************************************************
** Step 2.3: Handmatch NCSES to IPEDS ID: http://nces.ed.gov/globallocator/
********************************************************************************
foreach x in ipeds_handmatch {
foreach y in uni_clns_to_match {
set more off
replace `x' = 210492 if `y' == "academythenewchurchcollege"
replace `x' = 100654 if `y' == "alabamaagmechanicalu"
replace `x' = 188641 if `y' == "alfreduallcampuses"
replace `x' = 104151 if `y' == "arizonastumain"
replace `x' = 106458 if `y' == "arkansasstuallcampuses"
replace `x' = 100858 if `y' == "auburnuallcampuses"
replace `x' = 143084 if `y' == "augustanacollegerockislandil"
replace `x' = 219000 if `y' == "augustanacollegesiouxfallssd"
replace `x' = 156286 if `y' == "bellarminecollege"
replace `x' = 219046 if `y' == "blackhillsstuspearfishsd"
replace `x' = 201441 if `y' == "bowlinggreenstuallcampuses"
replace `x' = 230038 if `y' == "brighamyounguallcampuses"
replace `x' = 196060 if `y' == "cnanoscalesetheualbanysuny"
replace `x' = 190512 if `y' == "cunybernardmbaruchcollege"
replace `x' = 190549 if `y' == "cunybrooklyncollege"
replace `x' = 190558 if `y' == "cunycollegestnisland"
replace `x' = 190637 if `y' == "cunyherberthlehmancollege"
replace `x' = 190600 if `y' == "cunyjohnjaycollegecriminaljustice"
replace `x' = 190646 if `y' == "cunymedgareverscollege"
replace `x' = 190655 if `y' == "cunynewyorkcitytechnicalcollege"
replace `x' = 190691 if `y' == "cunyyorkcollege"
replace `x' = 190567 if `y' == "cunyallcampuses"
replace `x' = 110422 if `y' == "californiaorniastpolyusanluisobispo"
replace `x' = 241793 if `y' == "caribbeancenterforadvancedstudies"
replace `x' = 163286 if `y' == "centerforenvestuarinestudiesumd"
replace `x' = 176965 if `y' == "centeralmissouristu"
replace `x' = 111966 if `y' == "charlesrdrewumedsci"
replace `x' = 217873 if `y' == "claflincollege"
replace `x' = 241386 if `y' == "colegiouersitariodeleste"
replace `x' = 231624 if `y' == "collegewilliammaryallcampuses"
replace `x' = 144281 if `y' == "columbiacollegechicagoil"
replace `x' = 456542 if `y' == "commonwealthmedcollegethe"
replace `x' = 190372 if `y' == "cooperuon"
replace `x' = 190415 if `y' == "cornelluallcampuses"
replace `x' = 187648 if `y' == "easternnewmexicouallcampuses"
replace `x' = 208646 if `y' == "easternoregonstcollege"
replace `x' = 202648 if `y' == "edisoncommutycollege"
replace `x' = 198516 if `y' == "eloncollege"
replace `x' = 426314 if `y' == "embryriddleaeronauticalufl"|`y'=="embryriddleaeronauticalu"
replace `x' = 235167 if `y' == "evergreenstcollege"
replace `x' = 184603 if `y' == "fairleighdickinsonuallcampuses"
replace `x' = 133881 if `y' == "floridaitechmelbournefl"
replace `x' = 191241 if `y' == "fordhamubronxny"
replace `x' = 481030 if `y' == "futuregenerationsgraduatesch"
replace `x' = 139755 if `y' == "georgiaitechallcampuses"
replace `x' = 482149 if `y' == "georgiaregentsu"
replace `x' = 107044 if `y' == "hardinguallcampuses"
replace `x' = 151324 if `y' == "indianastuallcampuses"
replace `x' = 213020 if `y' == "indianaupaallcampuses"
replace `x' = 151351 if `y' == "indianauallcampuses"
replace `x' = 185129 if `y' == "jerseycitystcollege"
replace `x' = 177719 if `y' == "jewishhospitalcollegenursingalliedhealth"
replace `x' = 230913 if `y' == "johnsonstcollege"
replace `x' = 183062 if `y' == "keenestcollege"
replace `x' = 203517 if `y' == "kentstuallcampuses"
replace `x' = 226091 if `y' == "lamarubeaumont"
replace `x' = 177940 if `y' == "lincolnujeffersoncitymo"
replace `x' = 213598 if `y' == "lincolnulincolnupa"
replace `x' = 209065 if `y' == "linfieldcollegemcminnvilleor"
replace `x' = 159391 if `y' == "louisianastuallcampuses"
replace `x' = 163046 if `y' == "loyolacollege"
replace `x' = 173920 if `y' == "mankatostu"
replace `x' = 213826 if `y' == "marywoodcollege"
replace `x' = 140447 if `y' == "merceruallcampuses"
replace `x' = 193016 if `y' == "mercycollegedobbsferryny"
replace `x' = 204024 if `y' == "miamiuallcampuses"
replace `x' = 143853 if `y' == "midwesternudownersgroveil"
replace `x' = 200253 if `y' == "minotstu"
replace `x' = 17355902 if `y' == "moorheadstu"
replace `x' = 140553 if `y' == "morehousecollegemed"
replace `x' = 119605 if `y' == "nationalusandiegoca"
replace `x' = 262129 if `y' == "newcollegetheusouthflorida"
replace `x' = 188030 if `y' == "newmexicostuallcampuses"
replace `x' = 194091 if `y' == "newyorkitechallcampuses"
replace `x' = 199102 if `y' == "northcarolinaagtechstu"
replace `x' = 200332 if `y' == "northdakotastuallcampuses"
replace `x' = 482680 if `y' == "northgeorgiacollege"
replace `x' = 159993 if `y' == "northeastlouisianau"
replace `x' = 142461 if `y' == "northwestnazarenecollege"
replace `x' = 160038 if `y' == "northweststu"
replace `x' = 204796 if `y' == "ohiostuallcampuses"
replace `x' = 204857 if `y' == "ohiouallcampuses"
replace `x' = 207388 if `y' == "oklahomastuallcampuses"
replace `x' = 147828 if `y' == "olivetu"
replace `x' = 194310 if `y' == "paceuallcampuses"
replace `x' = 214777 if `y' == "pennsylvaniastuallcampuses"
replace `x' = 214786 if `y' == "pennsylvaniastumckeesportcampus"
replace `x' = 121150 if `y' == "pepperdineumalibuca"
replace `x' = 215123 if `y' == "philadelphiacollegepharmacyscience"
replace `x' = 183080 if `y' == "plymouthstcollege"
replace `x' = 243780 if `y' == "purdueuallcampuses"
replace `x' = 130226 if `y' == "quinnipiaccollege"
replace `x' = 445735 if `y' == "rosemanuhealthscience"
replace `x' = 184782 if `y' == "rowancollegenewjersey"
replace `x' = 186380 if `y' == "rutgersstunjallcampuses"
replace `x' = 196158 if `y' == "sunycollegefredonia"
replace `x' = 196033 if `y' == "sunycollegeagrictechcobleskill"
replace `x' = 196051 if `y' == "sunycollegeagtechmorrisville"
replace `x' = 196219 if `y' == "sunypurchasecollege"
replace `x' = 196130 if `y' == "sunybuffaloallcampuses"
replace `x' = 196042 if `y' == "sunyfarmingdale"
replace `x' = 196097 if `y' == "sunystonybrookallcampuses"
replace `x' = 163851 if `y' == "salisburystu"
replace `x' = 122409 if `y' == "sandiegostusandiegoca"
replace `x' = 481535 if `y' == "sanfordburnhammedrschigradschbiomedsci"
replace `x' = 124283 if `y' == "schtheologyclaremont"
replace `x' = 195474 if `y' == "sienacollegeloudonvilleny"
replace `x' = 163921 if `y' == "sojournerdouglascollege"
replace `x' = 107983 if `y' == "southernarkansasuallcampuses"
replace `x' = 123651 if `y' == "southerncaliforniaorniacollege"
replace `x' = 210146 if `y' == "southernoregonstcollege"
replace `x' = 179566 if `y' == "southwestmissouristu"
replace `x' = 215743 if `y' == "stfranciscollegelorettopa"
replace `x' = 174792 if `y' == "stjohn'sucollegevillemn"
replace `x' = 195809 if `y' == "stjohn'sujamaicany"
replace `x' = 161518 if `y' == "stjoseph'scollegewindhamme"
replace `x' = 179159 if `y' == "stlouisuallcampuses"
replace `x' = 228149 if `y' == "stmary'su"
replace `x' = 243744 if `y' == "stanfordustanfordca"
replace `x' = 196176 if `y' == "stunewyorknewplatz"
replace `x' = 228501 if `y' == "sulrossstualpinetx"
replace `x' = 196413 if `y' == "syracuseuallcampuses"
replace `x' = 228723 if `y' == "texasamuallcampuses"
replace `x' = 195128 if `y' == "thesagecollegessystemoffice"
replace `x' = 196592 if `y' == "tourocollegenewyorkny"
replace `x' = 164076 if `y' == "towsonstu"
replace `x' = 130590 if `y' == "trinitycollegehartfordct"
replace `x' = 446932 if `y' == "uoncollegeschenectadyny"
replace `x' = 102553 if `y' == "uakanchorageallcampuses"
replace `x' = 102614 if `y' == "uakfairbanksallcampuses"
replace `x' = 102632 if `y' == "uaksoutheast"
replace `x' = 200800 if `y' == "uakronallcampuses"|`y'=="uakronthe"
replace `x' = 128744 if `y' == "ubridgeportbridgeportct"
replace `x' = 110699 if `y' == "ucaliforniaorniaofficepresident"
replace `x' = 201885 if `y' == "ucincinnatiallcampuses"
replace `x' = 126614 if `y' == "ucoloradoallcampuses"
replace `x' = 129020 if `y' == "uconnecticutallcampuses"
replace `x' = 148654 if `y' == "uillinoisspringfield"
replace `x' = 155317 if `y' == "ukansasallcampuses"
replace `x' = 157085 if `y' == "ukentuckyallcampuses"
replace `x' = 166629 if `y' == "umassachusettscenteraloffice"
replace `x' = 170976 if `y' == "umichiganallcampuses"
replace `x' = 174066 if `y' == "uminnesotaallcampuses"
replace `x' = 176017 if `y' == "umississippiallcampuses"
replace `x' = 181464 if `y' == "unebraskacenteraladminsysoff"
replace `x' = 187985 if `y' == "unewmexicoallcampuses"
replace `x' = 200280 if `y' == "unorthdakotaallcampuses"
replace `x' = 227216 if `y' == "unorthtx"
replace `x' = 228909 if `y' == "unorthtxhealthsciencecenterftworth"
replace `x' = 127741 if `y' == "unortherncoloradogreeleyco"
replace `x' = 207500 if `y' == "uoklahomaallcampuses"
replace `x' = 154156 if `y' == "uosteopathicmedhealthsci"
replace `x' = 243151 if `y' == "uprcayeyucollege"
replace `x' = 243179 if `y' == "uprhumacaoucollege"
replace `x' = 243197 if `y' == "uprmayaguezcampus"
replace `x' = 243203 if `y' == "uprmedsciencecampus"
replace `x' = 243212 if `y' == "uprponcetechucol"
replace `x' = 243221 if `y' == "uprriopiedrascampus"
replace `x' = 215062 if `y' == "upennsylvaniaphiladelphiapa"
replace `x' = 215266 if `y' == "upittsburghbradford"
replace `x' = 215293 if `y' == "upittsburghallcampuses"
replace `x' = 218663 if `y' == "usouthcarolinaallcampuses"
replace `x' = 137351 if `y' == "usouthflorida"
replace `x' = 123961 if `y' == "usoutherncaliforniaorniaca"
replace `x' = 128106 if `y' == "usoutherncolorado"
replace `x' = 161554 if `y' == "usouthernmaine"
replace `x' = 176372 if `y' == "usouthernmississippims"
replace `x' = 160658 if `y' == "usouthwesternlouisiana"
replace `x' = 174914 if `y' == "ustthomasstpaulmn"
replace `x' = 137847 if `y' == "utampa"
replace `x' = 221759 if `y' == "utennesseeuwideadmcentoff"
replace `x' = 227368 if `y' == "utexaspanamerican"
replace `x' = 416801 if `y' == "utexasmdandersoncancercenter"
replace `x' = 228653 if `y' == "utexasmedbranchgalveston"
replace `x' = 229018 if `y' == "utexaspermianbasin"
replace `x' = 228802 if `y' == "utexastyler"
replace `x' = 234076 if `y' == "uvirginiaallcampuses"
replace `x' = 236948 if `y' == "uwashingtonseattle"
replace `x' = 138354 if `y' == "uwestflorida"
replace `x' = 221519 if `y' == "uthesouth"
replace `x' = 197045 if `y' == "uticacollegesyracuseu"
replace `x' = 156082 if `y' == "washburnutopeka"
replace `x' = 236939 if `y' == "washingtonstupullmanwa"
replace `x' = 237932 if `y' == "westlibertystcollege"
replace `x' = 237701 if `y' == "westvirginiastcollege"
replace `x' = 180692 if `y' == "westernmontanacollege"
replace `x' = 210429 if `y' == "westernoregonstcollege"
replace `x' = 168281 if `y' == "wheatoncollegenortonma"
replace `x' = 149781 if `y' == "wheatoncollegewheatonil"
replace `x' = 216852 if `y' == "wideneruallcampuses"
replace `x' = 187444 if `y' == "williampatersoncollege"
replace `x' = 206604 if `y' == "wrightstuallcampuses"
replace `x' = 197708 if `y' == "yeshivaunewyorkny"
replace `x' = -99 if `y' == "59thdentaltrainingsquadron"
replace `x' = 177834 if `y' == "atstillu"
replace `x' = 110440 if `y' == "alliantinternationalusandiego"
replace `x' = 109642 if `y' == "armstrongstu"
replace `x' = -99 if `y' == "atlantau"
replace `x' = 143084 if `y' == "augustanacollegerockisland"
replace `x' = 219000 if `y' == "augustanacollegesiouxfalls"
replace `x' = 409111 if `y' == "barnesjewishcollegegoldfarbschnursing"
replace `x' = -99 if `y' == "bostonstcollege"
replace `x' = 165024 if `y' == "bridgewaterstu"
replace `x' = 210492 if `y' == "brynathyncollege"
replace `x' = 190512 if `y' == "cunybaruchcollege"
replace `x' = 190576 if `y' == "cunygraduatecenter"
replace `x' = 190637 if `y' == "cunylehmancollege"
replace `x' = 190655 if `y' == "cunynewyorkcitycollegetech"
replace `x' = -99 if `y' == "cunysystemoffice"
replace `x' = 111188 if `y' == "californiaorniamaritimeacademythe"
replace `x' = 241331 if `y' == "carlosalbizuusanjuan"
replace `x' = 131283 if `y' == "catholicuamericathe"
replace `x' = 111966 if `y' == "charlesrdrewumedscience"
replace `x' = 143978 if `y' == "chicagoschprofessionalpsychologythe"
replace `x' = 187134 if `y' == "collegenewjerseythe"
replace `x' = 206589 if `y' == "collegewoosterthe"
replace `x' = 127556 if `y' == "coloradomesau"
replace `x' = 126818 if `y' == "coloradostufortcollins"
replace `x' = 190372 if `y' == "cooperuonforadvancementscienceartthe"
replace `x' = 154156 if `y' == "desmoinesu"
replace `x' = 187648 if `y' == "easternnewmexicouportales"
replace `x' = 442806 if `y' == "edwardviacollegeosteopathicmed"
replace `x' = 235167 if `y' == "evergreenstcollegethe"
replace `x' = 184694 if `y' == "fairleighdickinsonu"
replace `x' = 165820 if `y' == "fitchburgstu"
replace `x' = 114840 if `y' == "fullertheologicalseminary"
replace `x' = -99 if `y' == "georgepeabodycollegeforteachers"
replace `x' = -99 if `y' == "hawaiiloacollege"
replace `x' = 193405 if `y' == "icahnschmedmountsinai"
replace `x' = -99 if `y' == "indianauregionalcampussystem"
replace `x' = 213020 if `y' == "indianaupennsylvaniaindiana"
replace `x' = -99 if `y' == "indianaugary"
replace `x' = 117627 if `y' == "lasierrau"
replace `x' = 213598 if `y' == "lincolnuthecommonwealthpennsylvania"
replace `x' = 177940 if `y' == "lincolnujeffersoncity"
replace `x' = -99 if `y' == "longislanducenteraloffice"
replace `x' = 159416 if `y' == "louisianastu"
replace `x' = 440952 if `y' == "lowelltechnologicali"
replace `x' = 163046 if `y' == "loyolaumaryland"
replace `x' = 117946 if `y' == "loyolaulosangeles"
replace `x' = 123943 if `y' == "marshallbketchumu"
replace `x' = -99 if `y' == "memorialsloanketteringcancercenterlouisvgerstnerjrgraduateschbiomedscience"
replace `x' = 193016 if `y' == "mercycollege"
replace `x' = 213987 if `y' == "mercyhurstu"
replace `x' = 143853 if `y' == "midwesternudownersgrove"
replace `x' = 175263 if `y' == "minnesotastcollegesoutheasttechnicalcollege"
replace `x' = 179566 if `y' == "missouristuspringfield"
replace `x' = 180461 if `y' == "montanastubozeman"
replace `x' = 180522 if `y' == "montanastuhavre"
replace `x' = 180416 if `y' == "montanatechumontana"
replace `x' = -99 if `y' == "nationalflighttesti"
replace `x' = 167093 if `y' == "newenglandcollegeoptometrythe"
replace `x' = -99 if `y' == "newmexicostulascruces"
replace `x' = 193654 if `y' == "newschthe"
replace `x' = 194091 if `y' == "newyorkitech"
replace `x' = 200332 if `y' == "northdakotastufargomaincampus"
replace `x' = 204477 if `y' == "northeastohiomedu"
replace `x' = 147767 if `y' == "northwesternuevanston"
replace `x' = 101912 if `y' == "oakwoodu"
replace `x' = 204796 if `y' == "ohiostuthe"
replace `x' = 207388 if `y' == "oklahomastustillwater"
replace `x' = 154174 if `y' == "palmercollegechiropracticportorange"
replace `x' = 214616 if `y' == "pennsylvaniastuuparkhersheymedcenter"
replace `x' = 183080 if `y' == "plymouthstu"
replace `x' = 194824 if `y' == "polyinewyorku"
replace `x' = 456481 if `y' == "polyupuertorico"
replace `x' = 243081 if `y' == "ponceschmedhealthscience"
replace `x' = 194824 if `y' == "rensselaerpolyitroy"
replace `x' = 221351 if `y' == "rhodescollegememphis"
replace `x' = -99 if `y' == "richardstocktoncollegenewjerseythe"
replace `x' = 186283 if `y' == "riderulawrenceville"
replace `x' = 195049 if `y' == "rockefelleruthe"
replace `x' = 186371 if `y' == "rutgersstunewjersey"
replace `x' = 196079 if `y' == "sunybinghamtonu"
replace `x' = 196130 if `y' == "sunybuffalost"
replace `x' = 196033 if `y' == "sunycobleskill"
replace `x' = 196255 if `y' == "sunydownstmedcenter"
replace `x' = -99 if `y' == "sunyfarmingdalestcollege"
replace `x' = -99 if `y' == "sunyfashionitech"
replace `x' = 196167 if `y' == "sunygeneseo"
replace `x' = -99 if `y' == "sunymorrisvillestcollegeagtech"
replace `x' = -99 if `y' == "sunynewpaltz"
replace `x' = 196185 if `y' == "sunyoneonta"
replace `x' = 196194 if `y' == "sunyoswego"
replace `x' = -99 if `y' == "sunypolyi"
replace `x' = 196060 if `y' == "sunyualbany"
replace `x' = 196060 if `y' == "sunyualbanycollegenanoscalescienceengineering"
replace `x' = 196130 if `y' == "sunyubuffalo"
replace `x' = 410520 if `y' == "sagecollegesthesystemoffice"
replace `x' = 174792 if `y' == "stjohn'sucollegeville"
replace `x' = 161518 if `y' == "stjoseph'scollegemaine"
replace `x' = 122409 if `y' == "sandiegostusandiego"
replace `x' = -99 if `y' == "scrippsresearchithe"
replace `x' = 195474 if `y' == "sienacollegeny"
replace `x' = -99 if `y' == "skidawayioceanography"
replace `x' = 107983 if `y' == "southernarkansasu"
replace `x' = 160621 if `y' == "southernuamcollegeagresearchextensioncenter"
replace `x' = 160630 if `y' == "southernuamcollegeneworleans"
replace `x' = -99 if `y' == "stmary'susanantonio"
replace `x' = 196097 if `y' == "stonybrookustonybrook"
replace `x' = 152530 if `y' == "tayloruupland"
replace `x' = 216339 if `y' == "templeuphiladelphia"
replace `x' = 228723 if `y' == "texasamucollegestation"
replace `x' = 459736 if `y' == "tourouvallejo"
replace `x' = 130590 if `y' == "trinitycollegehartford"
replace `x' = 196866 if `y' == "uoncollegeschenectady"
replace `x' = 221759 if `y' == "utennesseesystemoffice"
replace `x' = 100663 if `y' == "ualabamabirminghamthe"
replace `x' = 100706 if `y' == "ualabamainhuntsvillethe"
replace `x' = 100751 if `y' == "ualabamathetuscaloosa"
replace `x' = 102553 if `y' == "ualaskaanchorageanchoragecampus"
replace `x' = 102614 if `y' == "ualaskafairbanksfairbanksfairbanksmaincampus"
replace `x' = 102632 if `y' == "ualaskasoutheastjuneau"
replace `x' = 104179 if `y' == "uarizonathe"
replace `x' = -99 if `y' == "ucaliforniaorniacooperativeextension"
replace `x' = 110398 if `y' == "ucaliforniaorniahastingsclaw"
replace `x' = 176965 if `y' == "ucenteralmissouri"
replace `x' = 144050 if `y' == "uchicagothe"
replace `x' = 224323 if `y' == "udallasirving"
replace `x' = 155317 if `y' == "ukansas"
replace `x' = -99 if `y' == "umarylandcenterforenvironmentalscience"
replace `x' = 220862 if `y' == "umemphisthe"
replace `x' = 176017 if `y' == "umississippithe"
replace `x' = 180489 if `y' == "umontanawesternthe"
replace `x' = 18048901 if `y' == "umontanathemissoula"
replace `x' = 181464 if `y' == "unebraskacenteraladministration"
replace `x' = 187985 if `y' == "unewmexicothe"
replace `x' = 159939 if `y' == "uneworleansthe"
replace `x' = 199111 if `y' == "unorthcarolinaashevillethe"
replace `x' = 199120 if `y' == "unorthcarolinachapelhillthe"
replace `x' = 199139 if `y' == "unorthcarolinacharlottethe"
replace `x' = 199148 if `y' == "unorthcarolinagreensborothe"
replace `x' = 199281 if `y' == "unorthcarolinapembrokethe"
replace `x' = 199218 if `y' == "unorthcarolinawilmingtonthe"
replace `x' = 482680 if `y' == "unorthgeorgia"
replace `x' = 227216 if `y' == "unorthtexasdenton"
replace `x' = 228909 if `y' == "unorthtexashealthsciencecenter"
replace `x' = 207500 if `y' == "uoklahomathe"
replace `x' = -99 if `y' == "upuertoricocenteraladministration"
replace `x' = 243179 if `y' == "upuertoricohumacao"
replace `x' = 243212 if `y' == "upuertoricoponce"
replace `x' = 243203 if `y' == "upuertoricomedsciencecampusschmed"
replace `x' = 219383 if `y' == "usiouxfallsthe"
replace `x' = 219471 if `y' == "usouthdakotathe"
replace `x' = 137351 if `y' == "usouthfloridatampa"
replace `x' = 176372 if `y' == "usouthernmississippithe"
replace `x' = 174914 if `y' == "ustthomasmn"
replace `x' = 137847 if `y' == "utampathe"
replace `x' = 229300 if `y' == "utexashealthsciencecenterhoustonthe"
replace `x' = -99 if `y' == "utexashealthsciencecentersanantoniothe"
replace `x' = 416801 if `y' == "utexasmdandersoncancercenterthe"
replace `x' = 228653 if `y' == "utexasmedbranchthe"
replace `x' = 228635 if `y' == "utexassouthwesternmedcenterthe"
replace `x' = 228769 if `y' == "utexasarlingtonthe"
replace `x' = 228778 if `y' == "utexasaustinthe"
replace `x' = 228787 if `y' == "utexasdallasthe"
replace `x' = 228796 if `y' == "utexaselpasothe"
replace `x' = 229018 if `y' == "utexaspermianbasinthe"
replace `x' = 229027 if `y' == "utexassanantoniothe"
replace `x' = 228802 if `y' == "utexastylerthe"
replace `x' = 227377 if `y' == "utexasbrownsville"
replace `x' = 227368 if `y' == "utexaspanamericanthe"
replace `x' = 206084 if `y' == "utoledothe"
replace `x' = 230764 if `y' == "uutahthe"
replace `x' = 210438 if `y' == "uwesternsts"
replace `x' = 123651 if `y' == "vanguardu"
replace `x' = 237932 if `y' == "westlibertyu"
replace `x' = 168254 if `y' == "westernnewenglandu"
replace `x' = 168281 if `y' == "wheatoncollegenorton"
replace `x' = 149781 if `y' == "wheatoncollegewheaton"
replace `x' = 216852 if `y' == "wideneru"
replace `x' = 187444 if `y' == "williampatersonu"
replace `x' = 168342 if `y' == "williamscollegewilliamstown"
replace `x' = -99 if `y' == "wisconsinstuersities"
replace `x' = 206604 if `y' == "wrightstudayton"
}
}
sum ipeds_handmatch if iped_match == 0
br ipeds_handmatch if iped_match == 0

** 6,966 handmatched using GRFP code, 27,164 remain to be matched
replace iped_match = 3 if ipeds_handmatch  > 0 & iped_match == .
replace iped_match = 4 if ipeds_handmatch == -99
lab var iped_match "1: match w first merge; 2: GRFP handmatch; 3: handmatch; 4: unable to id"
replace ipeds_handmatch = . if ipeds_handmatch == -99
gen double ipeds = ipeds_id
replace ipeds = ipeds_handmatch if ipeds_handmatch !=.
lab var ipeds "IPEDS id, final match"
	** IPEDS match rate: 97.24 (5 year panel)
sum ipeds
save "$dir/webcaspar_table2010_2013/NCSES list with IPEDS 10_14.dta", replace

********************************************************************************
*STEP 3: NCSES IPEDS TO NRC
********************************************************************************
********************************************************************************
** Step 3.A: Set up NCSES for NRC merge 
********************************************************************************
clear all
global dir "/Users/llanahan/Dropbox/Uni Funding Sources/Data"
use "$dir/webcaspar_table2010_2013/NCSES list with IPEDS 10_14.dta"
** create a cross walk with the NRC list of departments (NRC list departments.xls)
gen double nrc_dept = .
foreach x in nrc_dept {
foreach y in dept {
set more off
replace `x' = 1 if `y' == "Aerospace Engineering"
replace `x' = 2 if `y' == "Agricultural Sciences"
replace `x' = 43 if `y' == "Arts and Music"
replace `x' = 7 if `y' == "Astronomy"
replace `x' = 47 if `y' == "Atmospheric Sciences"
replace `x' = 9 if `y' == "Biological Sciences"
replace `x' = -99 if `y' == "Business and Management"
replace `x' = 12 if `y' == "Chemical Engineering"
replace `x' = 13 if `y' == "Chemistry"
replace `x' = 14 if `y' == "Civil Engineering"
replace `x' = 16 if `y' == "Communication and Librarianship"
replace `x' = 19 if `y' == "Computer Science"
replace `x' = 20 if `y' == "Earth Sciences"
replace `x' = 22 if `y' == "Economics"
replace `x' = -99 if `y' == "Education"
replace `x' = 23 if `y' == "Electrical Engineering"
replace `x' = 15 if `y' == "Humanities"
replace `x' = -99 if `y' == "Intersiciplinary or Other Sciences"
replace `x' = -99 if `y' == "Law"
replace `x' = 39 if `y' == "Materials Engineering"
replace `x' = 40 if `y' == "Mathematics and Statistics"
replace `x' = 41 if `y' == "Mechanical Engineering"
replace `x' = 9 if `y' == "Medical Sciences"
replace `x' = -99 if `y' == "Not Available"
replace `x' = 47 if `y' == "Oceanography"
replace `x' = 24 if `y' == "Other Engineering"
replace `x' = 20 if `y' == "Other Geosciences"
replace `x' = 9 if `y' == "Other Life Sciences"
replace `x' = -99 if `y' == "Other Non-sciences or Unknown Discipline"
replace `x' = -99 if `y' == "Other Physical Sciences"
replace `x' = 56 if `y' == "Other Social Sciences"
replace `x' = 51 if `y' == "Physics"
replace `x' = 54 if `y' == "Political Science and Public Administration"
replace `x' = 55 if `y' == "Psychology"
replace `x' = -99 if `y' == "Social Service Professions"
replace `x' = 59 if `y' == "Sociology"
}
}
save "$dir/webcaspar_table2010_2013/NCSES list with IPEDS & NRC dept 10_14.dta", replace

********************************************************************************
** Step 3.B: Clean up NCSES to merge with NRC
********************************************************************************
clear all
global dir "/Users/llanahan/Dropbox/Uni Funding Sources/Data"
use "$dir/webcaspar_table2010_2013/NCSES list with IPEDS & NRC dept 10_14.dta"
gen double ipeds_to_id = ipeds if ipeds > 0 & ipeds !=.
format ipeds_to_id %12.0f
** 3240 not have ipeds ID; 97344 have ipeds
replace ipeds_to_id = ipeds_to_id * 1000
describe nrc_dept
** 75438 have nrc_dept code
gen double nrc_dept_to_id = nrc_dept if nrc_dept > 0 & nrc_dept !=.
gen double ipeds_dept_id = ipeds_to_id + nrc_dept_to_id
format ipeds_dept_id %12.0f
gen double ipeds_dept_id_to_merge = ipeds_dept_id
format ipeds_dept_id_to_merge %12.0f
sort ipeds_dept_id_to_merge
save "$dir/webcaspar_table2010_2013/NCSES list with IPEDS & NRC dept clean 10_14.dta", replace

/*******************************************************************************
Note: this database is the original NCSES file. It now has the IPEDS ID & Unique Department ID.
There are 2 department IDs: NCSES listing and NRC (needed for the merge (next step).
In addition, this dataset includes a unique university/NRC department id (ipeds_dept_id_to_merge).
Use this variable to merge with NRC databases.
*******************************************************************************/

********************************************************************************
** Step 3.C: Clean up NRC to merge (note NEW DATASET)
********************************************************************************
clear all 
set more off
global dir "/Users/llanahan/Dropbox/Emerging Scholars/Data"
use "$dir/NRC 05 Survey/NRC list with IPEDS & dept clean.dta", replace
format ipeds_dept_id_to_merge %12.0f
br ipeds_dept_id_to_merge

*1* drop the 32 observations without ipeds affiliation
drop if ipeds_dept_id_to_merge ==.
* Leaving a total of 4927 departments from the NRC survey with unique IPEDS and department ID
drop _merge

format ProgramName ProgramWebsite nrc_dept %50s
gen double uni_public = 1 if Control == "Public"
recode uni_public (.=0)
lab var uni_public "Public/Private University (binary, 1 = public)"
rename RegionalCode RegionalCode
lab var RegionalCode "1 NE, 2 MW, 3 SA, 4 SC, 5 W"
rename ProgramSizeQuartile ProgramSize_Q
lab var ProgramSize_Q "Program Size Quartile Ranking"
foreach x in ProgramSize_Q {
replace `x' = "." if `x' == "N/D"
} 
rename RRankings5thPerce R_rank_5th_pct
lab var R_rank_5th_pct "R Ranking, 5th percent"
rename RRankings95thPercen R_rank_95th_pct
lab var R_rank_95th_pct "R Ranking, 95th percent"
rename SRankings5thPercent S_rank_5th_pct
lab var S_rank_5th_pct "S Ranking, 5th percent"
rename SRankings95thPercenti S_rank_95th_pct
lab var S_rank_95th_pct "S Ranking, 95th percent"
rename ResearchActivity5thPercenti res_act_5th_pct
lab var res_act_5th_pct "Research Activity, 5th percent"
rename ResearchActivity95thPercent res_act_95th_pct
lab var res_act_5th_pct "Research Activity, 95th percent"
rename StudentSupportOutcomes student_support_5th_pct
lab var student_support_5th_pct "Student Support & Outcomes, 5th percent"
rename Q student_support_95th_pct
lab var student_support_95th_pct "Student Support & Outcomes, 95th percent"
rename Diversity5thPerc diversity_5th_pct
lab var diversity_5th_pct "Diversity, 5th percent"
rename Diversity95thPerc diversity_95th_pct
lab var diversity_95th_pct "Diversity, 95th percent"

foreach x in R_rank_5th_pct R_rank_95th_pct S_rank_5th_pct S_rank_95th_pct res_act_5th_pct res_act_95th_pct student_support_5th_pct student_support_95th_pct diversity_95th_pct diversity_5th_pct {
replace `x' = "." if `x' == "Not Ranked"
} 
rename AverageNumberofPublications avg_pubs_per_fac 
rename AverageCitationsperPublicatio avg_citations_per_pub
rename PercentofFacultywithGrants pct_fac_w_grants_06
rename AwardsperAllocatedFacultyMe fac_awards_06
rename PercentofFirstYearStudentsw pct_1styr_stu_full_support_05
lab var pct_1styr_stu_full_support_05 "Percent of First Year Students with Full Financial Support, Fall 2005"
rename AvgCompletionPercentage8Y avg_degree_completion
lab var avg_degree_completion "Avg. Completion Percentage: 8 Years or Less for Humanities; 6 Years or Less for Other Fields"
rename MedianTimetoDegreeFulland median_time_to_degree_06
lab var median_time_to_degree_06 "Median Time to Degree (Full- and Part-Time Graduates), 2006"
rename PercentwithAcademicPlans pct_w_academic_plans
rename CollectsDataAboutPostGraduat dept_collects_data_postgrad
rename NonAsianMinorityFacultyasa nonasian_minority_fac_pct
lab var nonasian_minority_fac_pct "Non-Asian Minority Faculty as a Percent of Total Core and New Domestic Faculty, 2006"
rename FemaleFacultyasaPercentofT female_fac_pct
lab var female_fac_pct "Female Faculty as a Percent of Total Core and New Faculty, 2006"
rename NonAsianMinorityStudentsasa nonasian_minority_stu_pct
rename FemaleStudentsasaPercentof female_stu_pct
lab var female_stu_pct "Female Students as a Percent of Total Students, Fall 2005"
rename InternationalStudentsasaPer intl_stu_pct
lab var intl_stu_pct " International Students as a Percent of Total Students, Fall 2005"
rename AverageNumberofPhDsGradua avg_phd_grad
rename PercentofInterdisciplinaryFac pct_interdis_fac
rename AverageGREScores20042006 avg_GRE
lab var avg_GRE "Average GRE Scores, 2004-2006"
rename AK pct_1styr_w_ex_fellowship
lab var pct_1styr_w_ex_fellowship "Percent of First-Year Students with External Fellowships, 2005"
rename IsStudentWorkSpaceProvided stu_workspace
lab var stu_workspace "Is Student Work Space Provided? (1=Yes; 0=No)"
rename IsHealthInsuranceProvided  stu_hlt_ins_offer
rename NumberofStudentActivitiesMa num_stu_activity
lab var num_stu_activity "Number of Student Activities (Max=18)" 
rename TotalFaculty2006 tot_fac_06
rename NumberofAllocatedFaculty200  num_allocated_fac_06
rename AssistantProfessorsasaPercen pct_assist_fac
rename TenuredFacultyasaPercentof pct_tenured_fac
rename NumberofCoreandNewFaculty num_core_new_fac
rename NumberofStudentsEnrolledF num_stu_enrolled_05
lab var num_stu_enrolled_05 " Number of Students Enrolled, Fall 2005"
rename AverageAnnualFirstYearEnroll avg_annual_1styr_enroll
lab var avg_annual_1styr_enroll "Average Annual First Year Enrollment, 2002-2006"
rename PercentofStudentswithResearc pct_stu_res_fellow_05
rename PercentofStudentswithTeachin pct_stu_teach_fellow_05
rename AX pct_1styr_inst_fellow_alone
rename PercentofFirstYearStudents pct_1styr_combo
lab var pct_1styr_combo "Percent  of First Year Students with a Combination of Fellowships and Traineeships"
rename AZ pct_1styr_both
lab var pct_1styr_both "Percent of First Year Students with Both Internal Fellowships and Internal Assistantships"
rename BA pct_1styr_many_fellow
lab var pct_1styr_many_fellow "Percent of First Year Students with Multiple Internal Assistantships"
rename OrientationforNewGraduateStu stu_orientation
rename InternationalStudentOrientatio stu_intl_orientation
rename LanguageScreeningSupportPri stu_language_support
rename InstructioninWriting stu_writing_support
rename InstructioninStatistics stu_statistics_support
rename PrizesAwardsforTeachingor stu_prize_teach_res
rename AssistanceTraininginProposa stu_proposal_support
rename OncampusGraduateResearchConf stu_oncampus_grad_res_conf
rename FormalTraininginAcademicInte stu_academic_integrity_training
rename ActiveGraduateStudentAssociat stu_grad_assoc
rename StaffAssignedtotheGraduateS stu_staff_grad_assoc
rename FinancialSupportfortheGradua stu_finance_support_grad_assoc
rename PostedAcademicGrievanceProced stu_academic_greivance
rename DisputeResolutionProcedure stu_dispute_resolution
rename RegularGraduateProgramsDirect stu_grad_prog_meeting
rename AnnualReviewofAllEnrolledDo stu_annual_review_enrolled
rename OrganizedTrainingtoHelpStude stu_teacher_training
rename TravelSupporttoAttendProfess stu_travel_support
rename CountofStudentActivities stu_activities_count

foreach x in stu_travel_support stu_activities_count stu_teacher_training stu_annual_review_enrolled stu_grad_prog_meeting stu_dispute_resolution stu_academic_greivance stu_finance_support_grad_assoc stu_staff_grad_assoc stu_grad_assoc stu_academic_integrity_training stu_oncampus_grad_res_conf stu_proposal_support stu_orientation stu_intl_orientation stu_language_support stu_writing_support stu_statistics_support stu_prize_teach_res {
lab var `x' "Student Activities: 1 institution offer; 2 program offer; 3 both; 4 neither"
}

global var avg_citations_per_pub pct_fac_w_grants_06 pct_1styr_stu_full_support_05 avg_degree_completion median_time_to_degree_06 pct_w_academic_plans dept_collects_data_postgrad nonasian_minority_fac_pct female_fac_pct nonasian_minority_stu_pct female_stu_pct intl_stu_pct pct_interdis_fac avg_GRE pct_1styr_w_ex_fellowship stu_hlt_ins_offer num_stu_activity tot_fac_06 pct_assist_fac pct_tenured_fac num_core_new_fac num_stu_enrolled_05 avg_annual_1styr_enroll pct_stu_res_fellow_05 pct_stu_teach_fellow_05 pct_1styr_inst_fellow_alone pct_1styr_combo pct_1styr_both pct_1styr_many_fellow stu_orientation stu_intl_orientation stu_language_support stu_writing_support stu_statistics_support stu_prize_teach_res stu_proposal_support stu_oncampus_grad_res_conf stu_academic_integrity_training stu_grad_assoc stu_staff_grad_assoc stu_finance_support_grad_assoc stu_academic_greivance stu_dispute_resolution stu_grad_prog_meeting stu_annual_review_enrolled stu_teacher_training stu_travel_support stu_activities_count
foreach x in $var {
set more off
replace `x' = "." if `x' == "N/D"
}
foreach x in $var {
set more off
replace `x' = "." if `x' == "Not Ranked"
}
foreach x in $var {
set more off
replace `x' = "." if `x' == "N/R"
}
foreach x in pct_1styr_inst_fellow_alone pct_1styr_combo pct_1styr_both pct_1styr_many_fellow {
gen `x'_trim = trim(itrim(`x'))
replace `x' = "." if `x' == "*"
}
set more off 
destring, replace

recode dept_collects_data_postgrad (-1=0) 
recode stu_workspace (-1=0)
recode stu_hlt_ins_offer (-1=0)

drop nrc_uni_cleaned nrc_uni_clns nrc_group_clean nrc_group_clns nrc_group_original nrc_clns_counter uni_clns_to_match ipeds_uni_clean_full ipeds_uni_clean1 ipeds_uni_clean11 ipeds_uni_clean12 ipeds_uni_clean13 ipeds_uni_cleaned ipeds_uni_clns ipeds_handmatch nrc_dept_tally pct_1styr_inst_fellow_alone_trim pct_1styr_combo_trim pct_1styr_both_trim pct_1styr_many_fellow_trim

** Need to deal with DUPS of IPEDS_DEPT
/* note that NRC has university department dups that need to be accounted for. 
specifically, some of the universities have programs with the same university/department
classification. To account for this duplication, Nicole wrote a code to aggregate and 
average data points where necessary. This code has been pulled from the emerging scholars
database.
*/
quietly bys ipeds_dept_id_to_merge nrc_broad_dept nrc_dept_full nrc_uni Control RegionalCode: gen dup1 = cond(_N==1, 0, _n)
quietly bys ipeds_dept_id_to_merge: gen dup2 = cond(_N==1,0,_n)
sum dup1 if dup1>1
sum dup2 if dup2>1 
count if dup1!=dup2
	drop dup2
	*The first duplicate (dup1) is for institutions that are identical on Ipeds-Dept ID, NRC Broad, NRC Full, and NRS unversity name*
	*The second duplicate (dup2) is for institutions that are identical on Ipeds-Dept ID only*
	*Will deal with duplicates by looking at dup1 THEN dup2*
	
/*First Step: Get rid of duplicates across all 4 categories by taking the AVERAGE of some numeric variables
and the SUM of some other numeric variables*/

	*Average of Some Variables*
	global meanvars ProgramSize_Q R_rank_5th_pct R_rank_95th_pct S_rank_5th_pct S_rank_95th_pct res_act_5th_pct res_act_95th_pct student_support_5th_pct student_support_95th_pct diversity_5th_pct diversity_95th_pct avg_pubs_per_fac avg_citations_per_pub pct_fac_w_grants_06 pct_1styr_stu_full_support_05 avg_degree_completion median_time_to_degree_06 pct_w_academic_plans dept_collects_data_postgrad nonasian_minority_fac_pct female_fac_pct nonasian_minority_stu_pct female_stu_pct intl_stu_pct avg_phd_grad pct_interdis_fac avg_GRE pct_1styr_w_ex_fellowship stu_workspace stu_hlt_ins_offer num_stu_activity pct_assist_fac pct_tenured_fac avg_annual_1styr_enroll pct_stu_res_fellow_05 pct_stu_teach_fellow_05 pct_1styr_inst_fellow_alone pct_1styr_combo pct_1styr_both pct_1styr_many_fellow stu_orientation stu_intl_orientation stu_language_support stu_writing_support stu_statistics_support stu_prize_teach_res stu_proposal_support stu_oncampus_grad_res_conf stu_academic_integrity_training stu_grad_assoc stu_staff_grad_assoc stu_finance_support_grad_assoc stu_academic_greivance stu_dispute_resolution stu_grad_prog_meeting stu_annual_review_enrolled stu_teacher_training stu_travel_support stu_activities_count nrc_clns_tally groupid ipeds_year ipeds_id ipeds1 ipeds2 ipeds ipeds_to_id nrc_dept_id nrc_ipeds_dept_id uni_public
	foreach x in $meanvars {
		by ipeds_dept_id_to_merge nrc_broad_dept nrc_dept_full nrc_uni Control RegionalCode: egen double a`x' = mean(`x')
		replace `x'=a`x' 
	}
	*Sum of Some Variables*	
	global sumvars fac_awards_06 tot_fac_06 num_allocated_fac_06 num_core_new_fac num_stu_enrolled_05 
	foreach x in $sumvars {
		by ipeds_dept_id_to_merge nrc_broad_dept nrc_dept_full nrc_uni Control RegionalCode: egen double s`x' = sum(`x')
		replace `x'=s`x'
	}
quietly bys ipeds_dept_id_to_merge nrc_broad_dept nrc_dept_full nrc_uni Control RegionalCode $meanvars $sumvars: gen dup3 = cond(_N==1, 0, _n)
	sum dup1 dup3
		*Stats are the same, so loop ran, and now can drop duplicates*
	drop dup3
	drop if dup1>1
	foreach x in $meanvars {
		drop a`x'  
	}
	foreach x in $sumvars {
		drop s`x'
	}
/*Second Step: Get rid of duplicates across just Ipeds-Dept ID*/
quietly bys ipeds_dept_id_to_merge: gen dup2 = cond(_N==1,0,_n)
sum dup2 if dup2>1
	*Two school-programs in NJ have duplicates, so run the same code as before and drop 2nd observation* 

	*Average of Some Variables*
	global meanvars ProgramSize_Q R_rank_5th_pct R_rank_95th_pct S_rank_5th_pct S_rank_95th_pct res_act_5th_pct res_act_95th_pct student_support_5th_pct student_support_95th_pct diversity_5th_pct diversity_95th_pct avg_pubs_per_fac avg_citations_per_pub pct_fac_w_grants_06 pct_1styr_stu_full_support_05 avg_degree_completion median_time_to_degree_06 pct_w_academic_plans dept_collects_data_postgrad nonasian_minority_fac_pct female_fac_pct nonasian_minority_stu_pct female_stu_pct intl_stu_pct avg_phd_grad pct_interdis_fac avg_GRE pct_1styr_w_ex_fellowship stu_workspace stu_hlt_ins_offer num_stu_activity pct_assist_fac pct_tenured_fac avg_annual_1styr_enroll pct_stu_res_fellow_05 pct_stu_teach_fellow_05 pct_1styr_inst_fellow_alone pct_1styr_combo pct_1styr_both pct_1styr_many_fellow stu_orientation stu_intl_orientation stu_language_support stu_writing_support stu_statistics_support stu_prize_teach_res stu_proposal_support stu_oncampus_grad_res_conf stu_academic_integrity_training stu_grad_assoc stu_staff_grad_assoc stu_finance_support_grad_assoc stu_academic_greivance stu_dispute_resolution stu_grad_prog_meeting stu_annual_review_enrolled stu_teacher_training stu_travel_support stu_activities_count nrc_clns_tally groupid ipeds_year ipeds_id ipeds1 ipeds2 ipeds ipeds_to_id nrc_dept_id nrc_ipeds_dept_id uni_public
	foreach x in $meanvars {
		by ipeds_dept_id_to_merge: egen double a`x' = mean(`x') if dup2>0
		replace `x'=a`x' if dup2>0
	}
	*Sum of Some Variables*	
	global sumvars fac_awards_06 tot_fac_06 num_allocated_fac_06 num_core_new_fac num_stu_enrolled_05 
	foreach x in $sumvars {
		by ipeds_dept_id_to_merge: egen double s`x' = sum(`x') if dup2>0
		replace `x'=s`x' if dup2>0
	}
quietly bys ipeds_dept_id_to_merge $meanvars $sumvars: gen dup4 = cond(_N==1, 0, _n)
	sum dup2 dup4
		*Stats are the same, so loop ran, and now can drop duplicates*
	drop dup4 
	drop if dup2>1
	foreach x in $meanvars {
		drop a`x'  
	}
	foreach x in $sumvars {
		drop s`x'
	}
duplicates report ipeds_dept_id_to_merge
duplicates report ipeds_dept_id_to_merge nrc_broad_dept nrc_dept_full nrc_uni Control RegionalCode
	*Duplicates are taken care of now*
sort ipeds_dept_id_to_merge
drop nrc_dept
sum ipeds_dept_id_to_merge
save "/Users/llanahan/Dropbox/Uni Funding Sources/Data/webcaspar_table2010_2013/NRC dept data 10_14 to merge.dta", replace

********************************************************************************
** Step 3.D: Merge NCSES with NRC 
** important to note that NRC is cross-section and NCSES is panel
********************************************************************************
clear all
global dir "/Users/llanahan/Dropbox/Uni Funding Sources/Data"
use "$dir/webcaspar_table2010_2013/NCSES list with IPEDS & NRC dept clean 10_14.dta"
sum ipeds_dept_id_to_merge
br ipeds_dept_id_to_merge
drop _merge
merge m:m ipeds_dept_id_to_merge using "/Users/llanahan/Dropbox/Uni Funding Sources/Data/webcaspar_table2010_2013/NRC dept data 10_14 to merge.dta"
* Matched 11,594; not matched 111,094 (5 year panel)
drop if _merge == 2
drop _merge dup1 dup2
sort ipeds
save "/Users/llanahan/Dropbox/Uni Funding Sources/Data/webcaspar_table2010_2013/NCSES IPEDS NRC data 10_14.dta", replace

********************************************************************************
*STEP 4: DELTA COST CLEAN
********************************************************************************
********************************************************************************
** Step 4.A: CLEAN UP IPEDS DATA: PULLED FROM DELTA COST **
********************************************************************************
clear all
set memory 500M
set more off
set trace off
global dir "/Users/llanahan/Dropbox/Uni Funding Sources/Data"
*
* delta_87_10_variable_labels
*
* National Center for Education Statistics
* IPEDS Analytics: Delta Cost Project Database
*
* ----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|
* STEP 1. Merge separate Delta datasets 
* ----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|
use "/Users/llanahan/Dropbox/Emerging Scholars/Baccalaureate/Delta_Cost_IPEDS/Raw_data/delta_public_00_10.dta"
append using "/Users/llanahan/Dropbox/Emerging Scholars/Baccalaureate/Delta_Cost_IPEDS/Raw_data/delta_public_87_99.dta"
sort unitid academicyear
compress
* ----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|
* STEP 2. Define Delta formats
* ----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|
label define isgrouped ///
0 "Not grouped" ///
1 "Grouped"
label define ansi_code ///
1 "Alabama" ///
2 "Alaska" ///
4 "Arizona" ///
5 "Arkansas" ///
6 "California" ///
8 "Colorado" ///
9 "Connecticut" ///
10 "Delaware" ///
11 "District of Columbia" ///
12 "Florida" ///
13 "Georgia" ///
15 "Hawaii" ///
16 "Idaho" ///
17 "Illinois" ///
18 "Indiana" ///
19 "Iowa" ///
20 "Kansas" ///
21 "Kentucky" ///
22 "Louisiana" ///
23 "Maine" ///
24 "Maryland" ///
25 "Massachusetts" ///
26 "Michigan" ///
27 "Minnesota" ///
28 "Mississippi" ///
29 "Missouri" ///
30 "Montana" ///
31 "Nebraska" ///
32 "Nevada" ///
33 "New Hampshire" ///
34 "New Jersey" ///
35 "New Mexico" ///
36 "New York" ///
37 "North Carolina" ///
38 "North Dakota" ///
39 "Ohio" ///
40 "Oklahoma" ///
41 "Oregon" ///
42 "Pennsylvania" ///
44 "Rhode Island" ///
45 "South Carolina" ///
46 "South Dakota" ///
47 "Tennessee" ///
48 "Texas" ///
49 "Utah" ///
50 "Vermont" ///
51 "Virginia" ///
53 "Washington" ///
54 "West Virginia" ///
55 "Wisconsin" ///
56 "Wyoming" ///
60 "American Samoa" ///
64 "Federated States of Micronesia" ///
66 "Guam" ///
68 "Marshall Islands" ///
69 "Northern Mariana Islands" ///
70 "Palau" ///
72 "Puerto Rico" ///
74 "U.S. Minor Outlying Islands" ///
78 "Virgin Islands of the U.S." ///
label define sector ///
0 "Administrative Unit" ///
1 "Public 4-year or above " ///
2 "Private nonprofit 4-year or above " ///
3 "Private for-profit 4-year or above " ///
4 "Public 2-Year " ///
5 "Private nonprofit 2-year " ///
6 "Private for-profit 2-year " ///
7 "Public less-than-2-year " ///
8 "Private nonprofit less-than-2-year " ///
9 "Private for-profit less-than-2-year " ///
99 "Sector unknown"
label define iclevel ///
1 "4-year or higher (4 year)" ///
2 "2-but-less-than 4-year (2 year)" ///
3 "less than 2-year" ///
-3 "not available"
label define control ///
1 "Public institution" ///
2 "Private nonprofit institution" ///
3 "Private for-profit institution" ///
-3 "not available"
label define oberegion ///
0 "US Service schools"  ///
1 "New England (CT ME MA NH RI VT)" ///
2 "Mid East (DE DC MD NJ NY PA)" ///
3 "Great Lakes (IL IN MI OH WI)" ///
4 "Plains (IA KS MN MO NE ND SD)" ///
5 "Southeast (AL AR FL GA KY LA MS NC SC TN VA WV)" ///
6 "Southwest (AZ NM OK TX)" ///
7 "Rocky Mountains (CO ID MT UT WY)" ///
8 "Far West (AK CA HI NV OR WA)" ///
9 "Outlying areas (AS FM GU MH MP PR PW VI)"
label define census_division ///
1 "New England (CT, ME, MA, NH, RI, VT) " ///
2 "Middle Atlantic (NJ, NY, PA) " ///
3 "East North Central (IN, IL, MI, OH, WI) " ///
4 "West North Central (IA, KS, MN, MO, NE, ND, SD) " ///
5 "South Atlantic (DE, DC, FL, GA, MD, NC, SC, VA, WV) " ///
6 "East South Central (AL, KY, MS, TN) " ///
7 "West South Central (AR, LA, OK, TX) " ///
8 "Mountain (AZ, CO, ID, NM, MT, UT, NV, WY) " ///
9 "Pacific (AK, CA, HI, OR, WA) " ///
10 "Islands "
label define census_region ///
1 "Northeast (CT, ME, MA, NH, RI, VT, NJ, NY, PA)" ///
2 "Midwest (IN, IL, MI, OH, WI, IA, KS, MN, MO, NE, ND, SD)" ///
3 "South (DE, DC, FL, GA, MD, NC, SC, VA, WV, AL, KY, MS, TN, AR, LA, OK, TX)" ///
4 "West (AZ, CO, ID, NM, MT, UT, NV, WY, AK, CA, HI, OR, WA)" ///
5 "Islands"
label define region_compact ///
1 "SREB (Southern Regional Education Board)" ///
2 "WICHE (Western Interstate Commission for Higher Education)" ///  
3 "MHEC (Midwestern Higher Education Compact)" ///
4 "NEBHE (New England Board of Higher Education)" ///
0 "No affiliation (DC, NJ , NY , PA, Islands)"
label define carnegie2k ///
15 "Doctoral/Research Universities-Extensive" ///
16 "Doctoral/Research Universities-Intensive" ///
21 "Masterís Colleges and Universities I" ///
22 "Masterís (Comprehensive) Colleges and Universities II" ///
31 "Baccalaureate Colleges-Liberal Arts" ///
32 "Baccalaureate Colleges-General" ///
33 "Baccalaureate/Associateís Colleges" ///
40 "Associateís Colleges" ///
51 "Theological seminaries and other specialized faith-related institutions" ///
52 "Medical schools and medical centers" ///
53 "Other separate health profession schools" ///
54 "Schools of engineering and technology" ///
55 "Schools of business and management" ///
56 "Schools of art, music, and design" ///
57 "Schools of law" ///
58 "Teachers colleges" ///
59 "Other specialized institutions" ///
60 "Tribal Colleges and Universities" ///
-3 "Not available" 
label define carnegiegrp  ///
1 "Research/Doctoral Institutions"  ///
2 "Masterís Institutions"  ///
3 "Bachelors Institutions"  ///
4 "Associates Institutions"  ///
5 "Specialized Institutions"  ///
6 "Tribal Institutions"
label define carnegie_sector  ///
1 "Public Research" ///
2 "Public Masters"  ///
3 "Public Associates"  ///
4 "Private Nonprofit Research"  ///
5 "Private Nonprofit Masters"  ///
6 "Private Nonprofit Bachelors"  ///
7 "Public Bachelors"  ///
8 "Private Nonprofit Associates"  ///
9 "Public Specialty" ///
10 "Private Nonprofit Specialty"
label define carnegie2kfive  ///
1  "Associates--Public Rural-serving Small" ///
2  "Associates--Public Rural-serving Medium" ///
3  "Associates--Public Rural-serving Large" ///
4  "Associates--Public Suburban-serving Single Campus" ///
5  "Associates--Public Suburban-serving Multicampus" ///
6  "Associates--Public Urban-serving Single Campus" ///
7  "Associates--Public Urban-serving Multicampus" ///
8  "Associates--Public Special Use" ///
9  "Associates--Private Not-for-profit" ///
10  "Associates--Private For-profit" ///
11  "Associates--Public 2-year colleges under 4-year universities" ///
12  "Associates--Public 4-year Primarily Associates" ///
13  "Associates--Private Not-for-profit 4-year Primarily Associates" ///
14  "Associates--Private For-profit 4-year Primarily Associates" ///
15  "Research Universities (very high research activity)" ///
16  "Research Universities (high research activity)" ///
17  "Doctoral/Research Universities: Doctorate-granting Universities" ///
18  "Masters Colleges and Universities (larger programs)" ///
19  "Masters Colleges and Universities (medium programs)" ///
20  "Masters Colleges and Universities (smaller programs)" ///
21  "Baccalaureate Colleges--Arts & Sciences" ///
22  "Baccalaureate Colleges--Diverse Fields" ///
23  "Baccalaureate/Associates Colleges" ///
24  "Special Focus Institutions--faith-related institutions" ///
25  "Special Focus Institutions--medical" ///
26  "Special Focus Institutions--Other health professions schools" ///
27  "Special Focus Institutions--Schools of engineering" ///
28  "Special Focus Institutions--Other technology-related schools" ///
29  "Special Focus Institutions--Schools of business and management" ///
30  "Special Focus Institutions--Schools of art, music, and design" ///
31  "Special Focus Institutions--Schools of law" ///
32  "Special Focus Institutions--Other special-focus institutions" ///
33  "Tribal Colleges" ///
-3  "Not available"
label define  flagship  ///
1 "Flagship institution" ///
0 "Not flagship institution" 
label define  landgrnt ///
1 "Land grant institution" ///
2 "Not land grant institution" 
label define   hbcu ///
1 "Historically Black College or University" ///
2 "Not Historically Black College or University"
label define  his  ///
1 "Hispanic Serving Institution" ///
0 "Not Hispanic Serving Institution"
label define  medical ///
2 "Institution does not grant a medical degree" ///
1 "Institution grants a medical degree" ///
-1 "not reported" ///
-2 "not available/applicable" 
label define  hospital  ///
1 "institution has a hospital" ///
2 "institution does not have a hospital" ///
0 "institution implied it does not have a hospital" ///
-1 "not reported"
label define has_instruction ///
0 "institution did not report instructional expenditures" ///
1 "institution reported instructional expenditures" 
label define  has_fte ///
0 "institution did not report FTE enrollment" ///
1 "institution reported FTE enrollment" 
label define  has_completions  ///
0 "institution did not report student completions" ///
1 "institution reported student completions"
label define  has_all  ///
0 "institution did not report all three variables" ///
3 "institution did report all three variables"
label define  matched_set ///
0 "not in panel" ///
1 "included in panel"
label define  instacttype  ///
1 "contact hours" ///
2 "credit hours" /// 
3 "both contact and  credit hours" ///
-2 "not applicable" 
label define imputation_flag  ///
1 "data were imputed" ///
0 "data were reported" ///
-1 "data were not reported and could not be imputed" ///
-2 "data not eligible for imputing (lag or lead year data not available)" 
* ----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|
* STEP 3. Assign variable labels to variables and save dataset
* ----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|
label values isgrouped isgrouped
label values ansi_code ansi_code
label values sector sector
label values sector_revised sector
label values iclevel iclevel
label values control control
label values oberegion oberegion
label values census_division census_division
label values census_region census_region
label values region_compact region_compact
label values carnegie2000 carnegie2k
label values carnegiegrp_2000 carnegiegrp
label values carnegie_sector_2000 carnegie_sector
label values carnegie2005 carnegie2kfive
label values carnegiegrp_2005 carnegiegrp
label values carnegie_sector_2005 carnegie_sector
label values flagship flagship
label values landgrnt landgrnt
label values hbcu hbcu
label values hsi hsi
label values medical medical
label values hospital hospital
label values has_instruction has_instruction
label values has_fte has_fte
label values has_completions has_completions
label values has_all has_all
label values matched_n_87_10_24 matched_set
label values matched_n_00_10_11 matched_set
label values matched_n_05_10_6 matched_set
label values instacttype instacttype
label values I* imputation_flag
save "/Users/llanahan/Dropbox/Uni Funding Sources/Data/webcaspar_table2010_2013/delta_87_10.dta", replace
* note that this is the complete listing of IPEDS data from 1987 - 2010 (202800 observations)

********************************************************************************
/** Step 4.B: Format the delta cost data to then merge with NCSES data **
1. Keep only 2010 year
2. clean up merge variable: unitid to ipeds */
********************************************************************************
clear all
set more off
global dir "/Users/llanahan/Dropbox/Uni Funding Sources/Data"
use "$dir/webcaspar_table2010_2013/delta_87_10.dta"
keep if academicyear == 2010
gen double ipeds = unitid
*lab var ipeds "IPEDS id to merge on"
sort ipeds
keep groupid academicyear unitid isgrouped instname city state zip ansi_code sector sector_revised iclevel control oberegion census_division census_region region_compact carnegie2000 carnegiegrp_2000 carnegie_sector_2000 carnegie2005 carnegiegrp_2005 carnegie_sector_2005 flagship landgrnt hbcu hsi medical hospital cpi_index cpi_scalar_2010 hepi_index hepi_scalar_2010 heca_index heca_scalar_2010 has_instruction has_fte fte_count tuition01 tuition02 tuition03 nettuition01 federal03 state03 local03 state_local_app federal07 state06 local06 state_local_grant_contract unrestricted_revenue totaldegrees_100fte totalawards totalcompletions_100fte doc_deg_share_of_tot_deg assoc_deg_share_of_tot_deg bach_deg_share_of_tot_deg grad_deg_share_of_tot_deg prof_deg_share_of_tot_deg total_enrollment_amin_tot total_enrollment_asian_tot total_enrollment_black_tot total_enrollment_hisp_tot total_enrollment_white_tot total_faculty_all ipeds
drop has_instruction assoc_deg_share_of_tot_deg prof_deg_share_of_tot_deg
foreach x in groupid academicyear unitid isgrouped instname city state zip ansi_code sector sector_revised iclevel control oberegion census_division census_region region_compact carnegie2000 carnegiegrp_2000 carnegie_sector_2000 carnegie2005 carnegiegrp_2005 carnegie_sector_2005 flagship landgrnt hbcu hsi medical hospital cpi_index cpi_scalar_2010 hepi_index hepi_scalar_2010 heca_index heca_scalar_2010 has_fte fte_count tuition01 tuition02 tuition03 nettuition01 federal03 state03 local03 state_local_app federal07 state06 local06 state_local_grant_contract unrestricted_revenue totaldegrees_100fte totalawards totalcompletions_100fte bach_deg_share_of_tot_deg grad_deg_share_of_tot_deg doc_deg_share_of_tot_deg total_enrollment_amin_tot total_enrollment_asian_tot total_enrollment_black_tot total_enrollment_hisp_tot total_enrollment_white_tot total_faculty_all {
rename `x' `x'_DC
}
save "/Users/llanahan/Dropbox/Uni Funding Sources/Data/webcaspar_table2010_2013/delta_87_10 to merge.dta", replace

********************************************************************************
*STEP 5: NCSES IPEDS NRC MERGE TO DELTA COST
********************************************************************************
********************************************************************************
** Step 5.A: Merge NCSES (with NRC & IPEDS) database with Delta Cost (only merging 2010 year)
********************************************************************************
clear all
global dir "/Users/llanahan/Dropbox/Uni Funding Sources/Data"
use "$dir/webcaspar_table2010_2013/NCSES IPEDS NRC data 10_14.dta"
sort ipeds

merge m:m ipeds using "/Users/llanahan/Dropbox/Uni Funding Sources/Data/webcaspar_table2010_2013/delta_87_10 to merge.dta"
drop if _merge == 2
gen not_merge_to_DC = 1 if _merge == 1
recode not_merge_to_DC (.=0)
drop _merge
** Merged: 105,929; Not merge: 13,375 (not sync with Delta Cost database)
	** note: _merge == 1 pertain to the universities I was unable to identfy an IPEDS number
save "$dir/webcaspar_table2010_2013/NCSES IPEDS NRC Delta Cost data 10_14.dta", replace

********************************************************************************
* ----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|
* FULL DATASET for 5 year panel: NCSES (119304); merged with IPEDS (116012); merged with Delta Cost (105929); merged with NRC (11,594)
* ----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|
********************************************************************************

********************************************************************************
** Step 5.B: clean up the dataset in preparation for analysis
*********************										********************
* Step 5.B.1: clean up IPEDS (i.e. Carnegie codes); this will be useful for defining the population of universities
********************************************************************************
clear all
global dir "/Users/llanahan/Dropbox/Uni Funding Sources/Data"
use "$dir/webcaspar_table2010_2013/NCSES IPEDS NRC Delta Cost data 10_14.dta"
set more off

** Research University group: as defined by the 2005 Carnegie Classifications (from Delta Cost)
tab carnegie2005, gen(c05_DC_)
**1** Define traditional doctoral research institution population (only)**** 
/*	Doctoral/Research Universities : Doctoral-granting Universities
	Research Universities (high research activity) 
	Research Universities (very high research activity) 
	*/
gen doctoral_uni_c05_DC = 1 if c05_DC_7==1|c05_DC_5==1|c05_DC_6==1
	*code from 4 year panel, the list slightly adjusted with the additional panel* gen doctoral_uni_c05_DC = 1 if c05_DC_4==1|c05_DC_5==1|c05_DC_6==1
recode doctoral_uni_c05_DC (.=0)
**2** Define research institution population (more broad) **** 
/*	Doctoral/Research Universities : Doctoral-granting Universities
	Research Universities (high research activity) 
	Research Universities (very high research activity)
	Special Focus Institutions--medical
	Special Focus Institutions--Schools of engineering
	*/
gen research_uni_c05_DC = 1 if c05_DC_7==1|c05_DC_5==1|c05_DC_6==1|c05_DC_15==1|c05_DC_17==1 
	*code from 4 year panel*gen research_uni_c05_DC = 1 if c05_DC_4==1|c05_DC_5==1|c05_DC_6==1|c05_DC_14==1|c05_DC_16==1 
recode research_uni_c05_DC (.=0)

/** Note that there are some universities that have not been classified in Delta Cost.
The research universities listed below (beginning line 71) are not classified in Delta Cost.
To include them in the analysis, I have added a variable to inlcude them in the population
of research institutions.
consult: http://nces.ed.gov/globallocator/ to confirm. The code below adds 3492 observations to "doctoral" and "research" groups
*/

gen research_institutions_missing = 0
foreach x in research_institutions_missing {
foreach y in university {
set more off
replace `x' = 1 if `y' == "University of Illinois at Urbana-Champaign"
replace `x' = 1 if `y' == "University of Illinois at Springfield"
replace `x' = 1 if `y' == "University of AK Anchorage, All Campuses"
replace `x' = 1 if `y' == "University of Alaska Anchorage, Anchorage Campus"
replace `x' = 1 if `y' == "University of Massachusetts Lowell"
replace `x' = 1 if `y' == "University of Massachusetts at Amherst"
replace `x' = 1 if `y' == "University of Massachusetts at Dartmouth"
replace `x' = 1 if `y' == "University of Massachusetts at Worcester"
replace `x' = 1 if `y' == "University of Missouri, Kansas City"
replace `x' = 1 if `y' == "University of Missouri, Rolla"
replace `x' = 1 if `y' == "University of Missouri, St Louis"
replace `x' = 1 if `y' == "University of PR Mayaguez Campus"
replace `x' = 1 if `y' == "University of PR Medical Sciences Campus"
replace `x' = 1 if `y' == "University of PR Rio Piedras Campus"
replace `x' = 1 if `y' == "University of Southern Maine"
replace `x' = 1 if `y' == "University of Texas - Pan American"
replace `x' = 1 if `y' == "University of Texas Medical Branch at Galveston"
replace `x' = 1 if `y' == "University of Texas at Tyler"
replace `x' = 1 if `y' == "Southwest Texas State University"
replace `x' = 1 if `y' == "Southern University A&M Col, All Campuses"
replace `x' = 1 if `y' == "Rutgers the State Univ of NJ, All Campuses"
replace `x' = 1 if `y' == "New York Institute of Technology, All Campuses"
replace `x' = 1 if `y' == "New Mexico State University, All Campuses"
replace `x' = 1 if `y' == "La Salle University"
replace `x' = 1 if `y' == "Georgia Regents University"
replace `x' = 1 if `y' == "Embry-Riddle Aeronautical University (FL)"
	* added these with the addition of the 5 year panel
replace `x' = 1 if `y' == "Alliant International University, San Diego"
replace `x' = 1 if `y' == "Bridgeport Engineering Institute"
replace `x' = 1 if `y' == "Purdue University"
replace `x' = 1 if `y' == "University of Massachusetts Amherst"
replace `x' = 1 if `y' == "University of Massachusetts Dartmouth"
replace `x' = 1 if `y' == "Missouri University of Science and Technology"
replace `x' = 1 if `y' == "Plymouth State University"
replace `x' = 1 if `y' == "Fairleigh Dickinson University"
replace `x' = 1 if `y' == "CUNY, Graduate Center"
replace `x' = 1 if `y' == "University of Cincinnati"
replace `x' = 1 if `y' == "Drexel University"
replace `x' = 1 if `y' == "Pennsylvania State University University Park and Hershey Medical Center"
replace `x' = 1 if `y' == "University of Texas-Pan American, The"
replace `x' = 1 if `y' == "University of Texas, Brownsville"
replace `x' = 1 if `y' == "University of Texas Southwestern Medical Center, The"
replace `x' = 1 if `y' == "University of Texas Medical Branch, The"
replace `x' = 1 if `y' == "University of Texas at Arlington, The"
replace `x' = 1 if `y' == "University of Texas at Dallas, The"
replace `x' = 1 if `y' == "University of Texas at El Paso, The"
replace `x' = 1 if `y' == "University of Texas at Tyler, The"
replace `x' = 1 if `y' == "University of Texas at San Antonio, The"
replace `x' = 1 if `y' == "University of Texas Health Science Center at Houston, The"
replace `x' = 1 if `y' == "University of Puerto Rico, Medical Sciences Campus, School of Medicine"
replace `x' = 1 if `y' == "Universidad del Turabo"
replace `x' = 1 if `y' == "University of Puerto Rico at Mayaguez"
replace `x' = 1 if `y' == "Barnes-Jewish College Goldfarb School of Nursing"
replace `x' = 1 if `y' == "Lamar University"
replace `x' = 1 if `y' == "Embry-Riddle Aeronautical University"
replace `x' = 1 if `y' == "Touro University, Vallejo"
replace `x' = 1 if `y' == "Georgia Regents University"
replace `x' = 1 if `y' == "Georgia Regents U."
replace `x' = 1 if `y' == "University of Montana, The, Missoula"
replace `x' = 1 if `y' == "Armstrong State University"
replace `x' = 1 if `y' == "CUNY, College of Staten Island"
replace `x' = 1 if `y' == "New Mexico State University, Las Cruces"
replace `x' = 1 if `y' == "New York Institute of Technology"
replace `x' = 1 if `y' == "St. Mary's University, San Antonio"
replace `x' = 1 if `y' == "University of Alaska Anchorage, Anchorage Campus"
replace `x' = 1 if `y' == "University of Maryland Center for Environmental Science"
replace `x' = 1 if `y' == "University of Massachusetts Medical School"
replace `x' = 1 if `y' == "University of Massachusetts, central office"
replace `x' = 1 if `y' == "University of North Georgia"
replace `x' = 1 if `y' == "University of Puerto Rico Central Administration"
replace `x' = 1 if `y' == "University of Southern Maine"
replace `x' = 1 if `y' == "University of Texas Health Science Center at San Antonio, The"
replace `x' = 1 if `y' == "University of Texas, Brownsville"
replace `x' = 1 if `y' == "University of Texas-Pan American, The"
	** these were not listed as research via DC, but when checking with NCES (http://nces.ed.gov/globallocator/). ALL OF THESE OFFER DOC RESEARCH/SCHOLARSHIP DEGREE
	** I would argue that this sample is more inclusive of "research degree" granting institutions.
	** There is discrepancy between the Carnegie codes and the degree granting.
replace `x' = 1 if `y' == "Alabama A&M University"
replace `x' = 1 if `y' == "Alabama State University"
replace `x' = 1 if `y' == "Alaska Pacific University"
replace `x' = 1 if `y' == "Albany College of Pharmacy and Health Sciences"
replace `x' = 1 if `y' == "Alfred University"
replace `x' = 1 if `y' == "Angelo State University"
replace `x' = 1 if `y' == "Appalachian State University"
replace `x' = 1 if `y' == "Arcadia University"
replace `x' = 1 if `y' == "Arizona State University"
replace `x' = 1 if `y' == "Arkansas State University"
replace `x' = 1 if `y' == "Ashland University"
replace `x' = 1 if `y' == "Bard College"
replace `x' = 1 if `y' == "Bellarmine University"
replace `x' = 1 if `y' == "Boise State University"
replace `x' = 1 if `y' == "Bowie State University"
replace `x' = 1 if `y' == "California State Polytechnic University, Pomona"
replace `x' = 1 if `y' == "California State University, Fresno"
replace `x' = 1 if `y' == "California State University, Los Angeles"
replace `x' = 1 if `y' == "California State University, Northridge"
replace `x' = 1 if `y' == "California State University, San Bernardino"
replace `x' = 1 if `y' == "Central Connecticut State University"
replace `x' = 1 if `y' == "Chapman University"
replace `x' = 1 if `y' == "Chicago State University"
replace `x' = 1 if `y' == "Claremont School of Theology"
replace `x' = 1 if `y' == "Coastal Carolina University"
replace `x' = 1 if `y' == "Columbus State University"
replace `x' = 1 if `y' == "Creighton University"
replace `x' = 1 if `y' == "Dakota State University"
replace `x' = 1 if `y' == "Delaware State University"
replace `x' = 1 if `y' == "Dominican University"
replace `x' = 1 if `y' == "Drake University"
replace `x' = 1 if `y' == "Drew University"
replace `x' = 1 if `y' == "Eastern Michigan University"
replace `x' = 1 if `y' == "Emporia State University"
replace `x' = 1 if `y' == "Fayetteville State University"
replace `x' = 1 if `y' == "Florida Gulf Coast University"
replace `x' = 1 if `y' == "Franklin W. Olin College of Engineering"
replace `x' = 1 if `y' == "Frostburg State University"
replace `x' = 1 if `y' == "Gallaudet University"
replace `x' = 1 if `y' == "Gannon University"
replace `x' = 1 if `y' == "Gonzaga University"
replace `x' = 1 if `y' == "Governors State University"
replace `x' = 1 if `y' == "Grambling State University"
replace `x' = 1 if `y' == "Hamline University"
replace `x' = 1 if `y' == "Hampton University"
replace `x' = 1 if `y' == "Harding University"
replace `x' = 1 if `y' == "Indiana Wesleyan University"
replace `x' = 1 if `y' == "Jacksonville State University"
replace `x' = 1 if `y' == "James Madison University"
replace `x' = 1 if `y' == "Kean University"
replace `x' = 1 if `y' == "Keck Graduate Institute"
replace `x' = 1 if `y' == "Kennesaw State University"
replace `x' = 1 if `y' == "Lawrence Technological University"
replace `x' = 1 if `y' == "Loyola University Maryland"
replace `x' = 1 if `y' == "Loyola University, Los Angeles"
replace `x' = 1 if `y' == "Marshall University"
replace `x' = 1 if `y' == "Marywood University"
replace `x' = 1 if `y' == "Mercer University"
replace `x' = 1 if `y' == "Mercyhurst University"
replace `x' = 1 if `y' == "Middle Tennessee State University"
replace `x' = 1 if `y' == "Middlebury College"
replace `x' = 1 if `y' == "Mills College"
replace `x' = 1 if `y' == "Milwaukee School of Engineering"
replace `x' = 1 if `y' == "Minnesota State University, Moorhead"
replace `x' = 1 if `y' == "Mississippi College"
replace `x' = 1 if `y' == "Montana Tech of University of Montana"
replace `x' = 1 if `y' == "Montclair State University"
replace `x' = 1 if `y' == "National University of Health Sciences"
replace `x' = 1 if `y' == "Naval Postgraduate School"
replace `x' = 1 if `y' == "New Mexico Institute of Mining and Technology"
replace `x' = 1 if `y' == "Niagara University"
replace `x' = 1 if `y' == "Norfolk State University"
replace `x' = 1 if `y' == "North Carolina Central University"
replace `x' = 1 if `y' == "Northern Kentucky University"
replace `x' = 1 if `y' == "Palo Alto University"
replace `x' = 1 if `y' == "Philadelphia University"
replace `x' = 1 if `y' == "Prairie View A&M University"
replace `x' = 1 if `y' == "Prescott College"
replace `x' = 1 if `y' == "Rhode Island College"
replace `x' = 1 if `y' == "Rochester Institute of Technology"
replace `x' = 1 if `y' == "Rollins College"
replace `x' = 1 if `y' == "Rowan University"
replace `x' = 1 if `y' == "Rutgers, The State University of New Jersey"
replace `x' = 1 if `y' == "SUNY, College of Optometry"
replace `x' = 1 if `y' == "Saint Joseph's University"
replace `x' = 1 if `y' == "Salisbury University"
replace `x' = 1 if `y' == "Salus University"
replace `x' = 1 if `y' == "Salve Regina University"
replace `x' = 1 if `y' == "Sam Houston State University"
replace `x' = 1 if `y' == "San Francisco State University"
replace `x' = 1 if `y' == "Santa Clara University"
replace `x' = 1 if `y' == "Seattle Pacific University"
replace `x' = 1 if `y' == "Seattle University"
replace `x' = 1 if `y' == "Simmons College"
replace `x' = 1 if `y' == "Smith College"
replace `x' = 1 if `y' == "Southeastern Louisiana University"
replace `x' = 1 if `y' == "Southern Connecticut State University"
replace `x' = 1 if `y' == "Southern Illinois University, Edwardsville"
replace `x' = 1 if `y' == "Southern University and A&M College"
replace `x' = 1 if `y' == "St. Cloud State University"
replace `x' = 1 if `y' == "Stephen F. Austin State University"
replace `x' = 1 if `y' == "Suffolk University"
replace `x' = 1 if `y' == "Tarleton State University"
replace `x' = 1 if `y' == "Tennessee Technological University"
replace `x' = 1 if `y' == "Texas A&M International University"
replace `x' = 1 if `y' == "Texas A&M University-Corpus Christi"
replace `x' = 1 if `y' == "Texas Southern University"
replace `x' = 1 if `y' == "Texas State University, San Marcos"
replace `x' = 1 if `y' == "Towson University"
replace `x' = 1 if `y' == "Tuskegee University"
replace `x' = 1 if `y' == "Universidad Metropolitana"
replace `x' = 1 if `y' == "University of Arkansas Pine Bluff"
replace `x' = 1 if `y' == "University of Baltimore"
replace `x' = 1 if `y' == "University of California, Merced"
replace `x' = 1 if `y' == "University of Central Arkansas"
replace `x' = 1 if `y' == "University of Detroit Mercy"
replace `x' = 1 if `y' == "University of Hawaii at Hilo"
replace `x' = 1 if `y' == "University of Houston-Clear Lake"
replace `x' = 1 if `y' == "University of Louisiana at Monroe"
replace `x' = 1 if `y' == "University of Maryland Eastern Shore"
replace `x' = 1 if `y' == "University of Missouri, Saint Louis"
replace `x' = 1 if `y' == "University of New Haven"
replace `x' = 1 if `y' == "University of North Carolina at Wilmington, The"
replace `x' = 1 if `y' == "University of North Florida"
replace `x' = 1 if `y' == "University of Redlands"
replace `x' = 1 if `y' == "University of South Alabama"
replace `x' = 1 if `y' == "University of West Georgia"
replace `x' = 1 if `y' == "University of the Incarnate Word"
replace `x' = 1 if `y' == "Valdosta State University"
replace `x' = 1 if `y' == "Villanova University"
replace `x' = 1 if `y' == "Wesleyan University"
replace `x' = 1 if `y' == "West Texas A&M University"
replace `x' = 1 if `y' == "Western Carolina University"
replace `x' = 1 if `y' == "Western Illinois University"
replace `x' = 1 if `y' == "Wheaton College, Wheaton"
replace `x' = 1 if `y' == "Wilkes University"
replace `x' = 1 if `y' == "Youngstown State University"
}
}
	* this search added 6.39 percent of the full sample to the research institutions

sum research_uni_c05_DC doctoral_uni_c05_DC
replace research_uni_c05_DC = 1 if research_institutions_missing == 1
replace doctoral_uni_c05_DC = 1 if research_institutions_missing == 1
foreach x in research_uni_c05_DC doctoral_uni_c05_DC{
sum `x' if `x' == 1
}
	* Research: 52,120 obs (more accurate measure given the handmatch procedure)
	* Doctoral: 46,099 obs
	
	* With the NCES granting requirement
		* Research: 71,653
		* Doctoral: 65,632
lab var research_uni_c05_DC "Research Universities (derived from Carnegie05, cleaned)" 
lab var doctoral_uni_c05_DC "Doctoral Universities *only* (derived from Carnegie05, cleaned, may be overestiamte)"
foreach x in total_exp_log {
foreach y in doctoral_uni_c05_DC research_uni_c05_DC {
set more off
sort `y'
ttest `x', by(`y') unequal
}
}
save "$dir/webcaspar_table2010_2013/NCSES IPEDS NRC Delta Cost data 10_14 cleaned.dta", replace

********************************************************************************
*STEP 6: NCSES ANALYSIS 
	* First, clean up the sample to identify S&E departments from doctoral granting institutions with active federal R&D portfolios
	* Second, run econometric analyses 
********************************************************************************
clear all 
set more off
set matsize 500
global dir "/Users/llanahan/Dropbox/Uni Funding Sources/Data"
use "$dir/webcaspar_table2010_2013/NCSES IPEDS NRC Delta Cost data 10_14 cleaned.dta"

**** Unique IDs ****
** unique year, university, dept id: unique_id
lab var fice "Unique university id"
lab var dept_id "Unique department id"
gen double dept_to_id = dept_id * 0.01
gen double fice_dept = fice+dept_to_id
lab var fice_dept "Unique university-department id"
tab year, gen(year_dum)
tab dept_broad, gen(dept_broad_dum)

********************************************************************************
** Reclassify the broad departments as follows
** NOTE ** Variable: dept_broad_reclassify 
** Engineering == 1
** Physical Sciences == 2
** Environmental Sciences == 3
** Mathematical Sciences & Computer Sciences == 4
** Life Sciences == 5
** Social Sciences & Psychology == 6
** Other Sciences == 7
** Other Non-Sciences (e.g. Humanities) == 10 ** note, this has been dropped.

foreach y in dept_broad_reclassify {
foreach x in dept_broad {
gen `y' = 1
replace `y' = 1 if `x' == 1
replace `y' = 2 if `x' == 2
replace `y' = 3 if `x' == 3
replace `y' = 4 if `x' == 4 | `x' == 5
replace `y' = 5 if `x' == 6
replace `y' = 6 if `x' == 7 | `x' == 8
replace `y' = 7 if `x' == 9
replace `y' = 8 if `x' == 10
}
}

***************************************
** Drop non-research universities 
drop if research_uni_c05_DC == 0
	*drop 47,651
***************************************
** Drop Humanities 
drop if dept_broad_reclassify == 8 
	*drop 16,384
***************************************
** Drop Interdisciplinary
drop if dept_broad_reclassify == 7 
	*drop 2,047
***************************************
gen dept_all_sciences = 1
	/* Total number of observations: 53,222
	Distribution
	2010: 10634
	2011: 11544
	2012: 10582
	2013: 10244
	2014: 10218 
		Total number of departments: 11,674
		Balance of R&D funding data: 84.19% balanced; 4.9% with only 1 panel
	Total number of universities: 449
	*/

gen public_dummy = 1 if control == "Public"
recode public_dummy (.=0)
lab var public_dummy "Public dummy (1), Private (0)"
gen private_dummy = 1 if control == "Private"
recode private_dummy (.=0)
lab var private_dummy "Private dummy (1), Public (0)"
sum public_dummy private_dummy
	* 64.4% public

tab dept_broad_reclassify, gen(dept_broad_reclassify_dum)
	
sort Year
set more off
by Year: sum total_exp 
by Year: sum total_exp if total_exp > 0, detail

* Determine sample with complete data for all funding sources -- this sample is used with the regression analysis
sort fice_dept year
br total_exp fed_exp state_local_exp business_exp nonprofit_exp university_exp other_exp
sum total_exp fed_exp state_local_exp business_exp nonprofit_exp university_exp other_exp
egen uni_num = group(fice)
sum uni_num
egen dept_num = group(fice_dept)
sum dept_num
xtset fice_dept year
xtdescribe
sort public_dummy
egen dept_num_pub = group(fice_dept) if public_dummy == 1
egen dept_num_pri = group(fice_dept) if public_dummy == 0
sum dept_num_pub  dept_num_pri

foreach x in fed_exp state_local_exp business_exp nonprofit_exp {
set more off
xtdescribe if `x' > 0
}

/*******************************************************************************
Assess fields with missing information
*******************************************************************************/
foreach x in fed_exp_df_log business_exp_df_log state_local_exp_df_log nonprofit_exp_df_log other_exp_df_log {
sum `x' if `x' > 0
}
foreach x in fed_exp_df business_exp_df state_local_exp_df nonprofit_exp_df other_exp_df {
sum `x' if `x' > 0
}
	* there are a considerable number of missing values for the fields in this analysis
/*******************************************************************************
Assess fields without any missing information -- notably, those with Federal R&D activity
*******************************************************************************/
xtset fice_dept year
br fice_dept year fed_exp_df_log business_exp_df_log state_local_exp_df_log nonprofit_exp_df_log other_exp_df_log
foreach x in fed_exp_df_log business_exp_df_log state_local_exp_df_log nonprofit_exp_df_log other_exp_df_log {
gen any_`x' = 1 if `x' > 0
recode any_`x' (.=0)
}
sort fice_dept year
egen full_panel_fed = sum(any_fed_exp_df_log), by (fice_dept)
egen full_panel_business = sum(any_business_exp_df_log), by (fice_dept)
egen full_panel_state_local = sum(any_state_local_exp_df_log), by (fice_dept)
egen full_panel_nonprofit = sum(any_nonprofit_exp_df_log), by (fice_dept)
egen full_panel_other = sum(any_other_exp_df_log), by (fice_dept)

foreach x in full_panel_fed full_panel_business full_panel_state_local full_panel_nonprofit full_panel_other {
sum `x' if `x' == 5
}

foreach x in full_panel_fed full_panel_business full_panel_state_local full_panel_nonprofit full_panel_other {
sum `x' if `x' > 0
}

/*******************************************************************************
Descriptive Statisitics of sample across the various sub-populations under consideration
	1. Full sample: 53,222
	2. Any Federal R&D over 5 year panel
	3. Any Federal R&D for each panel (We have decided to focus on this sample of the population for the analysis.
*******************************************************************************/
tab dept_broad_reclassify, gen(dept_broad_reclassify_d)
preserve
*keep if full_panel_fed > 0
*keep if full_panel_fed == 5
sum fed_exp_df_log business_exp_df_log state_local_exp_df_log nonprofit_exp_df_log other_exp_df_log 
foreach x in fed_exp_df_log business_exp_df_log state_local_exp_df_log nonprofit_exp_df_log other_exp_df_log {
sum `x' if `x' > 0 

}
restore
	* By Broad Field
preserve
keep if full_panel_fed > 0
*keep if full_panel_fed == 5
foreach y in dept_all_sciences dept_broad_reclassify_d1 dept_broad_reclassify_d2 dept_broad_reclassify_d3 dept_broad_reclassify_d4 dept_broad_reclassify_d5 dept_broad_reclassify_d6 {
set more off 
sum fed_exp_df_log business_exp_df_log state_local_exp_df_log nonprofit_exp_df_log other_exp_df_log if `y' == 1
}
restore

preserve
*keep if full_panel_fed > 0
keep if full_panel_fed == 5
set more off
foreach y in dept_all_sciences dept_broad_reclassify_d1 dept_broad_reclassify_d2 dept_broad_reclassify_d3 dept_broad_reclassify_d4 dept_broad_reclassify_d5 dept_broad_reclassify_d6  {
foreach x in fed_exp_df_log business_exp_df_log state_local_exp_df_log nonprofit_exp_df_log other_exp_df_log {
sum `x' if `y' == 1 & `x' > 0
}
}
restore

/*******************************************************************************
	DEFINE SAMPLE for PLOS One (January 2016)
1. Academic field has positive federal R&D activity over the entire 5-year panel
2. University is "Doctoral Granting" (as defined by the NCSES classification)
3. University is not "Specialized Institution" (i.e. soley medical or engineering institution)
*******************************************************************************/

br fice_dept year fed_exp_df_log full_panel_fed doctoral_uni_c05_DC c05_DC_15 c05_DC_17

* Draw from NCES (WebCASPAR) institutional reporting; hdg = doctoral; no special institutions; 5 years of federal R&D activity
gen hdg_doctoral = 1 if full_panel_fed == 5
	*19,785 observations have federal funding over 5 year panel
replace hdg_doctoral = . if hdg != "Doctorate-granting"
	*17,610 observations are "Doctorate-granting" as defined by HDG
replace hdg_doctoral = . if c05_DC_15 == 1 | c05_DC_17 == 1
	*310 of those obervations were from special institutions
	*Total number of observations: 17,300 *

****************************************************************************************************************************************************************
****************************************************************************************************************************************************************
	* ANALYSIS * FOR PLOS One 
****************************************************************************************************************************************************************
****************************************************************************************************************************************************************
* WE HAVE SELECTED THE MOST RESTRICTIVE SAMPLE FOR THE ANALYSIS

keep if hdg_doctoral == 1
keep total_exp_df fed_exp_df state_local_exp_df business_exp_df nonprofit_exp_df university_exp_df ///
other_exp_df total_exp fed_exp state_local_exp business_exp nonprofit_exp university_exp ///
other_exp dept_broad_reclassify FICE Year fice public_dummy private_dummy fice_dept dept university city state ///
zipcode dept_all_sciences dept_broad_reclassify_d1 dept_broad_reclassify_d2 ///
dept_broad_reclassify_d3 dept_broad_reclassify_d4 dept_broad_reclassify_d5 ///
dept_broad_reclassify_d6 state_local_exp_log state_local_exp_df_log business_exp_log business_exp_df_log ///
nonprofit_exp_log nonprofit_exp_df_log university_exp_log university_exp_df_log other_exp_log ///
other_exp_df_log total_exp_log total_exp_df_log fed_exp_log fed_exp_df_log dept_id year
save "$dir/webcaspar_table2010_2013/Scholars Bank 10_14 Cleaned.dta", replace

lab var fed_exp_df_log "Federal"
lab var business_exp_df_log "Industry"
lab var state_local_exp_df_log "State & Local"
lab var nonprofit_exp_df_log "Nonprofit"
lab var other_exp_df_log "Other"

********************************************************************************
* Descriptive Statistics
********************************************************************************
* Figure 1: Total Funding (annual average) by source and broad field

** Variable: dept_broad_reclassify
	** Engineering == 1
	** Physical Sciences == 2
	** Environmental Sciences == 3
	** Mathematical Sciences & Computer Sciences == 4
	** Life Sciences == 5
	** Social Sciences & Psychology == 6

sort FICE Year
by FICE Year: egen total_uni_exp = sum(total_exp)
by FICE Year: egen total_uni_exp_df = sum(total_exp_df)

** WITH REAL VALUES
sum total_exp_df fed_exp_df state_local_exp_df business_exp_df nonprofit_exp_df university_exp_df other_exp_df
sort dept_broad_reclassify
set more off
by dept_broad_reclassify: sum total_exp_df fed_exp_df state_local_exp_df business_exp_df nonprofit_exp_df university_exp_df other_exp_df

** Average total (data for parentheses)
br fice_dept dept_broad_reclassify Year total_exp_df 
sort dept_broad_reclassify Year 
set more off
by dept_broad_reclassify Year: egen total_dept_annual_funding = sum(total_exp)
by dept_broad_reclassify: sum total_dept_annual_funding if Year == 2014
sort Year
by Year: egen total_annual_funding = sum(total_exp)
sum total_annual_funding if Year == 2014

* Additional Descriptive Statistics
	* Missing values
foreach x in fed_exp_df_log business_exp_df_log state_local_exp_df_log nonprofit_exp_df_log other_exp_df_log {
sum `x' if `x' > 0
*sum `x'
}

* Number of universities: 341 (HDG: 266)
egen uni_num_plos = group(fice)
sum uni_num_plos
egen uni_num_pub_plos = group (fice) if public_dummy ==1
egen dept_num_pub_plos = group (fice_dept) if public_dummy ==1

* Number of unique departments: 3887 (HDG: 3460)
egen dept_num_plos = group(fice_dept)
sum dept_num_plos
	
tab dept_id
tab dept_broad_reclassify

* Confirm the panel is fully balanced
xtset fice_dept year
xtdescribe

tab fice
* Average university has 11 narrow departments with full panel of federal R&D funding.
	* Min: 1; Max: 26; SD: 6.8

********************************************************************************
* Regression Analysis for paper submission
********************************************************************************
xtset fice_dept year
set more off

**** Table 1: Results for 3 primary outcomes ****
		*XTABOND2 (uncanned version) 
		*$y: endogenous; $x: predetermined; $z: endogenousß
		*one step GMM Command, robust is equivalent to cluster(id), where id if the panel identifier (Roodman, 2006; p. 37)

global x fed_exp_df_log
global y business_exp_df_log
global z state_local_exp_df_log nonprofit_exp_df_log other_exp_df_log
xi: xtabond2 $y $x $z l.$y i.year, gmmstyle($x l.$z l.$y) ivstyle(i.year) robust 
	*$y: endogenous; $x: predetermined; $z: endogenous
	*Test whether errors are serially autocorrelated: if 2 is not significant it indicates no serial autocorrelation
	*Sargan test of overidentification: if Prob > chi2 is insignificant, then the moment of conditions are correct.
outreg2 using "$dir/Results/full output.xls", bdec(3) rdec(3) replace ctitle(Industry outcome) label addtext(Department FE, Yes) 
global y state_local_exp_df_log
global z business_exp_df_log nonprofit_exp_df_log other_exp_df_log 
xi: xtabond2 $y $x $z l.$y i.year, gmmstyle($x l.$z l.$y) ivstyle(i.year) robust 
	*$y: endogenous; $x: predetermined; $z: endogenous
outreg2 using "$dir/Results/full output.xls", bdec(3) rdec(3) append ctitle(State & Local outcome) label addtext(Department FE, Yes) 
global y nonprofit_exp_df_log
global z business_exp_df_log state_local_exp_df_log other_exp_df_log 
xi: xtabond2 $y $x $z l.$y i.year, gmmstyle($x l.$z l.$y) ivstyle(i.year) robust 
	*$y: endogenous; $x: predetermined; $z: endogenous
outreg2 using "$dir/Results/full output.xls", bdec(3) rdec(3) append ctitle(Nonprofit outcome) label addtext(Department FE, Yes) 

**** Tables 2 - 4: OLS, FE, and Xtabond2 results for 3 non-federal R&D outcomes ****
global x fed_exp_df_log
	* Table 2
*global y business_exp_df_log
*global z state_local_exp_df_log nonprofit_exp_df_log other_exp_df_log
	* Table 3
*global y state_local_exp_df_log
*global z business_exp_df_log nonprofit_exp_df_log other_exp_df_log 
	* Table 4
global y nonprofit_exp_df_log
global z business_exp_df_log state_local_exp_df_log other_exp_df_log 

set more off 
	*1* FINAL MODEL - XTABOND2 
xi: xtabond2 $y $x $z l.$y i.year, gmmstyle($x l.$z l.$y) ivstyle(i.year) robust 
	*$y: endogenous; $x: predetermined; $z: endogenous
outreg2 using "$dir/Results/full output.xls", bdec(3) rdec(3) replace ctitle(Primary Model) label addtext(Department FE, Yes) 
	*2* POOLED OLS*
tsset fice_dept year
reg $y $x $z i.year l.$y l2.$y, cluster(fice_dept)
outreg2 $y $x $z i.year l.$y l2.$y using "$dir/Results/full output.xls", bdec(3) rdec(3) append ctitle(Double Lag OLS) label addtext(Department FE, No) 
	*3* FIXED EFFECTS*
xtset fice_dept year
xi: xtreg $y $x $z i.year, fe
outreg2 $y $x $z i.year using "$dir/Results/full output.xls", append ctitle(FE) label addtext(Department FE, Yes) 
	*4* XTABOND2: standard errors are adjusted for clustering on group variable (id)
xi: xtabond2 $y $x $z l.$y i.year, gmmstyle($x $z l.$y) ivstyle(i.year) robust 
	*$y: endogenous; $x: predetermined; $z: predetermined
outreg2 using "$dir/Results/full output.xls", bdec(3) rdec(3) append ctitle(xtabond2: pre x&z end y) label addtext(Department FE, Yes) 

/* Note: the sargan test grows weaker as the number of instruments increases. "Indeed, Sargan himself (1958) determined without the aid of modern computers that the error in the his test is ”proportional to the number of instrumental variables, so that, if the asymptotic approximations are to be used, this number must be small.”" Roodman
It is good practice to report the instrument count and test the robustness of results to reducing it. The next sections describe the instrument sets typical of difference and system GMM, and ways to contain them with xtabond2*/

********************************************************************************
	/* 3 Sets of STRATIFICATIONS
		1. Broad Academic Field
		2. High Research Capacity & Field
		3. Public vs. Private & Field
*******************************************************************************/

********************************************************************************
* Field Stratification
	* Need to run the code for each respective outcome for each robustness and senstitivty check
		*3 Outcomes: nonprofit; state & local; industry
		*Specifications:
			*Primary model
			*Primary model with relaxed assumptions
			*Fixed effects
			*Sensitivity check for primary model	
********************************************************************************
** Base Line ** Logged form with deflated values
global x fed_exp_df_log
*global y business_exp_df_log
*global z state_local_exp_df_log nonprofit_exp_df_log other_exp_df_log
*global y state_local_exp_df_log
*global z business_exp_df_log nonprofit_exp_df_log other_exp_df_log
global y nonprofit_exp_df_log
global z business_exp_df_log state_local_exp_df_log other_exp_df_log 
*/
	/* Sensitivity Check 1 ** Logged form with nominal values
	global x fed_exp_log
	*global y business_exp_log
	*global z state_local_exp_log nonprofit_exp_log other_exp_log 
	*global y state_local_exp_log
	*global z business_exp_log nonprofit_exp_log other_exp_log 
	global y nonprofit_exp_log
	global z business_exp_log state_local_exp_log other_exp_log 
	*/
matrix m1=(1)
matrix m2=(1)
matrix m3=(1)
matrix m4=(1)
foreach dept in dept_all_sciences dept_broad_reclassify_d1 dept_broad_reclassify_d2 dept_broad_reclassify_d3 dept_broad_reclassify_d4 dept_broad_reclassify_d5 dept_broad_reclassify_d6 {
foreach x in $x {
set more off
xtset fice_dept year
preserve
keep if `dept' == 1
** Arellano Bond
*xi: xtabond2 $y $x $z l.$y i.year, gmmstyle($x l.$z l.$y) ivstyle(i.year) robust 
	*$y: endogenous; $x: predetermined; $z: endogenous
*xi: xtabond2 $y $x $z l.$y i.year, gmmstyle($x $z l.$y) ivstyle(i.year) robust 
	*$y: endogenous; $x: predetermined; $z: predetermined
** Fixed Effects
xtreg $y $x $z $z1 i.year, fe
matrix mp = r(table)
matrix m1 = m1\(_b[`x'])
matrix m2 = m2\(_se[`x'])
matrix m3 = m3\(e(N))
matrix m4 = m4\mp[4,1]
matrix drop mp
restore
}
}
matrix q1 = (m1[2..8,1..1],m2[2..8,1..1],m4[2..8,1..1],m3[2..8,1..1])
matrix colnames q1 = Coefficient Standard_Error P_value Observations 
matrix rownames q1 = Total Eng Phys_Sci Env_Sci Math_CompSci Life_Sci Psych_SocSci 
matrix drop m1 m2 m3 m4 
mat list q1 

********************************************************************************
	* High research capacity stratification
********************************************************************************
** recode quartile rankings for reclassified departments. 
***************************************Department*******************************
sort Year dept_broad_reclassify 
forval x=25(25)75{
by Year dept_broad_reclassify: egen p_tot_dept_exp_rcl`x'=pctile(total_exp),p(`x')
}
forval x=25(25)75{
gen q_tot_dept_exp_rcl`x'=0
replace q_tot_dept_exp_rcl`x'=1 if total_exp>p_tot_dept_exp_rcl`x'
}
egen Q_tot_dept_exp_rcl = rowtotal(q_tot_dept_exp_rcl25-q_tot_dept_exp_rcl75)
replace Q_tot_dept_exp_rcl=Q_tot_dept_exp_rcl+1
tabulate Q_tot_dept_exp_rcl, generate(d_totdeptexp_rcl_quartile)
label variable d_totdeptexp_rcl_quartile1 "Total Exp (by dept & year) Q1, reclassify"
label variable d_totdeptexp_rcl_quartile2 "Total Exp (by dept & year) Q2, reclassify"
label variable d_totdeptexp_rcl_quartile3 "Total Exp (by dept & year) Q3, reclassify"
label variable d_totdeptexp_rcl_quartile4 "Total Exp (by dept & year) Q4, reclassify"
** Quartile rankings vary overtime. Want to assign a signal that department ever had high research activity for department and university level
sort fice_dept
by fice_dept: egen H_res_dept_rcl = max(d_totdeptexp_rcl_quartile4)
gen L_res_dept_rcl = 1 if H_res_dept_rcl == 0
recode L_res_dept_rcl (.=0)

********************************************************************************
* High research capacity stratification
	* Need to run the code for each respective outcome for each robustness and senstitivty check
		*3 Outcomes: nonprofit; state & local; industry
		*Specifications:
			*Primary model
			*Primary model with relaxed assumptions
			*Fixed effects
			*Sensitivity check for primary model
********************************************************************************
** Base Line ** Logged form with deflated values
global x fed_exp_df_log
global y business_exp_df_log
global z state_local_exp_df_log nonprofit_exp_df_log other_exp_df_log
*global y state_local_exp_df_log
*global z business_exp_df_log nonprofit_exp_df_log other_exp_df_log 
*global y nonprofit_exp_df_log
*global z business_exp_df_log state_local_exp_df_log other_exp_df_log
	/** Sensitivity Check 1 ** Logged form with nominal values
	global x fed_exp_log
	global y business_exp_log
	global z state_local_exp_log nonprofit_exp_log other_exp_log 
	*global y state_local_exp_log	
	*global z business_exp_log nonprofit_exp_log other_exp_log 
	*global y nonprofit_exp_log
	*global z business_exp_log state_local_exp_log other_exp_log 
	*/
matrix m1=(1)
matrix m2=(1)
matrix m3=(1)
matrix m4=(1)
foreach strat in H_res L_res {
foreach x in $x {
set more off
xtset fice_dept year
preserve
keep if `strat' == 1
** Arellano Bond
*xi: xtabond2 $y $x $z l.$y i.year, gmmstyle($x l.$z l.$y) ivstyle(i.year) robust 
	*$y: endogenous; $x: predetermined; $z: endogenous
*xi: xtabond2 $y $x $z l.$y i.year, gmmstyle($x $z l.$y) ivstyle(i.year) robust 
	*$y: endogenous; $x: predetermined; $z: predetermined
xtreg $y $x $z i.year, fe
matrix mp = r(table)
matrix m1 = m1\(_b[`x'])
matrix m2 = m2\(_se[`x'])
matrix m3 = m3\(e(N))
matrix m4 = m4\mp[4,1]
matrix drop mp
restore
}
}
foreach strat in H_res_dept_rcl L_res_dept_rcl {
foreach dept in dept_broad_reclassify_d1 dept_broad_reclassify_d2 dept_broad_reclassify_d3 dept_broad_reclassify_d4 dept_broad_reclassify_d5 dept_broad_reclassify_d6 {
foreach x in $x {
set more off
xtset fice_dept year
preserve
keep if `dept' == 1
keep if `strat' == 1
*xi: xtabond2 $y $x $z l.$y i.year, gmmstyle($x l.$z l.$y) ivstyle(i.year) robust 
	*$y: endogenous; $x: predetermined; $z: endogenous
*xi: xtabond2 $y $x $z l.$y i.year, gmmstyle($x $z l.$y) ivstyle(i.year) robust 
	*$y: endogenous; $x: predetermined; $z: predetermined
xtreg $y $x $z i.year, fe
matrix mp = r(table)
matrix m1 = m1\(_b[`x'])
matrix m2 = m2\(_se[`x'])
matrix m3 = m3\(e(N))
matrix m4 = m4\mp[4,1]
matrix drop mp
restore
}
}
}
matrix q1 = (m1[2..15,1..1],m2[2..15,1..1],m4[2..15,1..1],m3[2..15,1..1])
matrix colnames q1 = Coefficient Standard_Error P_Value Observations
foreach x in H {
foreach y in L {
matrix rownames q1 = Total_`x' Total_`y' Eng_`x' Phys_Sci_`x' Env_Sci_`x' Math_CompSci_`x' Life_Sci_`x' Psych_SocSci_`x' Eng_`y' Phys_Sci_`y' Env_Sci_`y' Math_CompSci_`y' Life_Sci_`y' Psych_SocSci_`y' 
}
}
matrix drop m1 m2 m3 m4
mat list q1 

********************************************************************************
*3* Code for Public VS. Private ** need to run for three separate models
	* Need to run the code for each respective outcome for each robustness and senstitivty check
		*3 Outcomes: nonprofit; state & local; industry
		*Specifications:
			*Primary model
			*Primary model with relaxed assumptions
			*Fixed effects
			*Sensitivity check for primary model
********************************************************************************
** Base Line ** Logged form with deflated values
global x fed_exp_df_log
*global y business_exp_df_log
*global z state_local_exp_df_log nonprofit_exp_df_log other_exp_df_log
global y state_local_exp_df_log
global z business_exp_df_log nonprofit_exp_df_log other_exp_df_log 
*global y nonprofit_exp_df_log
*global z business_exp_df_log state_local_exp_df_log other_exp_df_log
	/* Sensitivity Check 1 ** Logged form with nominal values
	global x fed_exp_log
	*global y business_exp_log
	*global z state_local_exp_log nonprofit_exp_log other_exp_log 
	global y state_local_exp_log
	global z business_exp_log nonprofit_exp_log other_exp_log 
	*global y nonprofit_exp_log
	*global z business_exp_log state_local_exp_log other_exp_log 
	*/
matrix m1=(1)
matrix m2=(1)
matrix m3=(1)
matrix m4=(1)
foreach strat in public_dummy private_dummy {
foreach dept in dept_all_sciences dept_broad_reclassify_d1 dept_broad_reclassify_d2 dept_broad_reclassify_d3 dept_broad_reclassify_d4 dept_broad_reclassify_d5 dept_broad_reclassify_d6 {
foreach x in $x {
set more off
xtset fice_dept year
preserve
keep if `dept' == 1
keep if `strat' == 1
** Arellano Bond
*xi: xtabond2 $y $x $z l.$y i.year, gmmstyle($x l.$z l.$y) ivstyle(i.year) robust 
	*$y: endogenous; $x: predetermined; $z: endogenous
*xi: xtabond2 $y $x $z l.$y i.year, gmmstyle($x $z l.$y) ivstyle(i.year) robust 
	*$y: endogenous; $x: predetermined; $z: predetermined
xtreg $y $x $z $z1 i.year, fe
matrix mp = r(table)
matrix m1 = m1\(_b[`x'])
matrix m2 = m2\(_se[`x'])
matrix m3 = m3\(e(N))
matrix m4 = m4\mp[4,1]
restore
}
}
}
matrix q1 = (m1[2..15,1..1],m2[2..15,1..1],m4[2..15,1..1],m3[2..15,1..1])
matrix colnames q1 = Coefficient Standard_Error P_Value Observations
foreach x in Pub {
foreach y in Pri {
matrix rownames q1 = Total_`x' Eng_`x' Phys_Sci_`x' Env_Sci_`x' Math_CompSci_`x' Life_Sci_`x' Psych_SocSci_`x' Total_`y' Eng_`y' Phys_Sci_`y' Env_Sci_`y' Math_CompSci_`y' Life_Sci_`y' Psych_SocSci_`y' 
}
}
matrix drop m1 m2 m3 m4
mat list q1 
	
end