* last updated 2022-05-03 clear all capture log close set more off set maxvar 30000 ************************************************************************** ***************************START UPDATING HERE**************************** ************************************************************************** * directory where the extract is saved cd C:\user\directory * name of the extract, without the file extension local extract meps_00001 * name of the log file that will be created by this .do file local logname link_events_conditions * NOTE: before running this .do file you need to unzip your extract ************************************************************************** ****************************STOP UPDATING HERE**************************** ************************************************************************** log using `logname'.log, replace ************************************************************************** * Purpose: This is example Stata code to merge condition and event records * from an IPUMS MEPS hierarchical extract ************************************************************************** * 1) Load the extract into Stata and save as a .dta file qui do `extract'.do save `extract'_hier.dta, replace * 2) Create macro with the record types levelsof rectype, clean local(record_types) * 3) Loop through each record type only keeping variables that have at * least one non-missing value foreach rt in `record_types' { clear use if rectype=="`rt'" using `extract'_hier.dta foreach v of varlist * { qui count if !missing(`v') if r(N)==0 { drop `v' } } save `extract'_`rt'.dta, replace } * 4) Link the condition records to the condition-event link records clear use `extract'_J.dta rename mepscondidj mepscondid merge m:1 year mepscondid using `extract'_X.dta keep if _merge==3 drop _merge save `extract'_linked_xj.dta, replace * 5) Link the event records to the condition-event link records clear use `extract'_J.dta rename mepsevntidj mepsevntid merge m:1 year mepsevntid using `extract'_E.dta keep if _merge==3 drop _merge save `extract'_linked_ej.dta, replace * 6) Link the results of steps 4 and 5 clear use `extract'_linked_xj.dta merge 1:1 year mepsclnkid using `extract'_linked_ej.dta keep if _merge==3 drop _merge save `extract'_linked_xe.dta, replace * The resulting *_linked_xe.dta file will have a record for each * event-condition pair and will have filtered out all conditions that don't * link to an event and all events that don't link to a condition log close clear all