Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Infile several ASCII files


From   Andrzej Niemierko <aniemierko@PARTNERS.ORG>
To   Stata <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Infile several ASCII files
Date   Fri, 02 Apr 2010 00:06:23 -0400

Thank you Maarten, Elan, Jeph, and Martin for your helpful suggestions. I
ended up using tempfiles and looping over file names. Although -append-
allows appending several datasets at once I had to merge datasets and
-merge- works only with two datasets at a time.
Andrzej



On 4/1/10 3:33 AM, "Maarten buis" <maartenbuis@yahoo.co.uk> wrote:

> --- On Wed, 31/3/10, Andrzej Niemierko wrote:
>> I have a data management problem. My data are stored in
>> several ASCII (.txt) files. Each file has the same
>> number of observations (in the same order). I can
>> -infile- each file individually, but I would like to
>> -infile- all of them and combine them into a one dataset.
>> I cannot figure out how to do it as -infile- refuses to
>> work if data are already in memory. I suspect that
>> someone has already faced that problem and knows the
>> solution. 
> 
> For this type of combining information from different
> datasets I tend to use a tempfile (which I usually call
> `tofill') and than use commands like -merge- and -append-
> to fill that file step by step. Applying this strategy
> to your case could work out something like this:
> 
> *---------------- begin example --------------
> // create some datasets
> cd "c:\temp"
> clear
> input x1
> 1 
> 2
> end
> outfile using "a.raw", replace
> 
> clear
> input x2
> 3 
> 4
> end
> outfile using "b.raw", replace
> 
> clear
> input x3
> 5 
> 6
> end
> outfile using "c.raw", replace
> 
> // combining them
> local i = 1
> foreach file in a b c {
> clear
> infile x`i' using `file'
> gen long id = _n
> sort id
> if `i' == 1 {
> tempfile tofill
> save `tofill'
> }
> else {
> merge 1:1 id using `tofill'
> sort id
> assert _merge == 3
> drop _merge
> save `tofill', replace
> }
> local ++i
> }
> 
> list
> *----------------- end example ------------------
> (For more on examples I sent to the Statalist see:
> http://www.maartenbuis.nl/example_faq )
> 
> Hope this helps,
> Maarten
> 
> --------------------------
> Maarten L. Buis
> Institut fuer Soziologie
> Universitaet Tuebingen
> Wilhelmstrasse 36
> 72074 Tuebingen
> Germany
> 
> http://www.maartenbuis.nl
> --------------------------
> 
> 
> 
> 
>       
> 
> *
> *   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/



The information in this e-mail is intended only for the person to whom it is
addressed. If you believe this e-mail was sent to you in error and the e-mail
contains patient information, please contact the Partners Compliance HelpLine at
http://www.partners.org/complianceline . If the e-mail was sent to you in error
but does not contain patient information, please contact the sender and properly
dispose of the e-mail.

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index