Announcement

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

  • Variable already Defined in a loop

    Goodmorning everyone,
    as reported by the title, when I start my code I get this "Variable already Defined" error.
    This error happens towards the final part of my code, precisely in here:

    Code:
    foreach x in ROE EBITDAVENDITE ROI TOTFATTVAR LEVERAGE DE TOTIMMVAR PFNEBITDA {
    forvalues i = 1/5 {
    gen p`x'`=2014+`i'' = `i'*(`x'`=2014+`i'' < .)
    }
    }
    
    foreach x in ROE EBITDAVENDITE ROI TOTFATTVAR LEVERAGE DE TOTIMMVAR PFNEBITDA {
    forvalues y=`=`YearN'-4'(1)`YearN' {
    gen n`x'`y' = p`x'`y'*`x'`y'Norm
    }
    }
    
    foreach x in ROE EBITDAVENDITE ROI TOTFATTVAR LEVERAGE DE TOTIMMVAR PFNEBITDA {
    forvalues y=`=`YearN'-4'(1)`YearN' {
    egen num`x'=rowtotal(n`x'`y')
    }
    }
    
    foreach x in ROE EBITDAVENDITE ROI TOTFATTVAR LEVERAGE DE TOTIMMVAR PFNEBITDA {
    forvalues y=`=`YearN'-4'(1)`YearN' {
    egen den`x'=rowtotal(p`x'`y')
    }
    }
    
    variable numROE already defined
    r(110);


    I specify that I have never used that name before, so the variable was not created before the loop.
    I can't fix this error.
    Thank you for your constant support and I apologize for the many questions I ask in this forum.
    Last edited by Riccardo Busin; 14 Nov 2021, 06:17.

  • #2
    changing -egen num`x'- and -egen den`x'- to -egen num`x'`y'- and -egen den`x'`y'- should fix the issue

    Comment


    • #3
      This portion of your code is incorrect, because each rowtotal() function is taking the total of a single observation.
      Code:
      foreach x in ROE EBITDAVENDITE ROI TOTFATTVAR LEVERAGE DE TOTIMMVAR PFNEBITDA {
      forvalues y=`=`YearN'-4'(1)`YearN' {
      egen num`x'=rowtotal(n`x'`y')
      }
      }
      
      foreach x in ROE EBITDAVENDITE ROI TOTFATTVAR LEVERAGE DE TOTIMMVAR PFNEBITDA {
      forvalues y=`=`YearN'-4'(1)`YearN' {
      egen den`x'=rowtotal(p`x'`y')
      }
      }
      You want rowtotal() to be given a list of values to total.
      Code:
      foreach x in ROE EBITDAVENDITE ROI TOTFATTVAR LEVERAGE DE TOTIMMVAR PFNEBITDA {
      local y4 = `YearN'-4
      egen num`x'=rowtotal(n`x'`y4'-n`x'`y')
      }
      
      foreach x in ROE EBITDAVENDITE ROI TOTFATTVAR LEVERAGE DE TOTIMMVAR PFNEBITDA {
      local y4 = `YearN'-4
      egen den`x'=rowtotal(p`x'`y4'-p`x'`y')
      }
      Thus, the first time through the first of these loops, after substituting the values of all the local macros, and pretending that the local macro YearN is 2014, the egen command becomes
      Code:
      egen numROE=rowtotal(nROE2010-nROE2014)
      which I think is what you want.

      Comment


      • #4
        Originally posted by William Lisowski View Post
        This portion of your code is incorrect, because each rowtotal() function is taking the total of a single observation.
        Code:
        foreach x in ROE EBITDAVENDITE ROI TOTFATTVAR LEVERAGE DE TOTIMMVAR PFNEBITDA {
        forvalues y=`=`YearN'-4'(1)`YearN' {
        egen num`x'=rowtotal(n`x'`y')
        }
        }
        
        foreach x in ROE EBITDAVENDITE ROI TOTFATTVAR LEVERAGE DE TOTIMMVAR PFNEBITDA {
        forvalues y=`=`YearN'-4'(1)`YearN' {
        egen den`x'=rowtotal(p`x'`y')
        }
        }
        You want rowtotal() to be given a list of values to total.
        Code:
        foreach x in ROE EBITDAVENDITE ROI TOTFATTVAR LEVERAGE DE TOTIMMVAR PFNEBITDA {
        local y4 = `YearN'-4
        egen num`x'=rowtotal(n`x'`y4'-n`x'`y')
        }
        
        foreach x in ROE EBITDAVENDITE ROI TOTFATTVAR LEVERAGE DE TOTIMMVAR PFNEBITDA {
        local y4 = `YearN'-4
        egen den`x'=rowtotal(p`x'`y4'-p`x'`y')
        }
        Thus, the first time through the first of these loops, after substituting the values of all the local macros, and pretending that the local macro YearN is 2014, the egen command becomes
        Code:
        egen numROE=rowtotal(nROE2010-nROE2014)
        which I think is what you want.
        Yes, that's what I meant.
        Thanks for the help, now the code runs perfectly.
        You have been very kind!

        Comment

        Working...
        X