Announcement

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

  • Double index foreach loop

    Apologies if this is very rudimentary but what my Googling could uncover I didn't really understand.

    Here's a semi-minimal example of what I'm trying to do with two indices:

    foreach (i j) in (first 0) (second 1) {
    gen `i'=1 if prov==`j'&!missing(age)
    recode `i' .=0
    gen `i'm=1 if `i'==1&M==1
    recode `i'm .=0
    gen `i'f=1 if `i'==1&F==1
    recode `i'f .=0
    }

    I'd like i to be first and j to be 0 for one iteration, for the second iteration, I'd like i to be second while j is 1.

  • #2
    You can do it this way:

    Code:
    local variables first second
    local values 0 1
    
    forvalues n = 1/2 {
        local variable: word `n' of variables
        local value: word `n' of values
        gen `variable' = (prov == `value' & !missing(age))
        gen `variable'm = (`variable' === 1 & M == 1)
        gen `variable'f = (`variable' == 1 & F == 1)
    }
    There are other ways to do parallel looping, but this is, I think the simplest.

    Note also that in general the construction
    Code:
    gen whatever = 1 if some_condition
    recode whatever (. = 0)
    is more parsimoniously coded as
    Code:
    gen whatever = (some_condition) // OUTER PARENTHESES OPTIONAL, USED ONLY FOR CLARITY
    Last edited by Clyde Schechter; 08 Sep 2016, 17:07.

    Comment


    • #3
      That's perfect and thanks for the last little bit of information as well! I had it typed out a very inefficient way but since I'm trying to add the code of my thesis to the appendix, parsimony is very valuable.

      Comment


      • #4
        Ran into an "invalid syntax" error.

        Here's my actual code , fiddled around a bit but can't quite see what seems to be the problem.

        local variables NL NS NB PEI QC ON MB SK AB BC YNWNT
        local values 0 1 2 3 4 5 6 7 8 9 10

        forvalues n = 0/10 {
        local variable: word `n' of variables
        local value: word `n' of values
        gen `variable' = (prov == `value' & !missing(age))
        gen `variable'm = (`variable' == 1 & M == 1)
        gen `variable'f = (`variable' == 1 & F == 1)
        }

        Comment


        • #5
          The problem is that in the forvalues loop, n is not a proxy for the values 0 through 10, it is a counter of "words" in the variables and values lists, so it must run from 1 through 11. So on your first time through the loop, with n = 0, you are trying to access word 0 of each list, and there is no such thing. Just change the top of that loop to -forvalues n = 1/11 {- and it'll be fine.

          Thanks for posting your exact code so we could see what was going on.

          Comment


          • #6
            Sorry for this problem being a little tedious, I really appreciate the help you're giving me.

            I tried running

            Code:
            local variables NL NS NB PEI QC ON MB SK AB BC YNWNT
            local values 0 1 2 3 4 5 6 7 8 9 10
            
            forvalues n = 1/11 {
            local variable: word `n' of variables
            local value: word `n' of values
            gen `variable' = (prov == `value' & !missing(age))
            gen `variable'm = (`variable' == 1 & M == 1)
            gen `variable'f = (`variable' == 1 & F == 1)
            }
            Code:
            values not found
            r(111);
            Additionally, I tried my original syntax error code but with 0 and NL removed and the index still going to 10 and the same error occurred.

            Comment


            • #7
              My error this time. The first two lines inside the loop should be:
              Code:
              local variables: word `n' of `variables'
              local value: word `n' of `values'
              Note the left quote (`) before and the right quote (') after variables and values, respectively.

              Please re-read my explanation in #5. Using -forvalues n = 0/10- will get you nowhere slowly.

              Comment


              • #8
                Applied the method to elsewhere and it cut the code by more than half! Thanks so much.
                Last edited by Rufus Randolf; 08 Sep 2016, 19:28.

                Comment

                Working...
                X