Hello,

I am importing a large number of .csv files into Stata. Before I can import the files, I need to remove several of the columns in the csv. (If I keep them in, it makes the later analysis very difficult). The number and name of the columns is constant across all the files.

I've used the code below with the intention of importing each of the csv files in the folder, dropping the unwanted columns, and saving each modified csv into a new file with a "_new" suffix. The code below works - however, it stops after the first file and does not continue through the rest of the files.

local filenames: dir . files "*.csv"

foreach file of local filenames {
import delimited "`file'" , delimiter(",") clear
drop v2 v3 v4 //dropping the unwanted columns
local new : subinstr local file ".csv" "_new.csv", all
export delimited using "`new'", replace
}



Does anyone have advice on what is missing here? Any advice or pointers would be appreciated.