Statalist


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

RE: st: STATA loop terminating over missing variables


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: STATA loop terminating over missing variables
Date   Fri, 5 Dec 2008 17:03:45 -0000

My gut instinct here is that the postings may reflect struggling
mightily with a data structure that is not fit for purpose. 

Only very rarely do I find myself writing triple loops and fourfold
loops even more rarely. On the whole I'd guess this level of complexity
should be avoidable. 

As already flagged twice, I'm speculating. More information on the data
structure and what Akshay wants to do with it might well refute or
confirm the speculation. 

Nick 
n.j.cox@durham.ac.uk 

Kieran McCaul

I don't think this would work anyway, even without the problem of
missing data because, on each pass through the loop, there is only every
one variable in the -min- function.

You appear to want the minimum of up to 13 variables, is that correct?

If that's the case, you don't need the m loop.

The -egen- function -rowmin- will give you the minimum, excluding
missing values.

Try:


forvalues i =1/49{
forvalues j=1/13{
forvalues k=1/49{
capture egen mindiff`i'_`j'__x_`k' = rowmin(diff`i'_`j'__*_`k')

}
}
}

Akshay SHanker

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. 

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'. 
> 
> 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/



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