Announcement

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

  • error message r(110)

    Hello,
    I am having issues in using the generate or gen command. I get the message "variable2 already defined". I am just giving the basic set up of the code (which I have inherited
    here at our firm).


    ------------code summary-------------------
    foreach ... in $...{



    run a regression across 50 cities between city and nation

    //then I generate a new variable let's call it variable2

    by city: gen variable2 = x1-x2 if time>1990q1

    ----end of code summary--------------------------

    Then I get the following error even though variable2 was never defined prior to this line of code.

    "variable2 already defined"

    Any idea how to fix this? Thanks in advance.

    Umair





  • #2
    You are generating variable2 in a foreach loop. You can only generate the variable once, in the first iteration of the loop. The error message appears at the second iteration of the loop.

    Comment


    • #3
      Not giving us exact code is a bad idea. Unless you understand every detail of your code you may well edit out or paraphrase revealing content.
      That's one of many reasons we urge in the strongest possible terms that you show us exact code.

      But we can still guess.

      Second time around the loop the line

      Code:
      by city: gen variable2 = x1-x2 if time>1990q1
      will fail because variable2 was created first time around the loop. This will be a problem even if variable2 didn't exist before the loop.

      The solution is take the creation out of the loop. Before the loop you go

      Code:
      gen variable2 = .
      and then change it within the loop using replace.

      That said, unless you change x1 and x2 there is no reason to put this within the loop at all.


      Note that I am assuming that the line cited is within the loop. Your posting leaves that unclear. But the error message only makes sense if the variable already exists and the only information you give us is that it didn't exist before the loop.

      That said it's a puzzle that this line ever worked first time. Let's try comparing a quarterly date with 1990q1

      Code:
      . di yq(2015, 4) > 1990q1
      1990q1 invalid name
      That doesn't work. Perhaps that's part of the code you paraphrased.

      Why is so much guesswork needed here? Again, it's because you didn't post exact code. If we [Friedrich and I] guessed wrong, you need to tell us more.

      Naturally, if commercial sensitivity stops you showing all the code, then that's understood, but in such circumstances you need a private consultant or to try StataCorp technical support (although typically they won't debug user programs).
      Last edited by Nick Cox; 17 Nov 2015, 12:48.

      Comment


      • #4
        Thanks Fredrich and Nick for your quick response. Adding a replace line after the gen line fixed the problem.
        And yes, commercial sensitivities limited me in putting the full code in the forum.

        Comment

        Working...
        X