一次导入 300 Excel 个文件 (Stata)
Importing 300 Excel Files at once (Stata)
我正在尝试自动将 300 个 Excel 文件导入 Stata。
这些文件代表 25 年的时间,每个月都在一个文件中。这些文件包含相同数量和类型的变量(即都是字符串变量)。
如果有任何提示,我将不胜感激。字符串变量如下:
unemp_rate gdp gini year
11 .02 22 2002
22 2 30 2003
我的代码如下:
clear all
ssc install xls2dta
xls2dta, save("C:\Users\xyz\OneDrive - xyz\Documents\xyz\xyz") : import excel "C:\Users\xyz\xyz- xyz\Documents\xyz\xyz"
xls2dta, save ("C:\Users\xyz\xyz- xyz\Documents\xyz\xyz") : append, force
use "C:\Users\xyz\xyz- xyz\Documents\xyz\xyz2\xyz.dta", clear
但是,变量已作为 A、B、C、D 等导入,而不是上面的实际变量名称。
有一整个discussion at Statalist about it,包括xls2dta
的用法;但是,这里有一个不依赖任何外部命令的解决方案。此外,这种进行方式特别适合您的问题,因为您提到所有文件都保留相同的结构(即相同的变量名)。
clear all
global route = "C:\root_to_your_files"
cd "$route"
tempfile building
save `building', emptyok
// List all the ".xlsx" files in folder "${route}"
local filenames : dir "${route}" files "*.xlsx"
// loop over all files, while appending them
foreach f of local filenames {
import excel using `"`f'"' ,firstrow allstring clear
//it identifies the procedence of the rows (i.e., the .xlsx)
gen source = `"`f'"'
// append new rows
append using `building'
save `"`building'"', replace
}
// save all the xlsx files in a single dta file
save "${route}\all_xlsx_files" ,replace
我正在尝试自动将 300 个 Excel 文件导入 Stata。 这些文件代表 25 年的时间,每个月都在一个文件中。这些文件包含相同数量和类型的变量(即都是字符串变量)。
如果有任何提示,我将不胜感激。字符串变量如下:
unemp_rate gdp gini year
11 .02 22 2002
22 2 30 2003
我的代码如下:
clear all
ssc install xls2dta
xls2dta, save("C:\Users\xyz\OneDrive - xyz\Documents\xyz\xyz") : import excel "C:\Users\xyz\xyz- xyz\Documents\xyz\xyz"
xls2dta, save ("C:\Users\xyz\xyz- xyz\Documents\xyz\xyz") : append, force
use "C:\Users\xyz\xyz- xyz\Documents\xyz\xyz2\xyz.dta", clear
但是,变量已作为 A、B、C、D 等导入,而不是上面的实际变量名称。
有一整个discussion at Statalist about it,包括xls2dta
的用法;但是,这里有一个不依赖任何外部命令的解决方案。此外,这种进行方式特别适合您的问题,因为您提到所有文件都保留相同的结构(即相同的变量名)。
clear all
global route = "C:\root_to_your_files"
cd "$route"
tempfile building
save `building', emptyok
// List all the ".xlsx" files in folder "${route}"
local filenames : dir "${route}" files "*.xlsx"
// loop over all files, while appending them
foreach f of local filenames {
import excel using `"`f'"' ,firstrow allstring clear
//it identifies the procedence of the rows (i.e., the .xlsx)
gen source = `"`f'"'
// append new rows
append using `building'
save `"`building'"', replace
}
// save all the xlsx files in a single dta file
save "${route}\all_xlsx_files" ,replace