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

Re: st: RE: combining many data files

From   Kyle Hood <>
Subject   Re: st: RE: combining many data files
Date   Wed, 19 Nov 2008 22:03:59 -0500


The problem with this approach is that you need to have all your datasets named correctly. If you use the dir extended function, you can use a wildcard to get everything that fits some pattern. This obviates the need to rename everything (which is all well and good if you have 30 datasets, but becomes very difficult it you have 500, or need to perform the operation repeatedly).

Anyway, I think this very problem is the reason they put the "dir" function in there.


Matt Spittal wrote:

I'm a big fan of using -foreach- for these types of problems.  The first thing is to make sure that your datasets are named in a similar way.  In the example below I have used a set of datasets called data1, data2, ..., data30.  Once that is done, you can place your -append- command inside a -foreach- loop. For instance:

	use data1, clear
	foreach num of numlist 2/30 {
		append using data`num'

This code is really just a shortcut for the following:

	use data1, clear
	append using data2
	append using data3
	append using data30

Other variations are possible too, see -help foreach-.

-- Matt

-----Original Message-----
[]On Behalf Of Mingfeng Lin
Sent: Thursday, 20 November 2008 1:35 PM
Subject: st: combining many data files


I am wondering if there's a straightforward way to append a large
number of .dta files into one file using Stata? The -append- command
seems to disallow wildcards in the file names.  I do find one method
from UCLA's ATS website,

but I'm still hoping there's an easier way, or somehow getting the
-append- command to recognize the wildcards - so that I could just
simply type

append using *.dta

Thanks :)

*   For searches and help try:

*   For searches and help try:

Kyle Hood
Department of Economics
Yale University
New Haven, CT

*   For searches and help try:

© Copyright 1996–2021 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index