set mem 500M * Determine the last location that an individual was resident at and the last time that location was visited. clear * Calculate the last date that a location was visited cd \chililabdata\data use observation sort locationid egen lastdate=max(obs_entry_date), by(locationid) format lastdate %d drop if !(obs_entry_date==lastdate) keep locationid obs_entry_date sort locationid save lastvisit,replace clear * Determine the last location that an individual was resident use individres sor individid egen lastsdate=max(res_sdate), by(individid) format lastsdate %d drop if !(res_sdate==lastsdate) keep individid locationid res_edate res_eeventtype sort individid save lastlocation,replace * Combine last location an individual was resident and the last visit date of the location sort locationid merge locationid using lastvisit, nokeep keep(obs_entry_date) rename obs_entry_date lastvisit drop _merge sort individid save lastobservation, replace