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 at the end of May, and its replacement, statalist.org is already up and running.


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

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


From   Ryan Kessler <ryan.kessler.stata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: skipping out of and restarting a loop if an error occurs
Date   Thu, 1 Nov 2012 12:51:18 -0400

Hi Ben,

To skip the rest of the loop's commands you can nest them within an if
_rc == 0 statement. For example:

foreach c in `county' {
      forval y=2002/2011 {
           capture noisily insheet using
"B:/nydata/`c'_`y'_parcel.txt", tab names clear
           if _rc == 0 {
                // Additional commands
           }
      }
}

Best,

Ryan

> 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
>         }

On Thu, Nov 1, 2012 at 12:40 PM, Ben Hoen <bhoen@lbl.gov> wrote:
> 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/
*
*   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