Mar 18, 2013

Merging quarterly time-series data with monthly time series data

version 12
set more off
clear

// DATASET: GDP and main components - Current prices [namq_gdp_c]
// LAST UPDATE: 15.03.13 20:50:29
// EXTRACTION DATE: 18.03.13 18:04:00
// SOURCE OF DATA: Eurostat

insheet using "namq_gdp_c_1_Data.csv", comma clear names
 
// Generate country variable
kountryadd "Germany (until 1990 former territory of the FRG)" to "Germany" add
kountry geo, from(other) stuck
ren _ISO3N_ country
kountry  country, from(iso3n) to(iso2c)
ren  _ISO2C_ country_str
replace country_str = "UK" if country_str == "GB"
list country_str geo
 
// Fix GDP variable
replace value = "" if value == ":"          // Fix missing data indicator 
replace value = subinstr(value, " ", "",.)  // Remove blank (thousands separator)
destring value, replace                     // Convert to numeric

// Drop unecessary variables and clean up data set
drop s_adj unit indic_na flagandfootnotes country
order country_str geo time value
sort geo time

// Generate monthly time variable
    
   // Generate quarterly time variable first
gen current_time_point_q = quarterly(time, "YQ")
    
   // Convert to months
gen current_time_point = mofd(dofq(current_time_point_q))
format current_time_point %tm
    
   // Carry forward first monthly observation
expand 3                                      // Create 2 duplicates of each case
by geo time, s: gen counter = _n - 1          // Count duplicates,
                                              // original    = 1 - 1 = 0
                                              // duplicate 1 = 2 - 1 = 1        
                                              // duplicate 2 = 3 - 1 = 2
replace current_time_point = current_time_point + counter
drop current_time_point_q counter time
 
// Cleaning up 
ren value gdp
label var gdp "GDP per capita (quarterly)"
keep country_str current_time_point gdp
save gdp.dta, replace
 
// Open master data set and merge
use unmepl.dta, clear 
merge 1:1 country_str current_time_point using gdp.dta 
keep if _merge == 3  
 
// Create semi-ugly plot for illustration 
keep if country_str == "NL" 
label var total_unempl_nsa "Unemployment rate (monthly)" 
 
tsset  current_time_point 
twoway (tsline gdp, yaxis(1)) /// 
       (tsline total_unempl_nsa, yaxis(2)) /// 
    , legend(ring(0)) /// 
      ytitle("Euros per inhabitant", axis(1)) /// 
   ytitle("Percentage points", axis(2)) /// 
   tlabel(2004m1 (6) 2009m12, angle(45)) /// 
   ttitle("Months")