Try
Code:
filelist, pat("*.TXT")
filelist, pattern("1*.TXT") filelist, pattern("2*.TXT")
foreach k in 1 2 3 4 5 { filelist, pat("`k'*.TXT") save("name_files_`k'.dta") replace use "name_files_`k'.dta", clear local obs = _N forvalues i=1/`obs' { use "name_files_`k'.dta" in `i', clear local f = filename clear infix uf 1-2 mun 3-7 dist 8-9 subdist 10-11 set 12-15 st_set 16-16 str lati 322-336 str longe 337-351 /// tp 472-473 str subtp 474-513 quadra 545-547 face 548-550 cep 551-558 using "`f'" gen source = "`f'" save "mydata`k'_`i'.dta", replace } }
clear forvalues i=1/`obs' { append using "mydata`i'.dta" } save "mydatacombo.dta", replace
clear save "my_txt_files.dta", emptyok replace foreach k in 1 2 3 4 5 { filelist, pat("`k'*.TXT") append using "my_txt_files.dta" save "my_txt_files.dta", replace } use "my_txt_files.dta", clear local obs = _N forvalues i=1/`obs' { use "my_txt_files.dta" in `i', clear local f = filename clear infix uf 1-2 mun 3-7 dist 8-9 subdist 10-11 set 12-15 st_set 16-16 /// str lati 322-336 str longe 337-351 /// tp 472-473 str subtp 474-513 quadra 545-547 face 548-550 cep 551-558 /// using "`f'" gen source = "`f'" save "mydata_`i'.dta", replace } clear forvalues i=1/`obs' { append using "mydata_`i'.dta" } save "mydatacombo.dta", replace
foreach k in 1 2 3 4 5 { filelist, pat("`k'*.TXT") save("name_files_`k'.dta") replace use "name_files_`k'.dta", clear local obs`k' = _N forvalues i=1/`obs`k'' { use "name_files_`k'.dta" in `i', clear local f = filename clear infix uf 1-2 mun 3-7 dist 8-9 subdist 10-11 set 12-15 st_set 16-16 str lati 322-336 str longe 337-351 /// tp 472-473 str subtp 474-513 quadra 545-547 face 548-550 cep 551-558 using "`f'" gen source = "`f'" save "mydata`k'_`i'.dta", replace } }
clear forval k in 1 2 3 4 5 { forvalues i=1/`obs`k'' { append using "mydata`k'_`i'.dta" } } save "mydatacombo.dta", replace
filelist keep if regexm(lower(filename),"\.txt$")
foreach k in 1 2 3 4 5 { clear tempfile temp save `temp', emptyok filelist, pat("`k'*.TXT") save("name_files_`k'.dta") replace use "name_files_`k'.dta", clear local obs = _N forvalues i=1/`obs' { use "name_files_`k'.dta" in `i', clear local f = filename clear infix uf 1-2 mun 3-7 dist 8-9 subdist 10-11 set 12-15 st_set 16-16 str lati 322-336 str longe 337-351 /// tp 472-473 str subtp 474-513 quadra 545-547 face 548-550 cep 551-558 using "`f'" gen source = "`f'" append using `temp' display "`k' & `i'" save `"`temp'"', replace } use `temp', clear save "mydata_`k'.dta", replace }
// Move to the directory where the files are located cd C:\Users\Paula\Pesquisa\DADOS\dados_georreferenciados\CEPs_Censo_2010\dados_bruto // List the files in the directory and pipe them into a text file ! DIR *.txt /a-d /b > C:\Users\Paula\Pesquisa\DADOS\dados_georreferenciados\CEPs_Censo_2010\dados_bruto\files.txt // Open a connection to the file file open flist using C:\Users\Paula\Pesquisa\DADOS\dados_georreferenciados\CEPs_Censo_2010\dados_bruto\files.txt, r // Read the first line of the file file read flist fname // Load the data and clear any data currently in memory infix uf 1-2 mun 3-7 dist 8-9 subdist 10-11 set 12-15 st_set 16-16 str lati 322-336 str longe 337-351 /// tp 472-473 str subtp 474-513 quadra 545-547 face 548-550 cep 551-558 using `"`fname'"', clear // Reserve namespace for a temp file tempfile mytempfile // Save the data temporarily qui: save `mytempfile'.dta, replace // Loop over the file while `r(eof)' != 1 { // Read the next line file read flist fname // Load the data and clear any data currently in memory infix uf 1-2 mun 3-7 dist 8-9 subdist 10-11 set 12-15 st_set 16-16 str lati 322-336 str longe 337-351 /// tp 472-473 str subtp 474-513 quadra 545-547 face 548-550 cep 551-558 using `"`fname'"', clear // Append the first file append using `mytempfile'.dta // Save over the temp file qui: save `mytempfile'.dta, replace } // End WHILE Block // Close the open file connection file close flist // save the data permanently save finalfile.dta, replace
. clear . set obs 10000 number of observations (_N) was 0, now 10,000 . gen fsize = 500000 . . * the size of the append dataset each time it is saved . gen double appendsize = sum(fsize) . . * the total number of bytes written . gen double totalbytes = sum(appendsize) . . dis %21.0fc totalbytes[_N] 25,002,500,000,000
filelist, pat("*.TXT") local obs = _N save "myfiles.dta", replace
forvalues i=1/`obs' { use "myfiles.dta" in `i', clear local f = filename insheet using "`f'", clear gen source = "`f'" save "mydata_`i'.dta", replace }
clear forvalues i=1/`obs' { append using "mydata_`i'.dta" }
* make a list of files, include all sub-directories filelist, pat("*.TXT") local obs = _N save "myfiles.dta", replace * input each file and save Stata datasets forvalues i=1/`obs' { use "myfiles.dta" in `i', clear local f = dirname + "/" + filename insheet using "`f'", clear gen source = "`f'" save "mydata_`i'.dta", replace } * Append! clear forvalues i=1/`obs' { append using "mydata_`i'.dta" } save "mydatacombo.dta", replace
Comment