Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: continuing a loop from after it encounters an error


From   Nick Mosely <[email protected]>
To   [email protected]
Subject   Re: st: continuing a loop from after it encounters an error
Date   Thu, 3 Feb 2011 11:52:59 -0800

Thanks, -noisily capture- is what I was looking for.

It's complicated, I have an access database which has descriptions of
my variables. I'm trying to use a spreadsheet made in access to create
value labels and label variables. To do this, I am creating value
labels and variable labels in one instance of stata and then
transferring them to the master database. Some of the variables in
access are not in the master database.

On Thu, Feb 3, 2011 at 11:45 AM, Nick Cox <[email protected]> wrote:
> My answer is the same, extended.
>
> 1. Use -capture- if you must do this.
>
> 2. I can't see why you would ever need to do anything like this. Just loop over a varlist. The globals are just middlemen here who can be cut.
>
> Nick
> [email protected]
>
> Nick Mosely
>
> OK, youre right, my example was weak. Here is a more accurate look at
> what I am trying to accomplish:
>
> set obs 1
> g object1 = 1
> g object3 = 3
> global p1 object1
> global p2 object2
> global p3 object3
> foreach i of numlist 1/3 {
>        foreach j of global p`i' {
>                tabulate `j'
>        }
> }
>
>    object1 |      Freq.     Percent        Cum.
> ------------+-----------------------------------
>          1 |          1      100.00      100.00
> ------------+-----------------------------------
>      Total |          1      100.00
> variable object2 not found
> r(111);
>
> I hope this makes it more clear. Thanks!
>
> On Thu, Feb 3, 2011 at 10:54 AM, Eric Booth <[email protected]> wrote:
>> <>
>>
>> I'm still not clear on what the "objects" are.  You mention that are "commands" and then you -display- them. Also, when I run your code, I don't get the r(199) error about it being an unrecognized command -- so there's something important missing from your example.
>>
>> Take a look at -help confirm- and decide which objects you are trying to confirm exist.
>> If these are variables you can:
>>
>> **************************!
>> clear
>>  set obs 10
>>  g object1 = 1
>>  g object3 = 3
>>
>> ****
>> foreach i in object1 object2 object3 {
>> cap confirm variable `i'
>>   if !_rc {
>>        display `i'
>>        }
>> }
>> **************************!
>>
>> If these are commands, you can also use -which- and -capture- to check to see if they are installed:
>> **************************!
>> clear
>> foreach i in  describe object1 object3 list  {
>>        cap which `i'
>>        if _rc continue
>> }
>> **************************!
>>
>> On Feb 3, 2011, at 12:41 PM, Nick Mosely wrote:
>
>>> I've built a loop that encounters an error periodically due to the
>>> fact that stata can't find one of the objects specified in the list. I
>>> don't mind the error, but I would like stata to skip objects in list
>>> that encounter the error and continue to the next iteration of the
>>> loop, rather than breaking the loop. I've tried the continue command,
>>> but am not sure that is what it's meant for.
>>>
>>> Here is a simplified version of the code. In this example, assume that
>>> objects1 and object3 are valid commands but object2 is not:
>>>
>>> foreach i in object1 object2 object3 {
>>> display `i'
>>> continue
>>> }
>>>> object1
>>>> unrecognized command:  object2
>>>> r(199);
>>>
>>> What can I do get the code to continue running through object3?
>>>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/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/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index