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 at the end of May, 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   Maarten buis <maartenbuis@yahoo.co.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Infile several ASCII files
Date   Thu, 1 Apr 2010 00:33:43 -0700 (PDT)

--- 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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index