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: repeat do-file over multiple files


From   "Allan Stubbe" <[email protected]>
To   <[email protected]>
Subject   st: Re: repeat do-file over multiple files
Date   Sat, 3 Mar 2012 11:26:00 +0100

Thanks for the outline Stas Kolenikov

It helped but I cant make it run without errors.

When run as it is the following error occur:
 . do "C:\Users\Stubbe\AppData\Local\Temp\STD00000000.tmp"
 . clear
 . tempfile current
 . save "`current'"
 no variables defined
 r(111);
 end of do-file
 r(111);
 .


If the the following line "use "00321.dta", clear" are put in, it will run with no errors but end up with both the new data and the original dataset from "00321.dta".
 clear
 tempfile current
 use "00321.dta", clear
 save "`current'"
 local allsrcfiles : dir . files "*.dta"
 foreach ff in `allsrcfiles' {
   do ProcessOneFile.do `"`ff'"'
   append using `current'
   save `current', replace
 }
 exit

Offcourse I can just delete the original dataset and save the new dataset manually but I would love to learn what I have done wrong.
The problem seems to be with my handling of the "current" command.
Im new to macros etc. so I might make a stupid mistake?
I would love to be better at working with Stata so any help are very appreaciated.

Thanks
Allan Stubbe



-----Oprindelig meddelelse----- From: Stas Kolenikov
Sent: Thursday, March 01, 2012 11:07 PM
To: [email protected]
Subject: Re: st: repeat do-file over multiple files

On Thu, Mar 1, 2012 at 2:19 PM, science <[email protected]> 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/
*
*   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