Hello there,
I am trying to merge appr. 400 excel files, ID1, ID2 and so on, into a dataset. Each file contains multiple sheets, day1, day2 etc. up to day 9, but not all sheets are present in each file.
How do I loop over the files, skipping a file when it does not contain the relevant sheet?
Up till now my code is:
cd "C:\Users\Tjur\Dropbox\Studie\ny"
local allfiles : dir . files "*.xls"
display `allfiles'
foreach file in `allfiles' {
import excel using `file', sheet("day1") firstrow clear
save "`noextension'"_day1, replace
}
And this code is only to import the "day1" sheet. My plan is then to merge the sheets by ID and append all the merged files to one dataset. However I can't find the solution for skipping when a sheet is not present. I have tryed using the command capture, but can't quite get it right. Also, it is likely, that there is a more elegant way about this??
Any suggestions?
Kind regards/
Marianne
I am trying to merge appr. 400 excel files, ID1, ID2 and so on, into a dataset. Each file contains multiple sheets, day1, day2 etc. up to day 9, but not all sheets are present in each file.
How do I loop over the files, skipping a file when it does not contain the relevant sheet?
Up till now my code is:
cd "C:\Users\Tjur\Dropbox\Studie\ny"
local allfiles : dir . files "*.xls"
display `allfiles'
foreach file in `allfiles' {
import excel using `file', sheet("day1") firstrow clear
save "`noextension'"_day1, replace
}
And this code is only to import the "day1" sheet. My plan is then to merge the sheets by ID and append all the merged files to one dataset. However I can't find the solution for skipping when a sheet is not present. I have tryed using the command capture, but can't quite get it right. Also, it is likely, that there is a more elegant way about this??
Any suggestions?
Kind regards/
Marianne
Comment