Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

Re: st: repeat do-file over multiple files

From   Stas Kolenikov <>
Subject   Re: st: repeat do-file over multiple files
Date   Thu, 1 Mar 2012 17:07:04 -0500

On Thu, Mar 1, 2012 at 2:19 PM, science <> wrote:
> Dear all,
> I have 256 dta-files in a folder.
> The same do-file should be run on all 256 files and saved as 256 new files.
> And lastly the 256 new files should be combined using append to one file.
> My question is if that is possible and how it should be done?
> Maybe the results from each of the 256 files can be stored in memory and
> just saved as 1 file.

How comfortable are you using local macros? You should be, as they are
essential for this job.

tempfile current
save `current'
local allsrcfiles : dir . files "*.dta"
foreach ff in `allsrcfiles' {
  do `"`ff'"'
  append using `current'
  save `current', replace

where has the structure

capture use `"`1'"'
if _rc exit
generate ...
regress ...
collapse ...
save `"Processed_`1'"', replace
* leave the data in memory without saving anything

This is an outline, it might be breaking down in some weak spots, such
as spaces in filenames (although I don't think it will), but it should
give you a reasonable starting point.

Stas Kolenikov, also found at
Small print: I use this email account for mailing lists only.
*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index