Project

General

Profile

Support #2066 » 2066 - long format.txt

 
1
/*  Note: The data file produced will be at the individual level and cover all ages, irrespective of the variables requested. 
2
 In this current version of the tool, it is not possible to select variables by wave - though you may edit the code below to do so. */
3

    
4
/****************************************************************************************
5
* Sample Code for your request:  f52ac44389924ce3bd30cf41399e35fd       *
6
*****************************************************************************************/
7
clear all
8
set more off
9

    
10
// Replace "where" with the filepath of the working folder (where any temporary files created by this programme will be stored)   eg:  c:\ukhls\temp
11
cd "where" 
12

    
13
// Replace "where" with the folderpath where the data has been downloaded and unzipped   eg:   c:\ukhls_data\UKDA-6614-stata\stata\stata13_se\ukhls
14
global ukhls "where\UKDA-6614-stata\stata\stata13_se\ukhls"
15

    
16
// Replace "where" with the filepath of the folder where you want to store the final dataset produced by this programme.  eg:  c:\ukhls\results
17
global outputpath "where"
18

    
19
// The file produced by this programme will be named as below. If you want to change the name do it here.
20
local outputfilename "UKHLS_long_hij"
21

    
22
// By default the data will be extracted from the waves whose letter prefixes are written below, and merged. If you want to a different selection of waves, make the change here
23
local allWaves = "h i j"
24

    
25
// These variables from the indall files will be included. These include some key variables as determined by us PLUS any variables requested by you. 
26
local indallvars "age_dv country ethn_dv gor_dv hhsize hidp mastat_dv nchild_dv pidp pno psnen01_lw psnen01_xw psnen91_lw psnen91_xw psnen99_lw psnen99_xw psnenub_lw psnenub_xw psnenui_lw psnenui_xw psnenus_lw psnenus_xw psu racel_dv sex_dv strata urban_dv"
27

    
28
// These variables from the indresp files will be included. These include some key variables as determined by us PLUS any variables requested by you. 
29
local indvars "age_dv country ethn_dv gor_dv hhsize hhtype_dv hidp ind5mus_lw ind5mus_xw indbd91_lw indbdub_lw indin01_lw indin01_xw indin91_lw indin91_xw indin99_lw indin99_xw indinub_lw indinub_xw indinui_lw indinui_xw indinus_lw indinus_xw indns91_lw indnsub_lw indpxub_lw indpxub_xw indpxui_lw indpxui_xw indpxus_lw indpxus_xw indscub_lw indscub_xw indscui_lw indscui_xw indscus_lw indscus_xw jbbgy jbsat jbsemp jbstat jsboss jspart jssize mastat_dv nchild_dv pidp pno psu racel_dv sclfsato sex_dv strata tenure_dv ukborn urban_dv wkaut1 wkaut2 wkaut3 wkaut4 wkaut5"
30

    
31
// These variables from the child files will be included. These include some key variables as determined by us PLUS any variables requested by you. 
32
local chvars "age_dv chddvub_lw chddvub_xw chddvui_lw chddvui_xw country gor_dv hhsize hidp pidp pno psnen01_lw psnen91_lw psnenub_lw psnenub_xw psnenui_lw psnenui_xw psnenus_lw psnenus_xw psu sex_dv strata urban_dv"
33

    
34
// These variables from the hhresp files will be included. These include some key variables as determined by us PLUS any variables requested by you. 
35
local hhvars "country fihhmnnet1_dv gor_dv hhden01_xw hhden91_xw hhden99_xw hhdenub_xw hhdenui_xw hhdenus_xw hhsize hhtype_dv hidp ieqmoecd_dv nkids_dv psu strata tenure_dv urban_dv"
36

    
37
// These variables from the youth files will be included. These include some key variables as determined by us PLUS any variables requested by you. 
38
local youthvars "age_dv country ethn_dv gor_dv hidp pidp pno psu racel_dv sex_dv strata urban_dv ythscub_xw ythscui_xw ythscus_xw"
39

    
40

    
41
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
42
// Anything below this line should not be changed! Any changes to the selection of variables and waves, and location of folders, should be made above. //
43
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
// this program returns all variable names with the wave prefix
46
program define getVars, rclass
47
    version 14.0
48
	if ("`1'" != "") {
49
		local wavemyvars = " `1'"
50
		local wavemyvars = subinstr("`wavemyvars'"," "," `2'_",.)
51
		local wavemyvars = substr("`wavemyvars'",2,.)
52
	}
53
	else local wavemyvars = ""
54
	return local fixedVars "`wavemyvars'"
