Announcement

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

  • STATA Looping Merges overwrites observations

    Hello STATA friends,

    I'm trying to merge inventory datasets. There is one with unique IDs (SKUs) and revenue data across all years, then 5 more datasets with the SKUs each across only a single year with price data (all in the same format). Here's the code.
    forvalues i = 11/16 { merge m:1 year sku using "$int\price20`i'.dta", force drop _merge }
    However, when it loops through, the matched observations don't append, but replace the observations from the prior year. When the loop finishes, there's only one year's worth of price data remaining. Any thoughts as to how to fix?

  • #2
    Comments on various levels:

    1. Most importantly for you, this sounds like a case for append, not merge.

    2. Cross-posted at https://stats.stackexchange.com/ques...s-observations (although doomed there as off-topic). Please note our policy on cross-posting in the FAQ Advice, which is that you are asked to tell us about it.

    3. Your code would be easier to read presented as we ask (FAQ Advice #12), using CODE delimiters

    Code:
    forvalues i = 11/16 {
        merge m:1 year sku using "$int\price20`i'.dta", force
        drop _merge
    }
    from which my guess is

    Code:
    use $int\price2011 , clear
    
    forval i = 12/16 {
          append using $int\price20`i'
    }
    4. Please read the FAQ Advice all the way up to #18, which explains that the spelling is Stata, not STATA.
    Last edited by Nick Cox; 18 Jun 2018, 11:51.

    Comment


    • #3
      Duly noted on all accounts. Thank you for the response.

      Comment

      Working...
      X