cap program drop EconGen
program define EconGen

		syntax [varlist] [if] [in], cyr(integer)
		marksample touse

		qui {

		******************************************************
		* Re-transform economic outcomes
		* Record timing of economic events
		* Generate spousal economic outcomes
		* Update AIME and tenure
		* Calculate DB pension benefits
		******************************************************

	*================================
	* Update year of claiming SS, claiming DB
	*================================
	*replace rssclyr = min(2004,rbyr + age) if ssclaim == 1 & rssclyr == 2100
	*replace rdbclyr = min(2004,rbyr + age) if dbclaim == 1 & rdbclyr == 2100

	replace rssclyr = year if ssclaim == 1 & rssclyr == 2100
	replace rdbclyr = year if dbclaim == 1 & rdbclyr == 2100


	replace ssage = age if ssclaim == 1 & ssage == 1000
	replace dbage = age if dbclaim == 1 & dbage == 1000

	* Spousal
	global rplist "ssage dbage"
	global splist "sssage sdbage"

	local vnum = 1
	foreach v in $rplist {
		local spvar = word("$splist",`vnum')
		sort entry hhid hhidpn, stable
		if `cyr' == $startyr {
			cap drop `spvar'
			by entry hhid: gen `spvar' = `v'[2] if _n == 1 & married == 1 &  _N == 2
			by entry hhid: replace `spvar' = `v'[1] if _n == 2 & married == 1 & _N == 2
		}
		else {
			by entry hhid: replace `spvar' = `v'[2] if _n == 1 & married == 1 & _N == 2 & `spvar' == 1000
			by entry hhid: replace `spvar' = `v'[1] if _n == 2 & married == 1 & _N == 2	& `spvar' == 1000
		}

		local vnum = `vnum' + 1
		replace `spvar' = 1000 if `spvar' == .
	}

	********************************
	* Rescale earnings and HH wealth - CHANGE THIS
	********************************
	cap drop ry_earn
	gen ry_earn = iearnx*1000

	cap drop hhwealth
	gen hhwealth = hatotax * 1000 if `touse'

	********************************
	* Rescale other resp inc-/unemp/gov trans/HH other
	********************************
	* cap drop otherinc
	* gen otherinc = exp(100 * logiothrx) * 1000 if `touse'
	* replace otherinc = 0 if missing(otherinc) & `touse'

	********************************
	* HH income: Other, asset income and total income
	* Not account for SS benefits, DI benefis, DB pension benefits
	* Put capital income and other income as zero, hh income only include earnings+OASI+DI+SSI+DB pension
	********************************
	* Other income (other than earnings, OASDI, SSI, DB pension income, asset return)
	* cap drop hhothinc
	* by entry hhid: egen hhothinc = total(otherinc) if `touse'

	* Exclude capital income and removing the double-counting of other income
	* replace hhothinc = hhothinc - 2 * hicap - hiothr if `touse'

	cap drop hhearn
	bys entry hhid:  egen hhearn = total(ry_earn) if `touse'

	* Total HH income
	cap drop hhothinc
	cap drop hicap
	gen hhothinc = 0
	gen hicap = 0
	cap drop hhttlinc
	gen hhttlinc = hhothinc + hicap + hhearn

		*================================
		*Update tenure
		*================================
		replace db_tenure = db_tenure + 2 if work == 1 & fanydb == 1

		*================================
		*Update AIME and quarters worked
		*================================
		if `cyr' > $startyr {

		  * Update AIME
		  cap drop cpi_1992
		  egen cpi_1992 = cpi(1992)
		  cap drop cpi_2004
		  egen cpi_2004 = cpi(2004)

		  cap drop styr
		  gen styr = $startyr

		  * Update AIME, all in 1992 dollars
		  cap drop age_1yr age_2yr
		  qui gen age_1yr = age - 1
		  qui gen age_2yr = age - 2

		  cap drop faime
			UpdateAIME raime ry_earn age_2yr male styr, gen(faime)
			qui replace raime = faime if work == 1 & ssclaim == 0

		  cap drop faime
			* Update quarters worked
			qui replace rq = rq + 8 if work == 1
		}


		*================================
		* Spousal information, for calculating government expenditures and benefits
		*================================
		global rplist "raime rq ry_earn rssclyr rbyr r1 rdthyr"
		global splist "saime sq sy_earn sssclyr sbyr s1 sdthyr"

		* Initialize Spousal vars if not initialized yet
		foreach v in $splist {
			cap gen `v' = .
		}


		cap drop r1
		gen r1 = 1 - died
		local vnum = 1

		* If starting year, obtain spousal info
		if `cyr' == $startyr {
			foreach v in $rplist {
				local spvar = word("$splist",`vnum')
				sort entry hhid hhidpn, stable
				by entry hhid: replace `spvar' = `v'[2] if _n == 1 & married == 1 & _N == 2
				by entry hhid: replace `spvar' = `v'[1] if _n == 2 & married == 1 & _N == 2
				local vnum = `vnum' + 1
			}
			replace sssclyr = 2100 if married != 1
			replace sdthyr = 2100 if married != 1
			replace saime = 0 if married != 1
			replace sq = 0 if married != 1
			replace sy_earn = 0 if married != 1
			replace sbyr = 2100 if married != 1
			replace s1 = 0 if married != 1
		}

		* If after starting year, update all if still married or newly widowed
		else if `cyr' > $startyr {
			foreach v in $rplist {
				local spvar = word("$splist",`vnum')
				sort entry hhid hhidpn, stable
				by entry hhid: replace `spvar' = `v'[2] if _n == 1 & _N == 2
				by entry hhid: replace `spvar' = `v'[1] if _n == 2 & _N == 2
				local vnum = `vnum' + 1
			}

			* For non-married and incoming cohorts
				replace sssclyr = 2100 if married != 1 & entry == `cyr'
				replace sdthyr = 2100 if married != 1 & entry == `cyr'
				replace saime = 0 if married != 1 & entry == `cyr'
				replace sq = 0 if married != 1 & entry == `cyr'
				replace sy_earn = 0 if married != 1 & entry == `cyr'
				replace sbyr = 2100 if married != 1 & entry == `cyr'
				replace s1 = 0 if married != 1 & entry == `cyr'

			* FOR WIDOWED AFTER AGE 51
			* Keep the earnings if the spouse died THIS year. Only set to zero after the spouse has been dead more than a year step
				* replace sssclyr = rssclyr if fmarried == 1 & married == 0 & single == 0 & s1 == 0
				* replace sbyr  = rbyr if fmarried == 1 & married == 0 & single == 0 & s1 == 0
				replace sy_earn = 0 if lmarried == 0 & lwidowed == 1  & s1 == 0
				replace sy_earn = 0 if missing(sy_earn)
		}

		*================================
		* DB pension benefits
		*================================
		* Tenure (need to construct the variable first)

		cap drop dbpen
		DbBenefit ry_earn db_tenure educ age era nra year male, gen(dbpen)

	}
end