55
end
56

    
57
// this program to returns  which variables exist in this wave
58
program define getExistingVars, rclass
59
    version 14.0
60
	local all = ""
61
	foreach var in `1' {
62
		capture confirm variable `var'
63
		if !_rc {
64
			local all = "`all' `var'"
65
		}
66
	}
67
	return local existingVars "`all'"
68
end  
69

    
70
//loop through each wave
71
foreach wave in `allWaves' {
72
	// find the wave number
73
	local waveno=strpos("abcdefghijklmnopqrstuvwxyz","`wave'")
74

    
75
	// find the wave household vars
76
	getVars "`hhvars'" `wave'
77
	local wavehhvars = "`r(fixedVars)'"
78
	
79
	// find the wave individual vars
80
	getVars "`indvars'" `wave'
81
	local waveindvars = "`r(fixedVars)'"
82
	
83
	// find the wave all individual vars
84
	getVars "`indallvars'" `wave'
85
	local waveindallvars = "`r(fixedVars)'"
86
	
87
	// find the wave child vars
88
	getVars "`chvars'" `wave'
89
	local wavechvars = "`r(fixedVars)'"
90
	
91
	// find the wave youth vars
92
	getVars "`youthvars'" `wave'
93
	local waveyouthvars = "`r(fixedVars)'"
94
	
95
	// open the the household level file with the required variables
96
	use "$ukhls/`wave'_hhresp", clear
97
	getExistingVars "`wave'_hidp `wavehhvars'"
98
	keep `r(existingVars)'
99
	
100
	// if only household variables are required, skip this part and return all households
101
	if ("`indvars'" != "" || "`chvars'" != "" || "`youthvars'" != "") {
102
		// if any individual variable is required, first  merge INDALL keeping the pipd (and possibly some default variables?), so that other files can merge on it.
103
		merge 1:m `wave'_hidp using "$ukhls/`wave'_indall"
104
		drop _merge
105
		// drop loose households with no individuals
106
		drop if (pidp == .)
107
		
108
		// keep only variables that were requested and exist in this wave
109
		getExistingVars "pidp `wave'_hidp `wavehhvars' `waveindallvars'"
110
		keep `r(existingVars)'
111
		
112
		// add any requested individual variables
113
		if ("`indvars'" != "") {
114
			merge 1:1 pidp using "$ukhls/`wave'_indresp"
115
			drop _merge
116
			// keep only variables that were requested and exist in this wave
117
			getExistingVars "pidp `wave'_hidp `wavehhvars' `waveindvars' `waveyouthvars' `wavechvars' `waveindallvars'"
118
			keep `r(existingVars)'
119
		}
120
		// add any requested youth variables
121
		if ("`waveyouthvars'" != "") {
122
			merge 1:1 pidp using "$ukhls/`wave'_youth"
123
			drop _merge
124
			// keep only variables that were requested and exist in this wave
125
			getExistingVars "pidp `wave'_hidp `wavehhvars' `waveindvars' `waveyouthvars' `wavechvars' `waveindallvars'"
126
			keep `r(existingVars)'
127
		}
128
		// add any requested child variables
129
		if ("`wavechvars'" != "") {
130
			merge 1:1 pidp using "$ukhls/`wave'_child"
131
			drop _merge
132
			// keep only variables that were requested and exist in this wave
133
			getExistingVars "pidp `wave'_hidp `wavehhvars' `waveindvars' `waveyouthvars' `wavechvars' `waveindallvars'"
134
			keep `r(existingVars)'
135
		}
136
	}
137

    
138
	// create a wave variable
139
	gen wavename=`waveno'
140

    
141
	// drop the wave prefix from all variables
142
	rename `wave'_* *
143

    
144
	// save the file that was created
145
	save temp_`wave', replace
146
	
147
}
148

    
149
// open the file for the first wave (wave a_)
150
local firstWave = substr("`allWaves'", 1, 1)
151
use temp_`firstWave', clear
152

    
153
// loop through the remaining waves appending them in the long format
154
local remainingWaves = substr("`allWaves'", 3, .)
155

    
156
foreach w in `remainingWaves' {
157
	// append the files for the second wave onwards
158
	append using temp_`w'
159
}
160

    
161
// check how many observations are available from each wave
162
tab wavename
163

    
164
// move pidp to the beginning of the file
165
order pidp, first
166

    
167
// save the long file
168
save "$outputpath/`outputfilename'", replace
169

    
170
// erase temporary files
171
foreach w in `allWaves' {
172
	erase temp_`w'.dta
173
}
174
$syntax;
(2-2/6)