Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Obs. nos. out of range error for looping

    Dear all,
    I am writing a simple loop by using the "local" function. The following is my code:
    gen pos_a =.
    gen pos_b =.


    local a = 1
    while `a'<=32{
    local b=1
    while `b'<=32 {

    local z=`a'*100 + `b'


    quietly ineqdec0 mv190 if mv131==77|mv131==88|mv131==99|mv131==17|mv131== `a'|mv131==`b'
    quietly replace gini_wi_group =r(gini) in `z'
    quietly replace pos_a=`a' in `z'
    quietly replace pos_b=`b' in `z'


    local b = `b'+1
    }
    local a = `a'+1
    }

    However, an error term of "Obs. nos. out of range" always shows up. When I change the number of 32 to 15 (for instance), it works. The sample size is about 2500. I am wondering why. Any help would be very much appreciated. Thanks a lot.



    Best,
    Jim


  • #2
    You create a local z that is 100*a + b. So in your setup the maximum value of `z' is 3232. You replaced various variables at the observation number `z'. If your sample size is 2500, then the observation number 3232 won't be present in your data, and you get the error you reported.

    I have solved that by creating a new macro i that increments at each step. The increment happens in the line quietly replace pos_b = `b' in `i++', which means do the replace in observation `i' and than add 1 to the local `i'. This way the maximum value of `i' is 1/2*(32*32 + 32) = 528, which is well below 2500, so it won't cause that error message.

    I have improved the code in two of other ways: I replaced the while loop with the more modern forvalues loop. In your setup your results will be the same when a=1 and b=2 or a=2 and b=1, so I removed these redundancies by starting the internal loop at `a'.

    Code:
    gen pos_a =.
    gen pos_b =.
    
    local i = 1
    forvalues a = 1/32{
        forvalues b = `a'/32 {
            quietly ineqdec0 mv190 if inlist(mv131, 77, 88, 99, 17, `a', `b')
            quietly replace gini_wi_group =r(gini) in `i'
            quietly replace pos_a=`a' in `i'
            quietly replace pos_b=`b' in `i++'
        }
    }
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment

    Working...
    X