Announcement

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

  • [HELP] Appending several CSV files together

    Hello Statalist,

    Currently trying to append several CSV files into one on Stata (I understand the risk in doing this). All the files are located in one folder on my desktop.

    I used the following code:

    Code:
    local filenames: dir "." files "*.csv"  
    
    foreach f of local filenames {
    import delim using `"`f'"', clear
    gen source_file = `"`f'"'
    append using `building'
    save `"`building'"', replace
    }
    I then got the following error:

    Code:
    invalid file specification
    r(198);
    When I browed to see what had happened I saw that stata had imported the first dataset fine, but did not append the rest.

    Any ideas on what I could do from here? Would greatly appreciate it!

  • #2
    Maybe begin the code section with something like
    Code:
    drop _all
    tempfile building
    quietly save `building', emptyok

    Comment


    • #3
      Worked perfectly, thank you so much. The finalized code (for those of you in the future) was the following:

      Code:
      local filenames: dir "." files "*.csv"
      drop _all
      tempfile building
      quietly save `building', emptyok
      foreach f of local filenames {
      import delim using `"`f'"', clear
      gen source_file = `"`f'"'
      append using `building'
      save `"`building'"', replace
      }

      Comment

      Working...
      X