我正在撰寫一個訪問 excel 檔案中的多個作業表的 stata 程式,在對它們進行操作后,我嘗試保存結果,但它顯示作業表名稱不存在(找不到 Cabo,r(111))我的代碼是這樣的:
clear
foreach sheet in "Cabo" "Ga" "Inha" {
import excel using "filename.xlsx", sheet("`sheet'") firstrow
//here are operations//
if `sheet' == "Cabo" {
save test1018
}
else {
append using test1018.dta
save test1018
}
}
有誰知道我做錯了什么?謝謝!!
uj5u.com熱心網友回復:
, replace在追加后保存資料時,您還需要包含一個for。您還可以通過在回圈之前保存一個空資料集來簡化代碼。然后每個作業表都將附加到此資料集,您不必將第一張作業表與后續作業表區別對待。
*Save an empty dataset to append to
clear
save test1018.dta, replace emptyok
foreach sheet in "Cabo" "Ga" "Inha" {
import excel using "filename.xlsx", sheet("`sheet'") firstrow
//here are operations//
append using test1018.dta
save test1018.dta, replace
}
uj5u.com熱心網友回復:
@TheIceBear 在這里指出了很好的技術。
這個答案的重點是,對于三張紙,回圈不是必需的。這也應該有效,但取決于我們無法檢查的檔案的詳細資訊。
import excel using "filename.xlsx", sheet("Cabo") firstrow clear
save test1018
import excel using "filename.xlsx", sheet("Ga") firstrow clear
append using test1018
save test1018, replace
import excel using "filename.xlsx", sheet("Inha") firstrow clear
append using test1018
save test1018, replace
我確實同意這是我不撰寫回圈所能達到的程度,但是代碼清晰、透明,并且可能會花費更少的時間來撰寫。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/326225.html
