Statalist


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

st: RE: Reading and Merging Multiple DBF files


From   "Joseph Coveney" <jcoveney@bigplanet.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Reading and Merging Multiple DBF files
Date   Mon, 23 Feb 2009 00:13:42 +0900

TA Stat  wrote:

How can I read and combine, in Stata, multiple DBF files with the same
variables all in once.

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

Your post's title said "merging", but by "combine" I assume that you want to
append them (UNION ALL in SQL).

If you're working in Windows, then you could use ODBC to read the dBase files
into Stata and append them.  It would look something like the do-file below.
For the example, I had set up a user DSN via Windows Control Panel to read dBase
IV files and named the DSN "dBase IV".  (StataCorp's website has details on how
to set up DSNs, I believe.)  The do-file below combines three dBase IV files
named First.dbf, Second.dbf and Third.dbf.

If you're going to be doing this type of task a lot, then look into licensing
Stat/Transfer ( www.stattransfer.com ).  It will pay for itself in convenience
and time saved.

Joseph Coveney

clear *
set more off
tempfile tmpfil0
odbc load, table(First) dsn("dbase IV")
save "`tmpfil0'"
foreach dbf in second third {
    odbc load, table(`dbf') clear
    append using "`tmpfil0'"
    save `tmpfil0', replace
}
erase "`tmpfil0'"
quietly compress
save AppendedDBFs
exit



*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index