Announcement

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

  • forval loop overwriting its previous iterations

    I’m having a rather silly problem with a simple forval loop, such that the last cycle of the loop seems to be populating all previous local macros created therein. I am simply trying to record a series of r-squared values in a matrix over subsets of observations defined by a decile variable (here called var3). My code is:
    forval i = 1/10 {
    qui xtreg var1 var2 if var3==`i'
    local rsquare`i' e(r2_o)
    }

    matrix define r2wide = (1,2,3,4,5,6,7,8,9,10\`rsquare1',`rsquare2',`rsqua re3',`rsquare4',`rsquare5',`rsquare6',`rsquare7',` rsquare8',`rsquare9',`rsquare10')
    matrix r2 = r2wide'
    matrix list r2
    The resulting matrix is something like:

    r2 [10,2]
    r1 r2
    c1 1 0.245521
    c2 2 0.245521
    c3 3 0.245521
    c4 4 0.245521
    c5 5 0.245521
    c6 6 0.245521
    c7 7 0.245521
    c8 8 0.245521
    c9 9 0.245521
    c10 10 0.245521
    where, as you can see, all r-squared values have been replaced with the most recent value generated – even though if I insert a command line into the loop to display its output in media res (di rsquare`i' inserted just below local rsquare`i' e(r2_o)), I obtain a series of distinct results (but still the same homogenous matrix at the end).

    If I run a similar forval loop without the regression and r-square extraction, there’s no problem:
    forval i = 1/10 {
    local rsquare`i' `i'
    }

    matrix define r2wide = (1,2,3,4,5,6,7,8,9,10 \ `rsquare1',`rsquare2',`rsquare3',`rsquare4',`rsqua re5',`rsquare6',`rsquare7',` rsquare8',`r2QtauS9',`rsquare10')
    matrix r2 = r2wide'
    matrix list r2
    yields:

    r2 [10,2]
    r1 r2
    c1 1 1
    c2 2 2
    c3 3 3
    c4 4 4
    c5 5 5
    c6 6 6
    c7 7 7
    c8 8 8
    c9 9 9
    c10 10 10
    Many thanks.
    Version: Stata 13.1 (up to date)
    Last edited by Topher McDougal; 08 Mar 2016, 15:50.

  • #2
    The problem is in the first loop where you write

    Code:
    local rsquare`i' e(r2_o)
    That command does not tell Stata to find the value of e(r2_o) and put it in local macro rsquare`i'. It tells Stata to put the string "e(r2_o)" in rsquare`i'. So each of the rsquare macaros contains the string "e(r2_o)". Then when you go to your -matrix define- command, Stata decides that you want to evaluate e(r2_o)--because how else could it make sense of that: you can't put strings in a Stata matrix. But at that point the only e(r2_o) hanging around is the final one. The solution is simple:

    Code:
    local rsquare`i' = e(r2_o)

    Comment


    • #3
      Of course! Thanks for the quick reply.

      Comment


      • #4
        Hi! I am writing the following line of command to generate dummy's:
        webuse nlswork
        forval i= 68/88 {
        gen dummy`i'= 0
        }

        forval i= 68/88 {
        replace dummy`i'= 1 if union`i'>0
        }

        But since in my data set i dont have year 74 76 79 81 84 86. I could not run the forval loop, because by writing 68/88 stata is saying that 74 is missing.
        Can anyone suggest what i can do if i don't have consecutive values in a loop?

        Comment


        • #5
          use the -foreach- command; see that last of the syntaxes at
          Code:
          h foreach
          note that if you don't understand what Stata means by a "numlist" (which the foreach command calls for), you should see
          Code:
          h numlist
          Last edited by Rich Goldstein; 20 Feb 2023, 13:54.

          Comment

          Working...
          X