Announcement

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

  • Importing multiple csv files from a folder and appending them

    Hi,

    I'm stuck with a very simple problem of importing a bunch of csv files from s folder and appending them. The files cannot be imported through a loop so I have done the following:


    Code:
    global weather C:\Users\...\Dropbox\...\Input\...\...\weather
    global tempdata C:\Users\...\Dropbox\...\...\data\temp data
    
    
    
    clear
    tempfile weather
    save `weather', emptyok
    
    local filenames: dir "$weather" files "*.csv*"
    foreach f of local filenames {
    import delimited using `"`f'"'
    gen source = `"`f'"'
    display `"Appending `f'"'
    append using `weather'
    save `"`weather'"', replace
    }
    
    export delimited using "$tempdata\Rainfall_appended.csv", firstrow(variables) replace
    This gives me error:
    file 1 xyz.csv not found
    But I have checked in the folder, the file does exist, so not sure what's going on.

    The folder weather has only these csv which are named like "1 Xyz", "2 Pqr" etc. I tried changing some names to all lower case, still getting the error.

    Appreciate any help, thanks.

  • #2
    So you are looking through all of the files in the weather directory? Is the working directory set to the $weather directory? If not then I think you need something like:

    Code:
    import delimited using "$weather/`f'"

    Comment

    Working...
    X