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]
Re: st: Append multiple files from .txt file with "file read"
From
Sergiy Radyakin <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: Append multiple files from .txt file with "file read"
Date
Thu, 5 Dec 2013 19:31:32 -0500
Nicole, use of -ls- is platform-specific, not only it is not available
on all platforms (e.g. in Windows) but also where available it might
produce results formatted differently. Here is a Windows-based
implementation of your above intended strategy. Note that the lines
you pull out from the list file will contain file size and other
trash. You can configure -dir- to skip that, but since you are at a
different platform anyways, consult your OS manual, I just display the
line. Secondly, in your solution you may want to put the quotes around
the filename, in case it contains spaces. In any case I would suggest
to use the -dir- macro as shown below:
local folder t:\2013\nb
cd `folder'
// one solution
tempfile index
!dir /s *.dta > "`index'"
file open fh using `"`index'"', read text
local l=1
file read fh line
while !r(eof) {
display `"`l' `line'"'
file read fh line
local l=`l'+1
}
file close fh
// this is much better:
local flist `"`:dir . files "*.dta"'"'
foreach f of local flist {
display `"`f'"'
}
** eof
Use -set trace on- to find out what is the content of your locals
before it fails.
Best, Sergiy Radyakin
On Thu, Dec 5, 2013 at 6:30 PM, Nicole Boyle <[email protected]> wrote:
> Hello all,
>
> First and foremost, I have yet to fully understand how to use macros,
> so please forgive me if the solution to this problem is painfully
> obvious. I actually hope it's painfully obvious.
>
> I'm trying to combine multiple .dta files (1:1 horizontally appended)
> by calling several .dta filenames stored in a .txt file. However, in
> the process of doing this, whenever I try to run:
>
> . use `line'
>
> Stata returns the error:
>
> . invalid file specification
>
>
> Here's the code I'm trying to execute (sourced from here*). To start,
> I'm trying to execute this code on a .txt file containing just two
> lines (aka: two .dta filenames), but the final file will have 25
> lines:
>
> pwd
> cd ~/Desktop/merge
> ! ls *.dta >filelist.txt
> file open myfile using "filelist.txt", read
> file read myfile line
> use `line' /* ERROR HERE */
> save master_data, replace
> file read myfile line
> while r(eof)==0 {
> append using `line'
> file read myfile line
> }
> file close myfile
> save master_data, replace
>
>
> I first thought the problem was that "filelist.txt" wasn't being read.
> However, I believe it IS being read, since running the following:
>
> ! ls *.dta >filelist.txt
> file open myfile using "filelist.txt", read
> file read myfile line
> while r(eof)==0 {
> display "`=word("`line'",1)'"
> file read myfile line
> }
>
> only displays the second (but not the first) line of the two-line .txt file.
>
> Perhaps my issue has something to do with Stata overlooking the first
> line of the .txt file? Or perhaps my general macro-incompetence (more
> likely)?
>
> Any help will be greatly appreciated. Thanks so much for your consideration.
>
> Nicole
>
>
> *Code from http://www.ats.ucla.edu/stat/stata/faq/append_many_files.htm
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/