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: Infile several ASCII files
From
Maarten buis <[email protected]>
To
[email protected]
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/