Announcement

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

  • Merge two datasets wih the same variable names

    Hi there,

    I have several datasets, each with the prediction error of different forecast models. I would like to merge the datasets in order to compare those errors and determine which predicts better. My problem is that I can“t merge them since the varname is the same in every dataset, so when I try to merge them Stata will automatically delete the merging dataset.

    Any idea how can I merge them without having to change the varnames one by one, I am talking about 256 different datasets.

    Thanks for your help beforehand!

    Mario

  • #2
    I think append would be a better option. If for some reason you need the data as seperate variables you can still reshape wide after append to get the same result.

    Comment


    • #3
      Thanks Mr. Wakker,

      I am also trying to make a loop with it, any idea how?

      Comment


      • #4
        Here is one way:

        Code:
        sysdir
        
        /*LOAD YOUR FIRST FILE*/
        use "C:\[INSERT YOUR FILE PATH FOR STATA]\auto.dta", clear
        
        rename price price0
        
        /*LOOP THROUGH ALL OTHER DATA SETS AND RENAME THE DESIRED VARIABLE (PRICE) ACCORDING TO THE LOOP NUMBER*/
        /*OBVIOUSLY, PRICE WILL BE DUPLICATED HERE BUT WILL BE DIFFERENT FOR EACH OF YOUR DATA SETS*/
        
        local iteration=0
        forvalues filenumber=1/256 {
        local iteration=`iteration'+1
        
        /*ASSUMING YOUR FILES ARE NAMED SEQUENTIALLY, YOU WILL REFER TO THEM USING THE NUMBERING IN FORVALUES*/
        *quietly merge 1:1 make using "C:\Program Files (x86)\Stata13\auto`filenumber'.dta", keepusing(price)
        
        /*BUT FOR THIS EXAMPLE, THE SAME DATASET WILL BE MERGED REPEATEDLY*/
        quietly merge 1:1 make using "C:\Program Files (x86)\Stata13\auto.dta", keepusing(price)
        
        drop _merge
        rename price price`iteration'
        }
        
        browse
        Last edited by Jenny Williams; 29 Jun 2020, 08:07.

        Comment


        • #5
          Thanks Jenny, exactly was I was looking for!

          Comment


          • #6
            Or:
            Code:
            cd "C:\Users\Your\Path\Here"
            clear
            append using `: dir . files "*.dta"', generate(source)
            reshape wide whatever, i(id) j(source)

            Comment

            Working...
            X