Announcement

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

  • Generate vars with conditions inside a loop

    Hi,

    I have a question, why the next sintaxis(with conditions) is not possible?

    foreach i of numlist 1/30{
    gen brd1=c if i>10
    gen brd2=c if i>15
    gen brd3=c if i>25
    gen brd4=c if i>30
    }
    When I run "brd1" the values are between 1/30, and I want values between 1/10.


    Thanks for your help.
    S.

  • #2
    I don't know why this syntax runs at all. I doubt that you ran exactly this.

    The outer loop

    Code:
    foreach i of numlist 1/30
    is equivalent to

    Code:
    forval i = 1/30
    and should cause the statements inside the loop to be executed 30 times. But if the loop runs once, then after the first loop brd1 brd2 brd3 brd4 will exist

    So on the second loop the instruction to generate brd1 should be fatal, as the variable already exists; and the loop would fail there.

    Alternatively supposing that

    Code:
    gen brd1 = c if i > 10
    runs, then the i in that statement is nothing at all to do with the loop index, which must be referred to as `i'. If that statement works then it is because i is the name of a variable in your dataset, or the abbreviation of a variable name if you haven't disabled variable name abbreviation. (Just possibly, i is the name of a numeric scalar if neither of those is true.)

    Otherwise put, your inner statements make no reference to the loop index. That's not illegal, but it's usually a sign that the programmer is confused or that there's a typo.

    From your final sentence, I guess that this is what you want:

    Code:
    gen brd1 = c if c <= 10
    
    gen brd2 = c if c > 10 & c <= 15
    and so on

    and if that is true no loop machinery is needed at all (and your inequalities are wrong too).


    Comment


    • #3
      Originally posted by Nick Cox View Post
      I don't know why this syntax runs at all. I doubt that you ran exactly this.

      The outer loop

      Code:
      foreach i of numlist 1/30
      is equivalent to

      Code:
      forval i = 1/30
      and should cause the statements inside the loop to be executed 30 times. But if the loop runs once, then after the first loop brd1 brd2 brd3 brd4 will exist

      So on the second loop the instruction to generate brd1 should be fatal, as the variable already exists; and the loop would fail there.

      Alternatively supposing that

      Code:
      gen brd1 = c if i > 10
      runs, then the i in that statement is nothing at all to do with the loop index, which must be referred to as `i'. If that statement works then it is because i is the name of a variable in your dataset, or the abbreviation of a variable name if you haven't disabled variable name abbreviation. (Just possibly, i is the name of a numeric scalar if neither of those is true.)

      Otherwise put, your inner statements make no reference to the loop index. That's not illegal, but it's usually a sign that the programmer is confused or that there's a typo.

      From your final sentence, I guess that this is what you want:

      Code:
      gen brd1 = c if c <= 10
      
      gen brd2 = c if c > 10 & c <= 15
      and so on

      and if that is true no loop machinery is needed at all (and your inequalities are wrong too).

      Thank You Nick,

      I gave a short example, but the db is bigger than that. That's the reason why I'm using a loop:

      levelsof c_lo1,local(levels)
      foreach m of local levels{
      foreach i of numlist 1/6{
      gen PRI_CDT_MAT_EIB_`m'_F_`i'=grado`i' if c_lo1=="`m'" & `i'<=2 & prioridad_eib1=="P1")
      }
      }
      And how you can see in the image, it generates vars until grade 6 when I want just until grade 2

      I would like run a sintaxis like this but is impossible
      levelsof c_lo1,local(levels)
      foreach m of local levels{
      foreach i of numlist 1/6{
      gen PRI_CDT_MAT_EIB_`m'_F_`i'=grado`i' if c_lo1=="`m'" & `i'<=2 & prioridad_eib1=="P1"
      gen PRI_CDT_MAT_EIB_`m'_F_`i'=grado`i' if c_lo1=="`m'" & `i'<=4 & prioridad_eib1=="P2"
      gen PRI_CDT_MAT_EIB_`m'_F_`i'=grado`i' if c_lo1=="`m'" & `i'<=6 & prioridad_eib1=="P3"
      }
      }

      Thanks in advance
      Attached Files

      Comment


      • #4
        Sorry, but I am bailing out here. It's understandable, if often frustrating for people who answer, if someone first presents a simplified version of a problem and then reveals complexities, but

        1. You're not responding to my comments in detail. I now feel I wasted my time explaining in some depth quite what was wrong or puzzling in your code, and the response is just in effect "Thank you, but the real problem is quite different".

        2. There is virtually no resemblance between this problem and the previous one except the occurrence of loops.

        3. I won't even try to work with code and data presented in this way. We spend time explaining how you're asked to present code and data examples, specifically that screenshots really are not that much help. https://www.statalist.org/forums/help#stata remains essential reading.

        Someone else may be willing to take this forward.

        EDIT: As William Lisowski points out in #4 this was in response to a post now deleted.
        Last edited by Nick Cox; 14 Mar 2019, 10:07.

        Comment


        • #5
          After looking at this topic, and what I think is an earlier statement of it yesterday at https://www.statalist.org/forums/for...-inside-a-loop, I realize that you are in need of the following advice, to which Nick refers in the third point of post #4.

          Take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

          The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

          Added in edit: by deleting your post #3, to which Nick responded, his earlier post #4 is now post #3, and his references to your (now deleted) post prior to his are now unanchored. You would better have left post #3 in place and provided an improved version in a subsequent post.
          Last edited by William Lisowski; 14 Mar 2019, 09:48.

          Comment

          Working...
          X