Announcement

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

  • Mergining in a loop

    Hello,

    I'm trying to merge 3 tempfiles named: part1, part2, part3. I used the following code:

    use `part1', clear
    foreach i of numlist 2/3 {
    merge m:1 state year using `part`i''
    }

    However, it's only merging part1 and part2, not part3. What am i doing wrong?

  • #2
    Well, it would be helpful to see the actual output you get from Stata, including any error messages, as I can see several possibilties:

    1. Perhaps file `part3' does not exist.
    2. After you merge `part2' with `part1' the variable _merge will already exist, so when you try to now merge in `part3' Stata complains that _merge already exists and halts. Either add -drop _merge- to the end of the loop, or add the -nogenerate- option to the -merge- command to fix this.
    3. Perhaps state and year do not uniquely identify observations in `part3'.
    4. Perhaps state or year doesn't exist in `part3'.

    In any case, Stata would give you an error message which should tell you what is going on. Sometimes things are so confusing that Stata's error message is not helpful, occasionally even misleading, but in this context I would expect the error message to pinpoint the problem.

    Comment


    • #3
      I was having error #2. I fixed it with the nogenerate option. Thank you so much!

      Comment

      Working...
      X