Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: importing multiple text files


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   st: RE: importing multiple text files
Date   Fri, 4 Feb 2011 18:32:25 +0000

Let's imagine first three files file1.csv ... file3.csv 

Here's a naïve way to do it. The stub, here "feb4", should just be unused by existing files. 

local j = 1 
forval i = 1/3 { 
	insheet using file`i'.csv, clear
	gen id = "`i'" 
	save feb4`j++', replace 
} 

local J = `j' - 2 
forval j = `J'(-1)1 { 
	append using feb4`j' 
}

Now what about an arbitrary list of files? Making sure that only the needed files are the *.csv in a given directory or folder, 

fs *.csv 

local j = 1
foreach f in `r(files)' { 
	insheet using "`f'", clear
	gen id = "`f'" 
	save feb4`j++', replace 
} 

local J = `j' - 2 
forval j = `J'(-1)1 { 
	append using feb4`j' 
}

Where -fs- is on SSC. There is some clean up to do afterwards. 

There will be smarter ways to do this. 

Nick 
[email protected] 

Harry Comber

I have 304 text files (Windows, comma-separated) with identical
structures (5 numeric variables) from 304 different cancer registries,
which I would like to import into Stata 10 and combine into a single
file. Each file has about 10,000 observations.  The files are named
using a unique numeric identifier for the registry from which it comes
(e.g. 38408099.csv) and I would like to also import this number as a
variable into the file in order to identify the data from that registry.
This is beyond my programming skills and I am not even sure it is
possible. I would appreciate some advice.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index