Skip to content

Code testing

Example 5: Merging individual files across waves into long format 

To match individual level files across two waves into a long format do the following (for more waves add wave specific prefix in the foreach statement): 

foreach w in a b { 
// open the individual level file
use pidp `w’_jbhas using `w’_indresp_ip, clear
// drop the wave prefix from all variables
renpfix `w’_
// create a wave variable
gen wave=strpos(“ab”, “`w’”)
// save one file for each wave
save temp`w’, replace
}

// open the file for the first wave (wave a_)
use tempa, clear
foreach w in b {
// append the files for second wave onwards
append using temp`w’
}

// save the long file
save final5, replace
// erase temporary files
foreach w in a b {
erase temp`w’.dta
}

Example 6: Merging individual files across waves into wide format 

The following code shows how toTo match individual level files across two waves into a wide format. do the following (for The code can be adapted to handle more waves by adding wave specific prefixes in the foreach statement): 

use pidp a_jbhas using a_indresp_ip, clear 
sort pidp
save temp, replace
foreach w in b {
use pidp `w’_jbhas using `w’_indresp_ip, clear
sort pidp
merge 1:1 pidp using temp
drop _merge
sort pidp
save temp, replace
}
save final6, replace
erase temp.dta

Email newsletter

Sign up to our newsletter