Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Fwd: Neverending loop....


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Fwd: Neverending loop....
Date   Thu, 12 Dec 2002 17:55:43 -0000

Roberto Mura

> I have been working on a do file, trying to re-rank some 60 
> variables I have in my 
> database. My problem is that i cannot make my loop a 
> "smart" loop so to 
> speak. My re-ranking do file (is probably very inefficient 
> because i am 
> relatively new to Stata) is based on pair-wise differences 
> among variables. 
> So I need Stata to keep on performing the series of 
> programs I wrote until 
> the condition that none of these 59 differences is 
> negative. I have been 
> able to create a "dumb loop" like: local i=1 while `i' <=23 
> { set more off 
> quietly zero quietly primo quietly secondo finale_bis 
> display `i' local 
> i=`i'+1 }
> 
> it takes 23 iterations until all differences are >=0
> 
> What I would need is for stata to do something like
> 
> while diff1<0 | diff2<0 | diff3<0 | diff4<0 | diff5<0 
> |diff6<0 |diff7<0 
> |diff8<0 |diff9<0 |diff10<0 | diff11<0 | diff12<0 | 
> diff13<0 | diff14<0 | 
> diff15<0 |diff16<0 |diff17<0 |diff18<0 |diff19<0 |diff20<0 
> | diff21<0 | 
> diff22<0 | diff23<0 | diff24<0 | diff25<0 |diff26<0 
> |diff27<0 |diff28<0 
> |diff29<0 |diff30<0 | diff31<0 | diff32<0 | diff33<0 | 
> diff34<0 | diff35<0 
> |diff36<0 |diff37<0 |diff38<0 |diff39<0 |diff40<0 | 
> diff41<0 | diff42<0 | 
> diff43<0 | diff44<0 | diff45<0 |diff46<0 |diff47<0 
> |diff48<0 |diff49<0 
> |diff50<0 | diff51<0 | diff52<0 | diff53<0 | diff54<0 | 
> diff55<0 |diff56<0 
> |diff57<0 |diff58<0 |diff59<0  { set more off quietly zero 
> quietly primo 
> quietly secondo finale_bis }
> 
> Now, for some reason I cannot explain myself, it works for only 9 
> iterations and then it stops. So it is not "understanding" 
> the if condition 
> properly.This statement is reinforced by the fact that if I 
> run this when 
> only, say,  3 more iterations are needed it does nothing as 
> after the first 
> nine.
> 
> The third and last way I could come up with was to 
> "encapsulate" that huge 
> if condition into a single variable......
> 
> by firm: gen elio=1 replace elio=0 if diff1<0 | diff2<0 | 
> diff3<0 | diff4<0 
> | diff5<0 |diff6<0 |diff7<0 |diff8<0 |diff9<0 |diff10<0 | 
> diff11<0 | 
> diff12<0 | diff13<0 | diff14<0 | diff15<0 |diff16<0 
> |diff17<0 |diff18<0 
> |diff19<0 |diff20<0 | diff21<0 | diff22<0 | diff23<0 | 
> diff24<0 | diff25<0 
> |diff26<0 |diff27<0 |diff28<0 |diff29<0 |diff30<0 | 
> diff31<0 | diff32<0 | 
> diff33<0 | diff34<0 | diff35<0 |diff36<0 |diff37<0 
> |diff38<0 |diff39<0 
> |diff40<0 | diff41<0 | diff42<0 | diff43<0 | diff44<0 | 
> diff45<0 |diff46<0 
> |diff47<0 |diff48<0 |diff49<0 |diff50<0 | diff51<0 | 
> diff52<0 | diff53<0 | 
> diff54<0 | diff55<0 |diff56<0 |diff57<0 |diff58<0 |diff59<0
> 
> while elio ==1  { quietly drop elio set more off quietly 
> zero quietly primo 
> quietly secondo finale_bis by firm: gen elio=1 replace 
> elio=0 if diff1<0 | 
> diff2<0 | diff3<0 | diff4<0 | diff5<0 |diff6<0 |diff7<0 
> |diff8<0 |diff9<0 
> |diff10<0 | diff11<0 | diff12<0 | diff13<0 | diff14<0 | 
> diff15<0 |diff16<0 
> |diff17<0 |diff18<0 |diff19<0 |diff20<0 | diff21<0 | 
> diff22<0 | diff23<0 | 
> diff24<0 | diff25<0 |diff26<0 |diff27<0 |diff28<0 |diff29<0 
> |diff30<0 | 
> diff31<0 | diff32<0 | diff33<0 | diff34<0 | diff35<0 
> |diff36<0 |diff37<0 
> |diff38<0 |diff39<0 |diff40<0 | diff41<0 | diff42<0 | 
> diff43<0 | diff44<0 | 
> diff45<0 |diff46<0 |diff47<0 |diff48<0 |diff49<0 |diff50<0 
> | diff51<0 | 
> diff52<0 | diff53<0 | diff54<0 | diff55<0 |diff56<0 
> |diff57<0 |diff58<0 
> |diff59<0 }
> 
> but that's where I get into a never ending loop! finale_bis 
> produces the 
> number of differences (count if.....) still negative but, 
> after reaching 
> the zero, it keeps on performing the operations.

If the diff* are variables then Stata can only 
interpret this as the equivalent of 

while diff1[1] < 0 ... { 

and it may well be that one bug lurks therein. 

The issue here is parallel to that for -if- 
documented at 
http://www.stata.com/support/faqs/lang/ifqualifier.html

Apart from that, it might help you to give us the 
whole of your problem, not part of it. 

In general terms I would recommend something 
more like 

local go = 1 

while `go' { 
	do calculations 
	check if calculations have succeeded 
	if so { 
		local go = 0 
	} 
} 

Your check should be able to include 
a loop over variables. In essence, you should 
_never_ need to type something like the compound 
condition used here. 

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

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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