Hi there,
I am using Stata 18. Using the code below I am trying to use a loop to append individual datasets for each region in each year (for each outcome/season/age) into one dataset per year. However, not all years have datasets for 9 all regions. As a result, Stata doesn't save a combined dataset for that year. Because I've used 'Capture' the loop runs to the end. Is there a way to get Stata to save each year's data when there aren't datasets for every region?
Thank you in advance,
Mel
I am using Stata 18. Using the code below I am trying to use a loop to append individual datasets for each region in each year (for each outcome/season/age) into one dataset per year. However, not all years have datasets for 9 all regions. As a result, Stata doesn't save a combined dataset for that year. Because I've used 'Capture' the loop runs to the end. Is there a way to get Stata to save each year's data when there aren't datasets for every region?
Thank you in advance,
Mel
Code:
Combine the regions for each year into one dataset cd "datasets\age" foreach outcome in anx cvd dep eatdis psy rti selfharm sleep{ foreach season in Spring Autumn{ foreach year in 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019{ foreach age in 0 1{ capture{ use "`outcome'_`season'_`year'_region1_age`age'.dta", clear foreach region in region2 region3 region4 region5 region6 region7 region8 region9{ append using "`outcome'_`season'_`year'_`region'_age`age'.dta" } save "datasets\age\regions_combined/`outcome'_`season'_`year'_allregions_age`age'.dta", replace } } } } }
Comment