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

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


From   Roberto Mura <roberto.mura3@tin.it>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: Fwd: Neverending loop....
Date   Fri, 13 Dec 2002 10:49:34 +0100

Hi Nick and thank you so much for your kind reply.
Let me try to be a little more precise. I apologise but, being relatively new to statalist i was not sure what to write.
I have some ownership data of italian listed firms; it is very detailed. I have 60 ownership variables (own1-own60)containing shares, plus 60 regarding the types of shareholders (type1-type60)....and others that are now irrelevant.
My problem is to re-rank all of these since it is very helpful to have an idea about the largest shareholders, second largest and so on.
In order to do that i have created a series of programs contained in 4 do files. Basically they create differences between
ownership variables, create copies of them and then swap the pairs if it is necessary.
This needs to be done in two steps.
First all the differences
ie: ** differences
by firm : gen diff1 = own1 - own2
by firm : gen diff2 = own2 - own3
by firm : gen diff3 = own3 - own4
.....
** generate doubles sh
by firm : gen sh1 = own1
by firm : gen str20 newtype1 = type1
by firm : gen sh2 = own2
by firm : gen str20 newtype2 = type2
by firm : gen sh3 = own3
by firm : gen str20 newtype3 = type3
** swap pairs
by firm : replace sh1 = own2 if diff1 < 0
by firm : replace sh2 = own1 if diff1 < 0
by firm : replace newtype1 = type2 if diff1 < 0
by firm : replace newtype2 = type1 if diff1 < 0

by firm : replace sh3 = own4 if diff3 < 0
by firm : replace sh4 = own3 if diff3 < 0
by firm : replace newtype3 = type4 if diff3 < 0
by firm : replace newtype4 = type3 if diff3 < 0
...... and so on
then i need to redo all this (ie drop old variables, gen doubles of the new ones, gen new differences and swap) for the "even" ...by which i mean using diff2,diff4,diff6 and so on.

so i need stata to stop doing all this when none of these differences is negative.
That is where I get stuck, I don't know how to give it this "keep on doing all the programs as long as any diff is <0" type of thing.
In temr sof your suggestion, the thing I cannot do is rightly "check if calculations have succeeded "
I have read the programming manual, the help, the web, and also some statanotes i found on the web (like exeter summer school, lse and so on..) but i could never resolve my issue.
I hope I explained myself a little better
Anyway thank you very much again for your kind attention and patience
Best of all
Have a good day
R.



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


At 17:55 12/12/02 +0000, you wrote:
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.


*
*   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/
********************************************************************
Roberto Mura

address in the UK
22, Turners Croft
Heslington
York
YO105EL
tel: 0044 (0)1904 412 869

indirizzo in Italia
via Capo Sandalo 4
09042 Monserrato, Cagliari

work related email: rmura@unica.it
roberto.mura@crenos.it
rm143@york.ac.uk

personal email: fellowsh@tiscali.it
fellowsh@libero.it
roberto.mura3@tin.it
********************************************************************
*
* 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