*==============================================================* * Ex0.do * *==============================================================* **************************************************************** * This do file includes a number of lines that say: * * STOP HERE AND THINK!! * * When Stata reaches the first of these, it will not * * reconise it as a valid command and will come to a juddering * * halt. At this point, there will be a few questions for you * * to think about. When you have answered them, delete the * * offending line and re-run the do-file. It will crash at the * * next point where you have some questions to think about * **************************************************************** *************************************************************** * In this exercise: * * Use annual HILDA data to create a longitudinal data set * * in long form * * - unbalanced long file (longperson_unbal.dta) * * We also provided some commented out sample code to create * * - balanded long file (longperson_bal.dta) * * - unbalanced wide file (wideperson_unbal.dta) * * - balanced wide file (wideperson_bal.dta) * *************************************************************** *************************************************************** * ACKNOWLEDGEMENT: * * This training material (ex0-ex3) uses unit record data from * * the Household, Income and Labour Dynamics in Australia * * (HILDA) Survey. The HILDA Project was initiated and is * * funded by the Australian Government Department of Social * * Services (DSS) and is managed by the Melbourne Institute of * * Applied Economic and Social Research (Melbourne Institute). * * The findings and views reported in this training, however, * * are those of the trainer and should not be attributed to * * either DSS or the Melbourne Institute. * *************************************************************** version 13 clear all set more off set matsize 800 set maxvar 32500 capture log close local working "P:\working" local origdata "E:\HILDA\STATA 130c" local versnum "130c" local wavestr "a b c d e f g h i j k l m" local maxwave "m" local alphabet "abcdefghijklmnopqrstuvwxyz" local fullivw "XXXXXXXXXXXXX" log using "`working'\ex0.log", replace //change to the prefered path to save the log file ********************************************************** * Use the combined person file for each wave and * * restrict the variables to those of interest * * Save dataset with the wave prefixes for variables and * * save one with the trimmed variable names * ********************************************************** foreach w in `wavestr' { *define the variables of interest that are common over time and then the variables specific to each wave local commonvar1 "xwaveid `w'hhrhid `w'hhpxid `w'hhresp `w'hhstate `w'hhsos `w'ancob `w'losathl `w'hgsex `w'hgage `w'edhigh1 `w'mrcurr `w'hglth `w'jbhruc " local commonvar2 "`w'esdtl `w'esbrd `w'jbmhruc `w'jbn `w'ehtjbyr `w'jbempt `w'jbmcnt `w'jbmo62 `w'jbmi62 `w'jbmsall `w'jbemlyr `w'jbemlwk `w'jboccyr `w'jboccwk" local commonvar3 "`w'fmfo62 `w'fmmo62 `w'wsce `w'wscei `w'wscef `w'wscme `w'wscmei `w'wscmef `w'wscoe `w'wscoei `w'wscoef `w'wsfe `w'wsfei `w'wsfef" if "`w'"=="a" { local specvar "`w'hstenur `w'jbmunio" } else if "`w'">="i" { local specvar "`w'hstenr `w'jbtu `w'jbou `w'pjmsemp" } else { local specvar "`w'hstenr `w'jbmunio `w'pjmsemp" } display "wave: `w'" display "commonvar1: `commonvar1'" display "commonvar2: `commonvar2'" display "commonvar3: `commonvar3'" display "specvar: `specvar'" use `commonvar1' `commonvar2' `commonvar3' `specvar' using "`origdata'\combined_`w'`versnum'.dta", clear gen waveletter="`w'" gen wave=strpos("`alphabet'",waveletter) tab wave drop waveletter * make the variables that differ over time consistent (housing tenure and union membership) if "`w'">="i" { recode `w'hstenr (1=1) (2/3=2) (4=3), gen(`w'hstenur) drop `w'hstenr * Combine jbtu and jbou to jbmunio tab `w'jbtu `w'jbou gen `w'jbmunio=`w'jbtu replace `w'jbmunio=1 if `w'jbou==1 drop `w'jbtu `w'jbou } else if "`w'"!="a" { recode `w'hstenr (1=1) (2/3=2) (4=3), gen(`w'hstenur) drop `w'hstenr } save "`working'\wave_`w'.dta", replace renpfix `w' save "`working'\wave_`w'_trim.dta", replace } ********************************************************** * Create unbalanced long file * ********************************************************** foreach w in `wavestr' { if "`w'"=="a" { use "`working'\wave_`w'_trim.dta", clear } else { append using "`working'\wave_`w'_trim.dta" } } sort xwaveid wave * Add on useful masterfile information merge m:1 xwaveid using "`origdata'\Master_`maxwave'`versnum'.dta", /// keepusing(xwaveid ivwptn enumptn hhsm sex) drop _merge * Keep observations for each person in the waves interviewed keep if substr(ivwptn,wave,1)=="X" * Identify topup sample gen topup=0 replace topup=1 if wave>=11 & (substr(hhrhid,1,1)=="8" | substr(hhrhid,1,1)=="9") tab wave topup * Save the file using saveold so that it can be used in Stata 12 if necessary saveold "`working'\longperson_unbal.dta", replace ************************************************************** * Questions to think about: * * (1) Tabulate a few variables (e.g. hstenur, jbmunio, sex) * * against wave to show trends over time * * (2) Use the browse command (after sorting by xwaveid and * * wave to look at the structure of the data * ************************************************************** *STOP HERE AND THINK!! ********************************************************** * Delete temporary files created * ********************************************************** foreach w in `wavestr' { erase "`working'\wave_`w'.dta" erase "`working'\wave_`w'_trim.dta" } set more on log close