


/******************************************************************************/
/*****************************Introductory remarks*****************************/
/******************************************************************************/

/*
- This do-file calculates the total cost of borrowing (TCB) measure using the reduced
  model provided in Online Appendix D. 
- It requires as input the variables described below under Step 0
- We also provides a dta-file that contains the TCB-measure alongside the FacilityID
  and loan type (file "TCB.dta") 
*/



/*Step 0: Read data*/
	/*Required variables:
	1) Variables that directly enter the TCB-calculation
	- Maturity_w 			Dealscan	Maturity in months, winsorized
	- AFR0_w				Dealscan	Facility Fee in bps, set to zero if no facility fee exists, winsorized (1 percent level)
	- CF0_w					Dealscan	Commitment Fee in bps, set to zero if no commitment fee exists, winsorized (1 percent level)
	- LIBOR_w				Dealscan	Spread over LIBOR in bps, winsorized (1 percent level)
	- UTF0_w				Dealscan	Utilization Fee in bps, set to zero if no utilization fee exists, winsorized (1 percent level)
	- CAF0_w				Dealscan	Cancellation Fee in bps, set to zero if no cancellation fee exists, winsorized (1 percent level)
	2) Variables that are needed to predict usage rates (ONLY FOR CREDIT LINES) 
	- LoanType2				Dealscan	Variable equal to "TermLoan" or "Revolver"
	- AISUAISD_ratio_w		Dealscan	ASIU/AISD-ratio, winsorized (1 percent level)
	- UTFd					Dealscan	Dummy equal to one if utilization fee exists
	- PPcont_w				Dealscan	Continuous performance pricing measure in bps, defined as ((SPREADmax - SPREAD) - (SPREAD - SPREADmin), winsorized (1 percent level)
	- SyndicateSize_w		Dealscan	Syndicate Size, winsorized (1 percent level)
	- Prof_w				Compustat	Profitability, winsorized (1 percent level)
	- logta_w				Compustat	Log(TotalAssets), total assets in USD mn deflated/inflated to 2005, winsorized (1 percent level)
	- logcoverage_w			Compustat	Log(1+CoverageRatio), winsorized (1 percent level)
	- lev_w					Compustat	Leverage, winsorized (1 percent level)
	- sprat_catmiss4		Compustat	Rating status according to S&P: IG, non-IG, not-rated
	- PrimaryPurpose2		Dealscan	Primary purpose
	3) Variables that are needed to predict upfront fee (if necessary)
	- Secured_d				Dealscan	Dummy equal to one if facility is secured
	- SyndicateSize_w		Dealscan	Number of syndicate members
	- LeadSize_w			Dealscan	Number of lead arrangers
	- logta_w				Compustat	Log(TotalAssets), total assets in USD mn defalated/inflated to 2005, winsorized (1 percent level)
	- logcoverage_w			Compustat	Log(1+CoverageRatio), winsorized (1 percent level)
	- sprat_catmiss4		Compustat	Rating status according to S&P: IG, non-IG, not-rated
	- LoanType				Dealscan	Revolver/Line < 1 Yr., Revolver/Line >= 1 Yr., Term Loan, Institutional Term Loan, Delay Draw Term Loan
	- PrimaryPurpose2		Dealscan	Primary purpose
	*/
	
	
	/*******************************************************************************************************************/
	/****************  Import your data set into stata *****************************************************************/
	/****************  Data set should include variables listed above **************************************************/
	/****************  Please check unit of variables (USD, USD mn, etc.), might deviate from Dealscan/Compustat********/
	/****************  (example:  total assets should be in USD mn AND deflated/inflated to 2005)***********************/
	/*******************************************************************************************************************/
	

