循环遍历多个 Stata 文件

Looping through several Stata files

我有大约 40 个 .dta 文件,所有文件都包含相同的变量,但用于不同的观察单位。我想使用 Stata 循环访问我拥有所有 40 个文件的目录以执行以下操作:

  1. 将每个文件加载到 Stata,然后导出到 Excel sheet。这个想法是有一个 Excel 工作簿,有 40 个 sheets - 每个 .dta 文件一个。

  2. 在 40 个文件的每一个中,计算名为 car_type 的变量的不同观察值的数量。输出可以保存到与 1 不同的 Excel sheet 中。但我愿意接受建议。

我之前从未使用过 local 或循环,因此将不胜感激任何帮助。我知道对于第 1 步,我可以用 use "C:\File1.dta 编写一个 do 文件,然后为每个文件编写 export excel using "C:\Workbook.xlsx", sheet("File1") firstrow(variables) replace,但我不想一个一个地手动重复这个对于每个文件。

您可以以此为模板开始:

set more off

/* (A) Create 5 Fake Datasets file1,...,file5 */ 
sysuse auto, clear

forvalues i=1/5 {
    preserve
        keep if rep78==`i'
        tab rep78
        save "file`i'.dta", replace
    restore
}


/* (B) Export Fake Files to Excel */
capture erase "my_excel_file.xls" // erase Excel file if it exists

ssc install distinct
ssc install fs

fs "file*.dta" // get a list of files to export to Excel

/* loop over this list, opening each one, calculate distinct values, and put the data below that */
foreach f in `r(files)' {
    quietly {
        use "`f'", clear
        distinct make
        generate distinct_vals_of_make = r(ndistinct) in 1
        export excel distinct_vals_of_make using "my_excel_file", sheet("`f'") firstrow(variables) cell(A1)
        drop distinct_vals_of_make
        export excel using "my_excel_file", sheet("`f'", modify) firstrow(variables) cell(A4)
    }
}

shell open "my_excel_file.xls"

答案:

r(files) 是包含 fs 返回的文件列表的本地宏的名称。当您用反引号和结束引号将名称括起来时,r(files) 将吐出该列表。 "foreach f in" 只是一种在顺序循环时引用该列表中每个元素的方法。你也可以写 "foreach file_name in" 。每次循环迭代,f(或file_name)都会被重新定义为包含下一个文件,可以通过反引号加上f加上结束引号得到。 in 1表示在Stata数据集的第一行。

最后,我认为你可以用

   /* loop over this list, opening each one, calculate distinct values, update the stats sheet, and put the data in a seprate sheet */
/* Initialize the names */
gen file_name=.
gen distinct_vals_of_make=. 
export excel file_name distinct_vals_of_make using "my_excel_file", sheet("stats") firstrow(variables)
local j=2

foreach f in `r(files)' {
    quietly {
        use "`f'", clear
        gen file_name = "`f'" in 1  
        distinct make
        generate distinct_vals_of_make = r(ndistinct) in 1
        export excel file_name distinct_vals_of_make using "my_excel_file", sheet("stats", modify) cell(A`j')
        local ++j

        drop distinct_vals_of_make file_name
        export excel using "my_excel_file", sheet("`f'") firstrow(variables)
    }
}