Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: skipping out of and restarting a loop if an error occurs


From   "Ben Hoen" <bhoen@lbl.gov>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: skipping out of and restarting a loop if an error occurs
Date   Thu, 1 Nov 2012 12:40:30 -0400

I am trying to loop through txt files of New York counties (9 of them) and
years (2000-2012) for which some year/county combinations are missing.  For
each file I am using insheet and then, once loaded I perform some additional
commands and the append it with the previously loaded file.  

The files are named using their respective year county, e.g, "<county
name>_<year>_parcel.txt"

I have been able to use .capture noisily to skip the unsuccessful .insheet
command, but have not been able to find a way to skip the rest of the loop's
commands and start at the top.  (I thought it might involve the "_rc" stored
value and "continue" ... but was not able to figure out how to do that) 

Any help would be much appreciated.  Thanks in advance. 

Ben 

Here is some sample code:

==============================================================
clear all
tempfile newparcel combinedyr combinedco
g testvar=. //to create some data to initially store
save "`combinedco'"
save "`combinedyr'"

local year "2002 2003 2004 2005 2006 2007 2008 2009 2010 2011"
local county "allegany clinton erie franklin madison oneida steuben wyoming
yates"

foreach c in `county' {
	foreach y in `year' {
	capture noisily insheet using "B:/nydata/`c'_`y'_parcel.txt", tab
names clear
	save "`newparcel'", replace
	use "`combinedyr'"
	append using `newparcel'
	save "`combinedyr'", replace
	}

duplicates drop, force
save "`combinedyr'",replace
use "`combinedco'"
append using "`combinedyr'"
save "`combinedco'", replace
drop _all
g testvar=. //to create some data to initially store
save "`combinedyr'", replace
}


use "`combinedco'"
drop testvar
save allparcels.dta, replace

===============================================================
Ben Hoen
Principal Research Associate
Lawrence Berkeley National Laboratory
Office: 845-758-1896
Cell: 718-812-7589
bhoen@lbl.gov
http://emp.lbl.gov/staff/ben-hoen

Visit our publications at: 
http://emp.lbl.gov/publications





*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index