简体   繁体   中英

Stata loop to retrieve data from Excel file

My Excel file has many sheets. I am trying to create a loop to retrieve every sheet of data to be able to save it as.dta. This is my code:

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"
}

The output does not show any error but it does not save the data either.

A good attempt, but there are a few small syntax errors in your code. Note the subtle differences in this code:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM