DESCRIBTION OF FOLDERS AND FILES
Level 1 = Main folder named HH_Plots, directory "C:\Atinga Files\IPADS DOCS\IPADS PhD Program\Theses\Weather Data\HH_Plots"
Level 2 = Subfolders (900) named 1 to 900, (each folder represent information from a household)
Level 3 = Files in each subfolder (8 csv files) named A, B, C, D, E, F, G, H (type of information from each household)
I want to append each type of file (example file A) in all the 900 folders as a single file, and also generate an ID variable (1 - 900) showing which folder data is appended from
Approach;
I first imported all csv files and saved all files as dta in respective folders named eg. A.dta to H.dta
I am however not able to figure out how to generate an ID variable and append each type of file.
Level 1 = Main folder named HH_Plots, directory "C:\Atinga Files\IPADS DOCS\IPADS PhD Program\Theses\Weather Data\HH_Plots"
Level 2 = Subfolders (900) named 1 to 900, (each folder represent information from a household)
Level 3 = Files in each subfolder (8 csv files) named A, B, C, D, E, F, G, H (type of information from each household)
I want to append each type of file (example file A) in all the 900 folders as a single file, and also generate an ID variable (1 - 900) showing which folder data is appended from
Approach;
I first imported all csv files and saved all files as dta in respective folders named eg. A.dta to H.dta
Code:
cd "C:\Atinga Files\IPADS DOCS\IPADS PhD Program\Theses\Weather Data\HH_Plots"
filelist
drop if filename == ".DS_Store"
gen path = dirname + "\" + filename
levelsof path, local(tofile)
foreach x in `tofile' {
import delimited using "`x'", clear
drop in 1/8
keep v1 v3
rename ( v1 v3 )(Time Prcp)
split Time , parse("-" " ") generate(Time)
split Prcp , parse(",") generate(Prcp)
local tosave = subinstr("`x'", ".csv", "", .)
save "`tosave'.dta", replace
}

Comment