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: read two kinds of files into a master file
From
Huaandrew <[email protected]>
To
<[email protected]>
Subject
st: read two kinds of files into a master file
Date
Sun, 25 Mar 2012 17:59:42 +0000
Hello,
I am using Stata 12 to create a large master file which is appended by 3,000 csv files. The problem is that the 3,000 files have two kinds of structure: one has only one column and the other two columns. For the two-column files, the first column is character used as note for the second column, which is numeral, such as 12,356. For the one-column files, the column is numeral as in the two-column file. What Stata commands I should use to distinguish the two kinds of files and if it's one-column file, let Stata create a new column which is used as note as in the two-column file.
The original code without distinguishing the two kinds of files is as follows. Stata clashed when reading the first one-column file after reading several two-column files.
cd d:\Stata_Data\found
! dir *.csv /a-d /b>d:\Stata_Data\filelist.txt
file open myfile using d:\Stata_Data\filelist.txt, read
file read myfile line
insheet using `line', comma
gen hpl_id = substr("`line'",1,22)
save `line'.dta, replace
save master_data.dta, replace
drop _all
file read myfile line
while r(eof)==0{
insheet using `line', comma
gen hpl_id = substr("`line'",1,22)
save `line'.dta, replace
append using master_data.dta
save master_data.dta, replace
drop _all
file read myfile line
}
Thanks!
Andrew
*
* 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/