******************************************************************************** * Created by: Claire Sun * * Updated by: Nicole Watson & Markus Hahn * * * * Purpose of the progam: * * ====================== * * This program creates an unbalanced and a balanced longitudinal data file, * * using the the combined files. The new data files are in Stata's long format. * * Please note that we use 'tempfile tempdata_w' to create a macro (local) that * * allows us to access a temporary data file which will be automatically * * deleted when this do-file ends. * ******************************************************************************** clear set memory 1g // Specify directories (use "." to point to current directory) local origdatadir "C:\orig-data" // Location of original HILDA data files local newdatadir "C:\new-data" // Location to which to write new data files // SECTION 1: CREATING AN UNBALANCED DATASET (LONG-FORMAT) // The following code uses the combined files. Since we want the final // file to be in long format, we need to remove the alphabetic wave // indicator from the variable names and a create a variable containing // the numeric wave indicator. For that we use a loop to make things easier. // Usually not all variables are required. Therefore, in the code we just // select a few before saving the temporary data file. local varstokeep hwhmhl hhrhid hhrpid hhpxid hhresp hhstate hhsos ancob losathl /// wsce wscei wscef wscme wscmei wscmef wscoe wscoei wscoef local i = 0 foreach w in a b c d e f g h i j k l m n { use "`origdatadir'\Combined_`w'140c" renpfix `w' // Strip off wave prefix local i = `i'+1 // Increase (wave) counter by 1 gen wave = `i' // Create wave indicator (1, 2, ...) // select variables needed if ("`varstokeep'"!="") { local tokeep // empty to keep list foreach var of local varstokeep { // loop over all selected variables capture confirm variable `var' // check whether variable exists in current wave if (!_rc) local tokeep `tokeep' `var' // mark for inclusion if variable exists } keep xwaveid wave `tokeep' // keep selected variables } // Save temporary data file tempfile tempdata_`w' save "`tempdata_`w''" } // The following code appends the temporary data files for each wave to create // an unbalanced panel. clear foreach w in a b c d e f g h i j k l m n { append using "`tempdata_`w''" } order xwaveid wave sort xwaveid wave // Save new data set save "`newdatadir'\long-file-unbalanced", replace // SECTION 2: CREATING BALANCED DATASET (LONG-FORMAT) // We prepare the master file because we want to keep some useful information. // To create a balanced panel we can use the variable ivwptn which contains // the interview pattern for each person. If you want keep more variables from // the master file, you need to change to following code. use "`origdatadir'\Master_n140c" keep xwaveid ivwptn // You can keep more variables if you want merge 1:m xwaveid using "`newdatadir'\long-file-unbalanced", nogen keep if ivwptn=="XXXXXXXXXXXXXX" // only keep persons responding in every wave order xwaveid wave sort xwaveid wave // Save new data set save "`newdatadir'\long-file-balanced", replace