Announcement

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

  • Foreach and while: opening interval until condition is met.

    Hi everyone!

    This is my first post here so I hope I am following the correct protocol. I am currently working with a database and I have run into a small problem. I will try to explain this in detail. pensy_ss is a dummy that is set as 1 if PY100G (pension value) falls within the specified interval

    What I need to do is the following:

    I have a database where each case (or line) represents a certain amount of individuals with similar characteristics according to the weight assigned to them (RB050). I have received some new data that I need to incorporate into my database. This tells me the number (numpensy2) of beneficiaries of social security pension according to age and gives me an average pension value (pensy2). Now I have succeeded in appending these new variables to my database but since numpensy2 is a real number I have to work a little magic to make this work.

    So what I want to do is to create a loop that for each age it runs while numpensy2 is higher than the sum of the individuals that have an average pension (pensy_ss: dummy variable) that falls within an interval, where the interval increases by 1 each way every loop (sounds a little confusing but I think the code can help you understand where I am trying to get at).

    foreach x in age 45/80 {
    while (pensycounter < numpensy2) {
    replace pensy_ss=1 if (PY100G>=pensy3 & PY100G<pensy4 & age==`x'
    local i = `i' + 1
    replace pensy3=pensy2 - `i' if age==`x' // expands my interval //
    replace pensy4=pensy2 + `i' if age==`x'
    replace counter = (pensy_ss*RB050) if age==`x'
    replace pensycounter = sum (counter) if age==`x'
    if (pensycounter >= numpensy2 ) continue, break
    }
    }

    Here is a description of the code step by step:
    1 - Create age loop from 45 to 80
    2 - Condition to keep going until false: while pensycounter is smaller than numpensy2 keep running the following code.
    3 - If PY100G (pension value) falls within the specified interval set pensy_ss as 1
    4 - Increase my local by 1 every run.
    5 - Expand my interval by `x' each way
    6 - Check the sum of the REAL number of individuals represented by each pensy_ss at a given age.
    7 - If condition is met get out of loop and go to next age.

    It's a little confusing but I hope you can understand it.

    When I run the loop literally nothing happens. No error, nothing....but also doesn't do anything else. Anyone has any experience with this type of work that they can share with me? It would be most appreciated.

    Best,
    Luis Manso
    Last edited by Luis Manso; 19 Oct 2015, 09:08. Reason: Had to edit cause it was confusing with those comments

  • #2
    Here's your code, reformatted (and edited slightly). Please see http://www.statalist.org/forums/help#stata on using CODE delimiters.

    Code:
    foreach x in age 45/80 { // loop age
          while (pensycounter < numpensy2) {
          // condition to keep going until false
                replace pensy_ss=1 if (PY100G>=pensy3 & PY100G<pensy4 & age==`x'
                // pensy_ss is a dummy that is set as 1 if PY100G (pension value) falls within the specified interval /
                local i = `i' + 1
                // increases my local in order to expand my interval
                replace pensy3=pensy2 - `i' if age==`x'
                replace pensy4=pensy2 + `i' if age==`x'
                replace counter = (pensy_ss*RB050) if age==`x'
                // applies the weight so I know exactly how many people this case represents
                replace pensycounter = sum (counter) if age==`x'
                // gives me the sum of my variable so see if condition is already met  
                if (pensycounter >= numpensy2 ) continue, break
                // stop while if condition is met and go to the next value in age
         }
    }
    The main problem here is that you are assuming that somehow there is a loop over observations that is automatic because you start a loop. Not so. (The problem may arise because you are extrapolating from what happens in other software.)

    We will get to that in a while.

    In fact your opening loop, although utterly legal, is a long way from what you want.

    Code:
    foreach x in age 45/80
    is a loop over precisely two cases, age and 45/80 which will both be interpreted as text. First time round the loop the local macro would lead to the substitution

    Code:
    if age == age
    which is legal but vacuous.

    Second time around the loop there would be the substitution

    Code:
    if age == 45/80
    which is just a fraction, which would be evaluated (as 0.5625): once again legal, but not at all what you want.


    In fact a loop over integers 45 to 80 would just be

    Code:
    forval x = 45/80
    The main problem, however, is that

    Code:
    while (pensycounter < numpensy2)
    will never be interpreted as anything but the corresponding values in the first observation, i.e.

    Code:
    while (pensycounter[1] < numpensy2[1])
    which is subtle but tacitly documented. The same problem is explicit in (e.g.) http://www.stata.com/support/faqs/pr...ier/index.html and would bite with your statement using if in similar fashion.

    This almost certainly explains why nothing happened. The code was mostly legal but the test failed and your illegal code was not tickled.

    I note further a clash between your comments and your code. The function sum()in Stata gives running or cumulative sum, which does not seem to be what you want, which is possibly a sum over similar observations. (Note that the extra space is illegal there.)

    All that said, I have some experience with Stata code, but I find more than a few sentences of verbal explanation too hard to hold in my head if I am unfamiliar with the subject matter, as is true here. I have explained various misunderstandings but I haven't provided alternative code. If no-one else does that, please go back to http://www.statalist.org/forums/help#stata for advice and give us a worked example.

    One good example is worth any amount of exegesis here.
    Last edited by Nick Cox; 19 Oct 2015, 10:15.

    Comment

    Working...
    X