Good morning,

I am working with TRADHIST data on Stata 14, which collects data on bilateral trade between two countries from 1827. The data looks as follows:

input str10(iso_o iso_d) int year float(FLOW GDP_o GDP_d IPTOT_o IPTOT_d XPTOT_o XPTOT_d)
"2SICIL" "AUTHUN" 1838 492920.7 . . . 10158525 . 10354825
"2SICIL" "BEL" 1834 38164.5 . . . 7171259 . 4669327
"2SICIL" "BEL" 1835 30052.34 . 61133280 . 6767636 . 5409698
"2SICIL" "BEL" 1836 44906.54 . 68204648 . 7668379 . 5931506
"2SICIL" "BEL" 1837 33957.27 . 71462456 . 8206626 . 5307155
"2SICIL" "BEL" 1838 47279.15 . 72479888 . 8237308 . 6421483
"2SICIL" "BEL" 1839 42427.62 . 76413248 . 7397826 . 5689458
"2SICIL" "BEL" 1840 38408.54 . 76188520 . 8499953 . 5772254
"2SICIL" "BEL" 1841 32978.24 . 76614128 . 8604525 . 6336223
"2SICIL" "BEL" 1842 43254.86 . 73157840 . 9352933 . 5811263
"2SICIL" "BEL" 1843 55120.8 . 71166792 . 8633158 . 6382548
"2SICIL" "BEL" 1844 36832.23 . 66871696 . 7658703 . 6761677
"2SICIL" "BEL" 1845 47595.44 . 68702456 . 8841313 . 7065934
"2SICIL" "BEL" 1846 44451.03 . 73220056 . 8279966 . 5667374
"2SICIL" "BEL" 1847 26068.5 . 84275952 . 9289152 . 6593477
"2SICIL" "BEL" 1848 22501.52 . 71001344 . 7031148 . 5836409
"2SICIL" "BEL" 1849 38073.31 . 72136376 . 8041558 . 6963410

So for each exporting country I have the corresponding exports to all other countries, for all years. For every country couple and year, I need to add a variable for imports. So, for example, in the very first row I would need to have also the value for exports from Belgium to The Kingdom of the 2 Sicilies ("SICIL) in 1834. I tried with the following loop:
Code:
levelsof iso_o, l(countries_o)
levelsof iso_d, l(countries_d)

foreach v of local countries_o{
foreach z of local countries_d{
gen exports_`v'`z'=.
replace exports_`v'_`z'=FLOW if iso_o=="`v'" iso_d=="`z'"
bysort year: egen m_exports_`v'`z'=min(exports`v'_`z'), m
drop exports_`v'_`z'
}
}
rename Flow Exports
gen Imports=.
foreach v of local countries_o{
foreach z of local countries_d{
replace Imports=m_exports_`v'_`z' if iso_o==`z' & iso_d=="`v'"
}
}
but it is not working as there are way too many observations. Would you happen to know a clever way to solve this issue in a rather "light" manner, so that any computer can run it?

Thanks in advance for your help!

Matteo