Hello,
I am trying to import different csv files in Stata with a loop. I am using the following code:
local countries "BEN CIV CMR COD GHA GIN KEN LBR MLI MOZ MRT NER NGA SEN SLE TGO TZA UGA ZMB"
foreach c of local countries {
clear all
import delimited "$agromaps"`c'".DBF.csv", varnames(1)
drop comments
keep if element_co == 51
drop element_co
merge m:1 item_code using "$agromaps\FAO_ITEM_CODES.dta"
drop if _merge == 2
drop _merge
keep if item_code == 661 | item_code == 656 | item_code == 328 | item_code == 254 | item_code == 242
sort name1 name2 item_code year
bysort name2 item_code: egen avg_crop = mean(data)
save "$agromaps"`c'"_DBF.dta", replace
}
This code works when I just do by one file, for example BEN. However, when I wrote the code I mentioned, it is said "using required" which I cannot understand why. Any help?
I am trying to import different csv files in Stata with a loop. I am using the following code:
local countries "BEN CIV CMR COD GHA GIN KEN LBR MLI MOZ MRT NER NGA SEN SLE TGO TZA UGA ZMB"
foreach c of local countries {
clear all
import delimited "$agromaps"`c'".DBF.csv", varnames(1)
drop comments
keep if element_co == 51
drop element_co
merge m:1 item_code using "$agromaps\FAO_ITEM_CODES.dta"
drop if _merge == 2
drop _merge
keep if item_code == 661 | item_code == 656 | item_code == 328 | item_code == 254 | item_code == 242
sort name1 name2 item_code year
bysort name2 item_code: egen avg_crop = mean(data)
save "$agromaps"`c'"_DBF.dta", replace
}
This code works when I just do by one file, for example BEN. However, when I wrote the code I mentioned, it is said "using required" which I cannot understand why. Any help?
Comment