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: Concatenate files


From   Huaandrew <andrew0845@hotmail.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Concatenate files
Date   Sat, 31 Mar 2012 13:59:33 +0000

Hello everybody,
 
I am using Stata 12 to create a large master file which is created by appending 3,000 csv files. The problem is that the 3,000 files have two kinds of structure (mixed together and no way to separate them by file names): one has only one column and the other two columns. For the two-column files, the first column is character variable, while the second column is numeral(character numeric), 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 when appending them into the master file. What I'd like to do is that if it's one-column file, let Stata create a new column which is just like the first column in the two-column file.The original code without distinguishing the two kinds of files is as follows. When I ran it, 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.txtfile 
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