Dear Experts
This would be very basic question but I have been struggling with foreach for several days and would love to get support from you.
I tried to look up answers and advices but most of them are referring to file names with different numbers.
For example, codes were forevalues n = 2000/2015
In my case, I saved csv data set using different names as follows.
TB_coepidemics of TB and HIV_country_WHO
TB_coepidemics of TB and HIV_income groups_WHO
TB_coepidemics of TB and HIV_region_WHO
TB_drug resistant_country_WHO
TB_drug resistant_income groups_WHO
TB_drug resistant_region_WHO
TB_incidence_country_WHO
TB_incidence_income groups_WHO
TB_incidence_region_WHO
TB_new case_country_WHO
TB_new case_income groups_WHO
TB_new case_region_WHO
.....
1. My first task is to import each csv file and save them as dta
I used the below codes. If i don't use substr, the file name was saved .......csv.dta. But I would like to keep only dta.
global FOLDER `"TB/Data/STATA"'
local csvdir: dir "TB/Data/CSV" files "TB_*.csv"
foreach file in `csvdir' {
insheet using `file', firstrow, clear
local filename = "substr("`file'", -4, "")"
save `"$FOLDER/`filename'.dta"', replace
}
2. My 2nd task would be
appending 3 files in the same group.
For example,
1 TB_coepidemics of TB and HIV_all file for
TB_coepidemics of TB and HIV_country_WHO
TB_coepidemics of TB and HIV_income groups_WHO
TB_coepidemics of TB and HIV_region_WHO
1 TB_drug resistant_all file for
TB_drug resistant_country_WHO
TB_drug resistant_income groups_WHO
TB_drug resistant_region_WHO
3. Then merge these appended file using variables country year
Could you please advise me how I can do this repetitive work using foreach?
Thank you so much.
This would be very basic question but I have been struggling with foreach for several days and would love to get support from you.
I tried to look up answers and advices but most of them are referring to file names with different numbers.
For example, codes were forevalues n = 2000/2015
In my case, I saved csv data set using different names as follows.
TB_coepidemics of TB and HIV_country_WHO
TB_coepidemics of TB and HIV_income groups_WHO
TB_coepidemics of TB and HIV_region_WHO
TB_drug resistant_country_WHO
TB_drug resistant_income groups_WHO
TB_drug resistant_region_WHO
TB_incidence_country_WHO
TB_incidence_income groups_WHO
TB_incidence_region_WHO
TB_new case_country_WHO
TB_new case_income groups_WHO
TB_new case_region_WHO
.....
1. My first task is to import each csv file and save them as dta
I used the below codes. If i don't use substr, the file name was saved .......csv.dta. But I would like to keep only dta.
global FOLDER `"TB/Data/STATA"'
local csvdir: dir "TB/Data/CSV" files "TB_*.csv"
foreach file in `csvdir' {
insheet using `file', firstrow, clear
local filename = "substr("`file'", -4, "")"
save `"$FOLDER/`filename'.dta"', replace
}
2. My 2nd task would be
appending 3 files in the same group.
For example,
1 TB_coepidemics of TB and HIV_all file for
TB_coepidemics of TB and HIV_country_WHO
TB_coepidemics of TB and HIV_income groups_WHO
TB_coepidemics of TB and HIV_region_WHO
1 TB_drug resistant_all file for
TB_drug resistant_country_WHO
TB_drug resistant_income groups_WHO
TB_drug resistant_region_WHO
3. Then merge these appended file using variables country year
Could you please advise me how I can do this repetitive work using foreach?
Thank you so much.
Comment