Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Appending several files


From   Paul Seed <paul.seed@kcl.ac.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Appending several files
Date   Mon, 16 Oct 2006 14:36:15 +0100

Nuno Soares  <ns.mlists@gmail.com> asked :


I'm trying to make Stata to append several files that are in a given
directory without the need of specifying the name of each one. I was
thinking of doing something like this (using the fs package):

clear
cd "C:\data"
qui fs *
foreach f in `r(files)' {
append using `f'
}

And Ulrich Kohler <kohler@wz-berlin.de>

suggested:

. fs *.dta
. local firstfile: word 1 of `r(files)'
. use `firstfile', clear
. foreach f in `r(files)' {
.   if "`f'" ~= "`firstfile'" append using `f'
. }

- --

Ulrich's solution works, but I thought it was
worth pointing out another approach:

clear
fs *.dta

local getfile "use"
foreach file in `r(files)' {
	`getfile' `file'
	local getfile "append using "
}


I often use local macros in this way,
where a different command, or option,
is needed at different stages in a loop.

I can either change the command after the first
run (as here), or when some condition is met.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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