cap program drop MedCosts
program define MedCosts

		*************************
		* Medical spending and medical utilization
		*************************
    syntax [varlist] [if] [in], cyr(integer) medicare_elig_age(integer) cost_noise(integer)
    marksample touse


    noi {
			local MAX_MEDICARE_AGE = 75

    	foreach v in totmd oopmd caremd caidmd doctim hsptim hspnit inpatient_ever {
    			cap drop `v'
    	}

	cap drop medicare_elig
	gen medicare_elig = age >= min(`medicare_elig_age',`MAX_MEDICARE_AGE') | (diclaim & ldiclaim)


			* tempvar inpatient_ever


			cap drop age75l
			cap drop age75p

			qui gen age75l = min(age, 75) if age < .
			qui gen age75p = max(0, age-75) if age < .

	***
	*** IF not medicare_elig, use model estimates from MEPS
	***

  * Predict inpatient_ever using MEPS if age < medicare elig age
	est restore rinpatient_ever
	predict p	if !medicare_elig & `touse', xb
	* Roll to get the result of inpatient ever
	gen inpatient_ever 	= x_inpatient_ever`cyr' + p >= 0	if !medicare_elig & `touse'
	drop p
	* Predict doctor times using MEPS if age < medicare elig age
		est restore rdoctim
		predict doctim    			if !medicare_elig  & `touse'


		foreach v in care tot caid oop {
			* First, do no inpatient ever
			est restore r`v'_no_inpatient
			predict  `v'md     		if !medicare_elig  & !inpatient_ever & `touse'

			* add jitter
			replace `v'md = `v'md + rnormal(0, e(rmse)) if `cost_noise' == 1 & !medicare_elig  & !inpatient_ever & `touse'

			* Next, inpatient ever
			est restore r`v'_inpatient
			predict p     				if !medicare_elig  & inpatient_ever & `touse'
			replace `v'md = p     if !medicare_elig  & inpatient_ever & `touse'
			drop p

			* add jitter
			replace `v'md = `v'md + rnormal(0, e(rmse)) if `cost_noise' == 1 & !medicare_elig  & inpatient_ever & `touse'
		}


		foreach v in hsptim hspnit {
			* Set number of times as inpatient and number of nights in the hospital to zero if never an inpatient
			gen `v' = 0 if !medicare_elig  & !inpatient_ever & `touse'

			* Otherwise predict using MEPS
			est restore r`v'
			predict p      if !medicare_elig  & inpatient_ever & `touse'
			replace `v' =  p      if !medicare_elig  & inpatient_ever & `touse'
			drop p

	}

	replace caremd = 0

		***
		***IF medicare_elig , use model estimates from MCBS
		***

		* Redefine age as 12/31

		ren age75l age75l_old
		ren age75p age75p_old

		qui gen age75l = min(age_1231, 75) if age_1231 < .
		qui gen age75p = max(0, age_1231-75) if age_1231 < .

		foreach v in iadl1 adl12 adl3 {
			cap drop `v'_old
			qui gen `v'_old = `v'
			replace `v' = 0 if nhmliv == 1
		}

		* Create interactions

 	gen diclaim_died = diclaim * died
 	gen diclaim_nhmliv = diclaim * nhmliv

		* Predict inpatient_ever using MCBS if age >= medicare elig age
		est restore minpatient_ever
	  predict p	if medicare_elig  & `touse', xb
	* Roll to get the result of inpatient ever
	  replace inpatient_ever 	= x_inpatient_ever`cyr' + p >= 0	if medicare_elig  & `touse'
		drop p



  * Predict doctor times using MCBS if age >= medicare elig age
  	est restore mdoctim
		predict p      					if medicare_elig  & `touse'
		replace doctim = p			if medicare_elig  & `touse'
		drop p



		foreach v in care tot caid oop {
			* First, do no inpatient ever
			est restore m`v'_no_inpatient
			predict  p		    		if medicare_elig & !inpatient_ever & `touse'
			replace `v'md = p     if medicare_elig & !inpatient_ever & `touse'
			drop p

			* add jitter
			replace `v'md = `v'md + rnormal(0, e(rmse)) if `cost_noise' == 1 & medicare_elig & !inpatient_ever & `touse'


			* Next, inpatient ever
			est restore m`v'_inpatient
			predict p     				if medicare_elig & inpatient_ever & `touse'
			replace `v'md = p     if medicare_elig & inpatient_ever & `touse'
			drop p

			* add jitter
			replace `v'md = `v'md + rnormal(0, e(rmse)) if `cost_noise' == 1 & medicare_elig & inpatient_ever & `touse'
		}


		foreach v in hsptim hspnit {
			* Set number of times as inpatient and number of nights in the hospital to zero if never an inpatient
			replace `v' = 0 				 if medicare_elig & !inpatient_ever & `touse'

			* Otherwise predict MCBS
			est restore m`v'
			predict p      				if medicare_elig & inpatient_ever  & `touse'
			replace `v' = p				if medicare_elig & inpatient_ever  & `touse'
			drop p
	}


  *** Recover original values of functional status
		foreach v in iadl1 adl12 adl3 {
			replace `v' = `v'_old
			drop `v'_old
		}

	qui replace age75l = age75l_old
	qui replace age75p = age75p_old

	drop age75l_old age75p_old

	drop  diclaim_died  diclaim_nhmliv

	*************
	* Utility score: EQ_5D
	*************
	cap drop adl1p
	cap drop eq5d
	gen adl1p = adl12 == 1 | adl3 == 1
	cap drop nrshom
	gen nrshom = nhmliv
	matrix score eq5d = coef_eq5d if `touse'
	replace eq5d = eq5d - 0.10 if nrshom == 1


	}


end