/*Step 1: Predict usage rates*/
	/*Step 1a: Mean usage rate*/
		*For term loans, set usage = 1
		gen meanusage_predict = 1 if LoanType2 == "TermLoan"
		*For credit lines, estimate usage rate with reduced model
		#d;
		replace meanusage_predict = 	0.404018
									+ 0.322277 * (UTFd==0) * AISUAISD_ratio_w 
									- 0.006881 * (UTFd==1) * AISUAISD_ratio_w
									+ 0.121858 * UTFd
									- 0.003690 * UTF0_w
									- 0.000259 * PPcont_w
									+ 0.002757 * SyndicateSize_w
									+ 0.157456 * prof_w
									- 0.036942 * logta_w
									- 0.025452 * logcoverage_w
									+ 0.148250 * lev_w
									+ 0.015765 * (sprat_catmiss4 == 2) /*non-IG*/
									+ 0.071090 * (sprat_catmiss4 == 3) /*not-rated*/
									- 0.016161 * (PrimaryPurpose2 == "Work. cap.")
									+ 0.079857 * (PrimaryPurpose2 == "Debt Repay.")
									+ 0.036134 * (PrimaryPurpose2 == "Takeover")
									+ 0.017891 * (PrimaryPurpose2 == "CP backup")
									+ 0.033975 * (PrimaryPurpose2 == "Acquis. line")
									+ 0.027247 * (PrimaryPurpose2 == "Other")
									+ 0.045916 * (PrimaryPurpose2 == "LBO/MBO")
									- 0.001041 * (PrimaryPurpose2 == "Recap.")
									+ 0.285240 * (PrimaryPurpose2 == "Debtor-in-poss.")
				if LoanType2 == "Revolver";
		#d cr
		*Make sure estimated usage rate is in [0,1]
		replace meanusage_predict = max(0, min(1, meanusage_predict)) if meanusage_predict != . 
		
	
	/*Step 1b: Probability that usage > 30%*/
		*For term loans, set probability = 1
		gen usage30d_predict = 1 if LoanType2 == "TermLoan"
		*For credit lines, estimate usage rate with reduced model
		#d;
		replace usage30d_predict = 	0.659377
									+ 0.352106 * (UTFd==0) * AISUAISD_ratio_w 
									- 0.204244 * (UTFd==1) * AISUAISD_ratio_w
									+ 0.200437 * UTFd
									- 0.005860 * UTF0_w
									- 0.000249 * PPcont_w
									+ 0.005131 * SyndicateSize_w
									+ 0.338015 * prof_w
									- 0.066010 * logta_w
									- 0.043726 * logcoverage_w
									+ 0.196485 * lev_w
									+ 0.052304 * (sprat_catmiss4 == 2) /*non-IG*/
									+ 0.120964 * (sprat_catmiss4 == 3) /*not-rated*/
									- 0.013920 * (PrimaryPurpose2 == "Work. cap.")
									+ 0.110207 * (PrimaryPurpose2 == "Debt Repay.")
									+ 0.079544 * (PrimaryPurpose2 == "Takeover")
									+ 0.016903 * (PrimaryPurpose2 == "CP backup")			
									+ 0.070886 * (PrimaryPurpose2 == "Acquis. line")
									+ 0.088707 * (PrimaryPurpose2 == "Other")		/*=all other types not listed here and not equal to "Corp. purposes" (baseline category)*/
									+ 0.020391 * (PrimaryPurpose2 == "LBO/MBO")		/*=LBO, MBO*/
									+ 0.159105 * (PrimaryPurpose2 == "Recap.")							
									+ 0.366676 * (PrimaryPurpose2 == "Debtor-in-poss.")
				if LoanType2 == "Revolver";
		#d cr
		*Make sure estimated usage rate is in [0,1]
		replace usage30d_predict = max(0, min(1, usage30d_predict)) if usage30d_predict != . 
	
	
	
/*Step 2: Predict ufront fee (for sample with missing upfront fee information)*/
		#d;
		gen UFR_predict = - 3.324947

							+  21.430367 * Secured_d
							-   0.666990 * SyndicateSize_w
							+  10.188443 * LeadSize_w
							+   3.863522 * logta_w
							-   5.250414 * logcoverage_w
							+   4.750910 * (sprat_catmiss4 == 2) /*non-IG*/
							+  15.916019 * (sprat_catmiss4 == 3) /*not-rated*/
							+  30.361361 * (LoanType == "Delay Draw Term Loan")	
							+   9.762851 * (LoanType == "Institutional Term Loan") /*= Term Loan B - Term Loan H*/
							-   2.371381 * (LoanType == "Revolver/Line < 1 Yr.") /*= Revolver/Line < 1 Yr., 364-Day Facility, Limited Line*/
							+  17.831724 * (LoanType == "Term Loan") 			/*=Term Loan A*/							
							-   2.319456 * (PrimaryPurpose2 == "Work. cap.")
							-   4.079759 * (PrimaryPurpose2 == "Debt Repay.")
							+  12.382184 * (PrimaryPurpose2 == "Takeover")
							-  12.676116 * (PrimaryPurpose2 == "CP backup")			
							-   0.957326 * (PrimaryPurpose2 == "Acquis. line")
							+  13.702028 * (PrimaryPurpose2 == "Other")		/*=all other types not listed here and not equal to "Corp. purposes" (baseline category)*/
							+  63.805629 * (PrimaryPurpose2 == "LBO/MBO")	/*=LBO, MBO*/
							+  43.293838 * (PrimaryPurpose2 == "Recap.")							
							+  65.073865 * (PrimaryPurpose2 == "Debtor-in-poss.");
		#d cr
		*Make sure estimated upfront fee is zero or larger
		replace UFR_predict = max(0, UFR_predict) if UFR_predict != . 
		*Use upfront fee from Dealscan if available
		replace UFR_predict = UFR_w if UFR_w != .


/*Step 3: Determine total-cost-of-borrowing measure (TCB)*/
	gen TCB = UFR_predict / (Maturity_w/12) + (1-meanusage_predict)*(AFR0_w + CF0_w) + meanusage_predict * (AFR0_w + LIBOR_w) + usage30d_predict* UTF0_w + 0.005 * CAF0_w 
		