Reshape faster… much faster

As mentioned in the last Stata News 39-1, the reshape command is now faster, much faster. For an ongoing project, I had to reshape daily data for sovereign bond spreads coming from the J. P. Morgan’s Emerging Market Bond Index – Global (EMBIG) database for 20 countries observed from January 2, 1990, to December 4, 2023. More detail about the reshape command can be found in a previous blog of mine:

Initially, when I used the reshape command, it took more than 6 minutes to reshape the data from wide to long. As recommended in the last Stata News, I changed the default option for the reshape command.

**#*** J.P. Morgan Emerging Market Bond Index (EMBI) ***********

clear
set maxvar 30000

import excel data\fincl_vars_BB_JS.xlsx, ///
 sheet("embig") cellrange(A1:MBK21) firstrow clear
 
rename A country

set reshape_favor speed

reshape long embig, i(country) j(period)

It took less than 5 seconds to reshape this huge amount of data!

format %td period

kountry country, from(other) stuck marker
rename  _ISO3N_ iso3n
drop    MARKER
kountry iso3n, from(iso3n) to(imfn) marker
rename _IMFN_ imfcode
drop    NAMES_STD MARKER iso3n

gen      month=mofd(dofd(period))
format   %tm month
order    month, first
drop     if  month == tm(2023m12)
drop     period
rename   month period
collapse (mean) embig, by(imfcode country period)
 
order    imfcode period
xtset    imfcode period
xtdes 

Now, I have a monthly data from January 1990 to January 2023 in a long format.

As we have seen in this blog, the reshape command is much faster now. In this example, the “reshape time” reduced from more than 600 seconds to 5 seconds. The files for replicating the results in this blog are available on my GitHub.