Hi
I am using Stata 15.0. I am creating code that will automatically clean and create variables from four different types of Excel sheets that will be saved in four different folders (Folder1- Folder4). I do not know how many Excel sheets will be saved into the different folders at this stage. I know how to loop through the files in each folder, import the files and save them as Stata files, but when I try and create the outer loop (foreach folder in Folder1 Folder2 Folder3 Folder {) I run into problems as Stata shows the error below, and it doesn't seem to be reading `folder' correctly.
Error:
directory C:\Users\Input\Folders\`folder'\ not found
My code:
If I change the 5th line of code to be what I've written below, then I receive the following error suggesting that my `folder' is being read correctly but the apostrophes remain.
Error:
directory C:\Users\Input\Folders\`Folder1'\ not found
Where am I going wrong?
Thanks!
Megan
I am using Stata 15.0. I am creating code that will automatically clean and create variables from four different types of Excel sheets that will be saved in four different folders (Folder1- Folder4). I do not know how many Excel sheets will be saved into the different folders at this stage. I know how to loop through the files in each folder, import the files and save them as Stata files, but when I try and create the outer loop (foreach folder in Folder1 Folder2 Folder3 Folder {) I run into problems as Stata shows the error below, and it doesn't seem to be reading `folder' correctly.
Error:
directory C:\Users\Input\Folders\`folder'\ not found
My code:
Code:
global input "C:\Users\Input\Folders" *Loop across the four folders: Folder1 Folder2 Folder3 Folder 4 foreach folder in Folder1 Folder2 Folder3 Folder 4{ * Set so can loop over all files in that folder local files : dir "$input\`folder'\" files "*.xlsx" cd "$input\`folder'\" * Import from Excel and save as a datafile foreach file in `files' { * Import import excel using "`file'", clear save "$temp\`file'.dta", replace } }
Error:
directory C:\Users\Input\Folders\`Folder1'\ not found
Code:
local files : dir "$input\``folder''\" files "*.xlsx"
Thanks!
Megan
Comment