cap program drop ObesityReduction_intervene
program define ObesityReduction_intervene
	syntax [varlist] [if] [in], cyr(integer)

* Obesity Reduction Intervention
* Alters the obesity transition probability by "aobese"

		marksample touse, zeroweight strok novarlist

	cap drop ltreat_now
	gen ltreat_now = treat_now
	replace ltreat_ever = treat_ever

	* Record Status before intervention
	cap drop old_obese
	gen old_obese = obese

	replace treat_now = (lobese == 1) & died == 0  if `touse'
	replace  treat_effective = 0  if `touse'

	replace treat_ever = 1 if treat_now == 1

	* The variable we will be working on
	local v = "wtstate"

	* Save pre-intervention probabilities
	forvalues i = 1/3{
		cap drop p`v'`i'_old
		gen p`v'`i'_old = p`v'`i'
	}
	cap drop `v'_old
	ren `v' `v'_old

	/* Determine new probabilities after adjustment */
	replace p`v'3 = p`v'3 * aobese if d_`v' & treat_now
	replace p`v'2 = (p`v'2_old /(p`v'1_old + p`v'2_old)) * (1 - p`v'3) if d_`v' & treat_now
	replace p`v'1 = (p`v'1_old /(p`v'1_old + p`v'2_old)) * (1 - p`v'3) if d_`v' & treat_now

	local numcut = 2
	local lastc = `numcut' + 1
	gen `v' = `lastc' if normal(x_`v'`cyr') >= 1 - p`v'`lastc' & d_`v'
	forvalues j = `numcut'(-1)2 {
		local k = `j'-1
		replace `v' = `j' if (normal(x_`v'`cyr') >= p`v'`k') & (normal(x_`v'`cyr') < p`v'`k' + p`v'`j') & d_`v'
	}
	replace `v' = 1 if normal(x_`v'`cyr') < p`v'1 & d_`v'

	replace `v' = `v'_old if treat_effective == 1
	replace treat_effective = 1 if `v' < `v'_old

	* Assign categorical vars
	cap drop overwt obese
	gen overwt = `v' == 2 if d_`v'
	gen obese = `v' == 3 if d_`v'


end