Announcement

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

  • Merging more than 2 files

    Hi all,

    I have around 300 files which need to be merged into one. The files are all in the same folder as my do file and are called 1.dta, 2.dta, 3.dta etc. I tried to merge the files with the code below:

    Code:
    clear all
    global dir "C:\directory\stata data" 
    
    local myfilelist : dir "." files "*.dta"
    
    foreach filename of local myfilelist {
    
    use 'filename', clear
    merge 1:1 date using "$dir\1"
    drop _merge
    save "$dir\1", replace
    }
    as suggested in here. with this code i'm getting a invalid 'filename' error back from stata.

    Could one of you help me solve this problem? Thanks!

    Tristan

  • #2
    Your error is
    Code:
    use 'filename', clear
    The proper way to reference a local macro is to start with the left quote (`) character. Notice that it is not vertical: it slants down to the right. On a US keyboard you will find it to the left of the 1! key. Just change that one character and I think everything else will run.


    Comment


    • #3
      Looks like an error in the line:
      Code:
      use 'filename', clear
      To start a local quote, use the ` character.

      Code:
      use `filename', clear

      Comment


      • #4
        Thank you very much! silly mistake...

        Comment

        Working...
        X