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 at the end of May, and its replacement, statalist.org 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 <skolenik@gmail.com>
To   statalist@hsphsun2.harvard.edu
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 <science@allanstubbe.dk> 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.

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

where ProcessOneFile.do has the structure

clear
capture use `"`1'"'
if _rc exit
generate ...
regress ...
collapse ...
save `"Processed_`1'"', replace
exit
* 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 http://stas.kolenikov.name
Small print: I use this email account for mailing lists only.
*
*   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   |   Site index