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: Append multiple files from .txt file with "file read"


From   Nicole Boyle <nicboyle@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: Append multiple files from .txt file with "file read"
Date   Thu, 5 Dec 2013 15:30:06 -0800

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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index