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]

st: read two kinds of files into a master file


From   Huaandrew <andrew0845@hotmail.com>
To   <statalist@hsphsun2.harvard.edu>
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/


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