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]

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


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

That worked perfect guys.  

Thank you.

Ben Hoen
LBNL
Office: 845-758-1896
Cell: 718-812-7589


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: Thursday, November 01, 2012 1:14 PM
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: skipping out of and restarting a loop if an error occurs

I agree with Ryan's advice. You could write

if _rc != 0 continue

if you so wished.

Nick

On Thu, Nov 1, 2012 at 4:51 PM, Ryan Kessler
<ryan.kessler.stata@gmail.com> wrote:
> 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/
*
*   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