Insheet all files in a directory

1,855 views
Skip to first unread message

J

unread,
Sep 23, 2009, 3:49:39 PM9/23/09
to Stata Users Forum
Does anyone know of a clever way to import all delimited files in a
directory into a single stata file or even into separate stata files?

For my problem I have several hundred .csv files which I need to
append together. All data are in the same format. I don't want to have
to type the name of each .csv file into my .do file.

I want to do something like insheet using *.csv. Something like this
exists with StatTransfer where it will create a .dta file for
each .csv file. It of course doesn't work with insheet.

Another solution would be to be able to create a macro with the names
of all of the .csv files in it. Then I could loop over the files in a
foreach loop. Anyone know how to do this?

Alan Riley

unread,
Sep 23, 2009, 5:33:23 PM9/23/09
to Stata Users Forum
J <jacu...@gmail.com> asked how to import all .csv files from
a given directory into a single Stata dataset:

J's last paragraph is the way to approach this problem. Below is
a do-file which implements one possible solution. Comments are
embedded in the do-file explaining each step.

------------------------------------------------------------------
// clear any current data from memory
clear

// obtain list of filenames
local files : dir "." files "*.csv"

// (optional) sort list of filenames to ensure in alphabetical order
local files : list sort files

// create 'master' dataset from first file
gettoken first files : files
insheet using "`first'"
save master, replace

// loop over subsequent files
foreach fname of local files {
// make sure no data in memory
clear

// read a file, create a temporary Stata dataset from it,
// read in the master file, and append the temporary file
// to it, resaving the master at the end for the next
// iteration through the loop
insheet using "`fname'"
save temp, replace
use master
append using temp
erase temp.dta
save master, replace
}

// master dataset from all .csv files is now in memory

exit

------------------------------------------------------------------


Alan
ari...@stata.com

szenttehen

unread,
Sep 25, 2009, 4:32:19 PM9/25/09
to Stata Users Forum
Thank Alan!

In short if one wants to get a list of the files into a macro just use
the extend macro function as below:

local myfiles: dir "C:\data\" files "*.csv"

*Adding this will display the files names
foreach file of local myfiles {
display "`file'"
}


On Sep 23, 5:33 pm, Alan Riley <ari...@stata.com> wrote:
> J <jacul...@gmail.com> asked how to import all .csv files from
Reply all
Reply to author
Forward
0 new messages