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


From   "Radwin, David" <[email protected]>
To   <[email protected]>
Subject   st: RE: Append multiple files from .txt file with "file read"
Date   Thu, 5 Dec 2013 18:52:15 -0500

Nicole,

The -append- command lets you add multiple files at once without the
need for loops, ancillary text files, etc. Why not just make a local
macro of all the Stata files in your directory and then -append- them
all at once?


local f : dir . files "*.dta"
display as result `"`f'"'
append using `f'


David
--
David Radwin, Senior Research Associate
Education and Workforce Development
RTI International
2150 Shattuck Ave. Suite 800, Berkeley, CA 94704
Phone: 510-665-8274

www.rti.org/education


> -----Original Message-----
> From: [email protected] [mailto:owner-
> [email protected]] On Behalf Of Nicole Boyle
> Sent: Thursday, December 05, 2013 3:30 PM
> To: [email protected]
> Subject: st: Append multiple files from .txt file with "file read"
> 
> 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