******************************************************************************** * 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 wide 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 (WIDE-FORMAT) // The following code uses the combined files. 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. // Specify variables you want to keep without wave prefix, i.e. hhrhid // rather that ahhrhid. If you want to include all variables, specify // "local varstokeep", i.e. without variables. local varstokeep hhrhid hhrpid hhpxid hhresp hhstate hhsos ancob losathl /// wsce wscei wscef wscme wscmei wscmef wscoe wscoei wscoef foreach w in a b c d e f g h i j k l m n { use "`origdatadir'\Combined_`w'140c.dta" // select variables needed if ("`varstokeep'"!="") { local tokeep // empty to keep list foreach var of local varstokeep { // loop over all selected variables capture confirm variable `w'`var' // check whether variable exists in current wave if (!_rc) local tokeep `tokeep' `w'`var' // mark for inclusion if variable exists } keep xwaveid `tokeep' // keep selected variables } // Save temporary data file tempfile tempdata_`w' save "`tempdata_`w''" } use "`origdatadir'\Master_n140c" keep xwaveid ivwptn // You can keep more variables if you want sort xwaveid foreach w in a b c d e f g h i j k l m n { merge 1:1 xwaveid using "`tempdata_`w''", nogen } save "`newdatadir'\wide-file-unbalanced", replace // SECTION 2: CREATING BALANCED DATASET (WIDE-FORMAT) // To create a balanced panel we can use the variable ivwptn which contains // the interview pattern for each person. use "`newdatadir'\wide-file-unbalanced" keep if ivwptn=="XXXXXXXXXXXXXX" save "`newdatadir'\wide-file-balanced", replace