Dear all,
My issue is, I want to convert all files .csv to .dta, so that later on I can edit, append, and merge them together.
However, I have a list of folders, called region1, region2, region 3.. to region20.
In each folder, there are 5 .csv files with the same names (across 20th region files), called file1, file2, file3, file4, file5.
My purpose is to convert those files1-5 in every 20 folders (region1-region20) into .dta so that I can work on them later.
This is my code so far, I guess I have a problem of specifying the local destination (or my thought process)
And this is the error I got:
I would be very grateful if someone can help me with this
And also any notice I should keep in mind if I work with them (edit inside, append, merge) later
Many thanks
My issue is, I want to convert all files .csv to .dta, so that later on I can edit, append, and merge them together.
However, I have a list of folders, called region1, region2, region 3.. to region20.
In each folder, there are 5 .csv files with the same names (across 20th region files), called file1, file2, file3, file4, file5.
My purpose is to convert those files1-5 in every 20 folders (region1-region20) into .dta so that I can work on them later.
This is my code so far, I guess I have a problem of specifying the local destination (or my thought process)
Code:
cd "C:\Work\DATA\temp1"
local sourcecsv "C:\Work\DATA\temp1" #(temp1 contains 20 folders region1-region20. Inside each region- folders are .csv files)
local dtafile "C:\Work\DATA\temp1\temp2" #(new folders that I want to have 20 new folders of regions1-20, inside there are .dta files)
foreach t of local sourcecsv {
local filenames: dir "`t'" files "*.csv"
foreach f in `t' {
clear
import delimited "`t'/`f'"
local dta_name: subinstr local f ".csv" ""
cap noi mkdir "`dtafile'/`t'"
cd "`dtafile'/`t'"
save `dta_name'.dta, replace
}
}
And this is the error I got:
Code:
- foreach t of local sourcecsv {
- local filenames: dir "`t'" files "*.csv"
= local filenames: dir "C:\Work\DATA\temp1" files "*.csv"
- foreach f in `t' {
= foreach f in C:\Work\DATA\temp1 {
- clear
- import delimited "`t'/`f'"
= import delimited "C:\Work\DATA\temp1/C:\Work\DATA\temp1"
file C:\Work\DATA\temp1/C:\Work\DATA\temp1.csv not found
local dta_name: subinstr local f ".csv" ""
cap noi mkdir "`dtafile'/`t'"
cd "`dtafile'/`t'"
save `dta_name'.dta, replace
}
}
And also any notice I should keep in mind if I work with them (edit inside, append, merge) later
Many thanks

Comment