Dear Statalist,
I am trying to append a series of csv file within several folders using a double loop. In particular my folder structure is the following:
---rawdata---wheat---Italy
--- UK
--- USA
--- Germany
---Maize---Italy
--- UK
--- USA
--- Germany
and so on. To this end I am using the following double loop:
* Define Agricultural Products
local products ""wheat" "maize" "Sunflower Oil" "Bovine Meat" "Olive Oil""
* Define the countries to process
local countries "Italy France UK USA Germany"
foreach product in `products' {
local filepath = "$path/rawdata/comtrade/`product'"
* Outer loop over countries
foreach country in `countries' {
local filepath = "`filepath'/`country'" // Save path to current folder in a local
local files : dir "`filepath'" files "*.csv" // Save name of all files in folder ending with .csv in a local
di `"`files'"' // Display list of files to import data from
tempfile master // Generate temporary save file to store data in
save `master', replace empty
foreach x of local files {
* Import the file
import delimited "`filepath'/`x'", clear
* Save as a temporary .dta file
tempfile temp_dta
save "`temp_dta'", replace
* Append to the master file
append using `master'
save `master', replace
save "$path/data/`country'_`product'_1962_2023", replace
}
}
}
The loop runs, hovever, instead of obtaining datasets for each combination of country and product, I get that the data for different products of the same country end up in the same dataset. In practice a mess. Could you please help me in sorting it out?
Thanks in advance
I am trying to append a series of csv file within several folders using a double loop. In particular my folder structure is the following:
---rawdata---wheat---Italy
--- UK
--- USA
--- Germany
---Maize---Italy
--- UK
--- USA
--- Germany
and so on. To this end I am using the following double loop:
* Define Agricultural Products
local products ""wheat" "maize" "Sunflower Oil" "Bovine Meat" "Olive Oil""
* Define the countries to process
local countries "Italy France UK USA Germany"
foreach product in `products' {
local filepath = "$path/rawdata/comtrade/`product'"
* Outer loop over countries
foreach country in `countries' {
local filepath = "`filepath'/`country'" // Save path to current folder in a local
local files : dir "`filepath'" files "*.csv" // Save name of all files in folder ending with .csv in a local
di `"`files'"' // Display list of files to import data from
tempfile master // Generate temporary save file to store data in
save `master', replace empty
foreach x of local files {
* Import the file
import delimited "`filepath'/`x'", clear
* Save as a temporary .dta file
tempfile temp_dta
save "`temp_dta'", replace
* Append to the master file
append using `master'
save `master', replace
save "$path/data/`country'_`product'_1962_2023", replace
}
}
}
The loop runs, hovever, instead of obtaining datasets for each combination of country and product, I get that the data for different products of the same country end up in the same dataset. In practice a mess. Could you please help me in sorting it out?
Thanks in advance
Comment