******************************************************* *This do file is a solution to the Linking ASEC to Basic (II) *exercise from the 2018 IPUMS-CPS Summer Data Workshop * *Written by the IPUMS-CPS team *31 May 2018 ******************************************************* cap log close log using logs/05_asec_to_basic_partII.log, t replace set more off /*********USER-DEFINED MACROS**********/ local extract_num 00202 local expected_obs 4 local simple_validation_vars sex race /*************************************/ qui{ /**************Read the Data***************/ cd data qui do cps_`extract_num'.do noisily: di "...now we have IPUMS data ready to go" noisily: list cpsidp mish age sex in 1/10 /***********Link the Things**********/ noisily: di "...drop the oversample" noisily: keep if asecoverp != 1 noisily: di "...sort, generate index, generate count variables" bysort cpsidp (mish) : gen time = _n egen count = max(time),by(cpsidp) noisily: list cpsidp mish age sex time count in 1/10 noisily: di "...keep only those who appear the expected number of times" noisily: keep if count==`expected_obs' noisily: di "...generate roll-up var instances for each 'time' a person appears local roll_up_vars mish age sex race month year ctccrd fsstatus foreach var in `roll_up_vars'{ forvalues i = 1/`expected_obs'{ noisily: di "`var', `i'" //requires that extracts be sorted by cpsid year month gen `var'_`i' = `var' if time==`i' egen `var'_`i'_max = max(`var'_`i'),by(cpsidp) replace `var'_`i' = `var'_`i'_max if `var'_`i'==. drop `var'_`i'_max } drop `var' } noisily: di "...this is what the data looks like now" noisily: list cpsidp age_1 age_2 age_3 age_4 fsstatus_1 fsstatus_3 ctccrd_2 ctccrd_4 in 1/15 noisily: di "...keep only the first appearance" noisily: keep if time == 1 /*********************VALIDATE****************************/ noisily: di "...validate the wide file qui do ../validate_wide.txt `expected_obs' noisily: di "...keep only the links that are valid based on age, sex, and race" noisily: drop if all_match != 1 noisily: count /*Question 2*/ noisily: di "...generate a flag to indicate how reciept of child tax credits changes over time" // remember to exclude ctccrd NIU (999999) records //ctc decreases gen ctc_trans = 1 if ctccrd_2 > ctccrd_4 & ctccrd_2 != 999999 & ctccrd_4 != 999999 //ctc increases replace ctc_trans = 2 if ctccrd_2 < ctccrd_4 & ctccrd_2 != 999999 & ctccrd_4 != 999999 //ctc doesn't change replace ctc_trans = 0 if ctc_trans==. & ctccrd_2 != 999999 & ctccrd_4 != 999999 //record is NIU for ctccrd replace ctc_trans = -1 if ctc_trans == . label def ctclbl -1 "niu" 1 "decreased ctc" 2 "increased ctc" 0 "no change in ctc" label val ctc_trans ctclbl noisily: tab ctc_trans, missing noisily: di "...generate a flag to indicate how food security changes over time" // remember to exclude NIU (99) and no response (98) values //fs increases gen fs_trans = 2 if fsstatus_1 < fsstatus_3 & fsstatus_1 < 98 & fsstatus_3 < 98 //fs decreases replace fs_trans = 1 if fsstatus_1 > fsstatus_3 & fsstatus_1 < 98 & fsstatus_3 < 99 //fs doesn't change replace fs_trans = 0 if fs_trans ==. & fsstatus_1 < 98 & fsstatus_3 < 98 // record is NIU or otherwise missing for food security status replace fs_trans = -1 if fs_trans == . label def fslbl -1 "niu" 1 "decreased fs" 2 "increased fs" 0 "no change in fs" label val fs_trans fslbl noisily: tab fs_trans, missing noisily: tab fs_trans ctc_trans, missing /*Question 4*/ noisily: di "...decreased food security" noisily: count if fs_trans == 1 noisily: di "...decreased food security and increased ctc" noisily: count if fs_trans == 1 & ctc_trans == 2 log close cd .. }