I have several folders containing CSV files that I want to import into a single folder named "CSV Import". The folder structure I have looks like this: the main folder "C:/Main" has multiple subfolders, each in turn containing one or more subfolders with CSV files.
Currently, the code saves each imported CSV file as a ".dta" file in the same folder where the original CSV file resides. How can I modify the code to save all the ".dta" files in the "C:/CSV Import" folder instead?
Currently, the code saves each imported CSV file as a ".dta" file in the same folder where the original CSV file resides. How can I modify the code to save all the ".dta" files in the "C:/CSV Import" folder instead?
Code:
clear
local save_dir "C:/CSV Import"
filelist, dir("C:/Main") pattern("*.csv") save("csv_filelist.dta") replace
use "C:/Main/csv_filelist.dta", clear
gen filepath = dirname + "/" + filename
levelsof filepath, local(csv_file)
// Importing all csv files and saving as .dta
foreach x in `csv_file' {
import delimited using "`x'", clear
local dta_file = subinstr("`x'", ".csv", ".dta", .)
save "`dta_file'", replace
}

Comment