Hi, I have a folder (D:\myname\Data\) containing files for each year, file1990.csv, file1991.csv, ...file2015.csv. I want to import them all at once and create a year variable based on the file name, so that for file1990.csv, year=1990. I have a following codes that work for importing all files, but I don't know how to create a year variable. Could anyone please help me? Thank you!
cd "D:\myname\Data"
clear
local allfiles: dir . files "*.csv"
foreach file of local allfiles {
preserve
import delimited using `file', clear
save temp, replace
restore
append using temp, force
}
cd "D:\myname\Data"
clear
local allfiles: dir . files "*.csv"
foreach file of local allfiles {
preserve
import delimited using `file', clear
save temp, replace
restore
append using temp, force
}
Comment