Warm tip: This article is reproduced from serverfault.com, please click

其他-Stata循环从Excel文件中检索数据

(其他 - Stata loop to retrieve data from Excel file)

发布于 2020-12-04 01:11:13

我的Excel文件有很多工作表。我想创建一个循环以检索每张数据表,以便将其另存为.dta。这是我的代码:

local sheet = `" "Data dictionary" "Wave 1" "Wave 2" "Wave 3" "Wave 4" "'

foreach x of local sheets{
     import excel "/Users/a/Downloads/EVSdata.xlsx", sheet("`x'") 
     save "/Users/a/Downloads/sheet_`x’.dta"
}

输出不会显示任何错误,但也不会保存数据。

Questioner
A01066656
Viewed
0
Cybernike 2020-12-04 09:27:45

一个很好的尝试,但是你的代码中有一些小的语法错误。请注意此代码中的细微差别:

local sheet = `" "Data dictionary" "Wave 1" "Wave 2" "Wave 3" "Wave 4" "'

foreach x in `sheet' {
     import excel "mydata.xlsx", sheet("`x'") clear
     save "sheet_`x'.dta", replace
}