Re: st: STATA loop terminating over missing variables

 From Akshay SHanker To statalist@hsphsun2.harvard.edu Subject Re: st: STATA loop terminating over missing variables Date Thu, 04 Dec 2008 12:24:50 +1100

```Thanks for this, this seems to work. I have an extension to the question, suppose i have the following loop:
forvalues i =1/49{
forvalues j=1/13{
forvalues k=1/49{
forvalues m =1/13{
capture gen mindiff`i'_`j'__x_`k' = min(capture diff`i'_`j'__`m'_`k')
}
}
}
}

I am trying to get the min of diff for a particular i,j and k over a list of m. Now again not all diff`i'_`j'__`m'_`k' exist, so while the capture allows the loop to keep running, the min function does not work unless all the specified variables exist. Is there any way around this? I could just generate missing vars for every diff`i'_`j'__`m'_`k' but that would be about .5. million vars - not possible.

Thanks again, this is very helpful!
Akshay

On 2/12/08 1:15 AM, "Nick Cox" <n.j.cox@durham.ac.uk> wrote:

> The original question seems backwards. The purpose of the loop as
> written is to create (e.g.) mindiff1_2 from mindiff1 and mindiff_2, so
> that far from being a problem, the prior non-existence of mindiff1_2 is
> expected. Perhaps the questioner meant that some of the
> mindiff1-mindiff15 may not exist, in which case a better syntax is (as
> indicated by Maarten)
>
> forval i = 1/15 {
> forval j = 1/15 {
> capture gen mindiff`i'_`j' = mindiff`i' - mindiff`j'
> }
> }
>
> Note correction of typo ``i' to `i'.
>
> Nick
> n.j.cox@durham.ac.uk
>
> Maarten buis
>
> --- Akshay SHanker <ashanker@optusnet.com.au> wrote:
>> I am running the following simple loop which i need some help with:
>>
>> forvalues i =1/15 {
>> forvalues j=1/15{
>> gen mindiff`i'_`j' = mindiff``i' - mindiff`j'
>> }
>> }
>>
>> The problem is that not all combinations of i and j exist , so for
>> example mindiff1_2 may not exist. Everytime the loop goes over such a
>> variable, the loop terminates and thus stops running the loop over
>> all consecutive variables which may exist. How do i stop the loop
>> from terminating?
>
> see: -help capture-
>
>
> *
> *   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/
```