Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ vendor/

# Ignore Notes and Old Files
Notes/
old/
old/
**/*.quarto_ipynb

.review/
44 changes: 44 additions & 0 deletions do_files/next_steps-data_discovery.do
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
* Load Packages
clear all
set more off

* lookfor
use "safeguarded_eul/ns8_2015_derived.dta", clear
lookfor "ghq"

* codebook
log using codebook.log, replace
codebook
log close

* Create a Lookup Table Across All Datasets
* ssc install filelist
filelist, pattern("*.dta")
keep if !strmatch(dirname, "*UKDS*")
tempfile file_list
save `file_list'

clear
tempfile all_vars
save `all_vars', emptyok

use `file_list', clear
local n_files = _N
forvalues i = 1/`n_files' {
use `file_list' in `i', clear
local filename = filename[1]
local dirname = dirname[1]
use "`dirname'/`filename'", clear
describe *, replace
gen dir = "`dirname'"
gen file = "`filename'"
append using `all_vars'
save `all_vars', replace
}

use `all_vars', clear
gen name_low = lower(name)
gen varlab_low = lower(varlab)
gen sweep = real(regexs(1)) if regexm(name_low, "^w([0-9])") // Extracts sweep from the variable name
list dir file sweep name varlab if strmatch(varlab_low, "*ghq*")
list dir file sweep name varlab if strmatch(varlab_low, "*ghq*") & strmatch(varlab_low, "*score*")
18 changes: 18 additions & 0 deletions do_files/next_steps-data_structures.do
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
clear all
set more off

* Missing Values
capture program drop negative_to_na
program define negative_to_na
syntax varlist(numeric)

foreach var of varlist `varlist' {
replace `var' = . if `var' < 0
}
end

use NSID W1alceverYP using "safeguarded_eul/wave_one_lsype_young_person_2020.dta", clear
numlabel, add // Adds numeric values to value labels
tabulate W1alceverYP, missing // Raw variable
negative_to_na W1alceverYP // Replaces negative values with missing (.)
tabulate W1alceverYP, missing // Cleaned variable
64 changes: 64 additions & 0 deletions do_files/next_steps-household_grid.do
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
clear all
set more off

* Finding Mother
use NSID W1HhID W1Sex W1R_1 W1EmpStat using "household_grids/wave_one_lsype_household_grid_file_2020.dta", clear
numlabel, add // Adds numeric values to value labels
tabulate W1Sex
tabulate W1R_1
keep if W1R_1 == 8 & W1Sex == 2
bysort NSID: gen n = _N
keep if n == 1
drop n
keep NSID W1HhID W1EmpStat
save "temp_mothers.dta", replace

* Mother's Employment
tabulate W1EmpStat
gen mother_works = .
replace mother_works = 1 if inrange(W1EmpStat, 1, 2)
replace mother_works = 0 if inrange(W1EmpStat, 3, 9)
keep NSID mother_works
tabulate mother_works, missing

* Main and Second Parent
use NSID W1HhID W1Sex W1R_1 W1mainres W1secores using "household_grids/wave_one_lsype_household_grid_file_2020.dta", clear
gen role = "Main parent" if W1HhID == W1mainres
replace role = "Second parent" if W1HhID == W1secores & role == ""
drop if role == ""
bysort role: tabulate W1R_1 W1Sex

* Changes in Main Parent Across Sweeps
use NSID W1HhID W1mainres using "household_grids/wave_one_lsype_household_grid_file_2020.dta", clear
keep if W1HhID == W1mainres
rename W1HhID w1_main_hhid
keep NSID w1_main_hhid
save "temp_w1_main.dta", replace

use NSID W2HhID W2mainres using "household_grids/wave_two_lsype_household_grid_file_2020.dta", clear
keep if W2HhID == W2mainres
rename W2HhID w2_main_hhid
keep NSID w2_main_hhid

merge 1:1 NSID using "temp_w1_main.dta", keep(match) nogenerate
gen same_person = w1_main_hhid == w2_main_hhid
tabulate same_person

* Relationships
use NSID W1HhID W1R_* using "household_grids/wave_one_lsype_household_grid_file_2020.dta", clear
list W1HhID W1R_1 W1R_2 W1R_3 if NSID == NSID[1]

* Reshape for Partner
reshape long W1R_, i(NSID W1HhID) j(W1HhID_alter)
keep if inlist(W1R_, 1, 2)
keep NSID W1HhID W1HhID_alter
rename W1HhID_alter partner_hhid
list in 1/10 // One row per couple: each relationship is only recorded for the person with the higher person number
save "temp_couples.dta", replace

* Add Row for Other Partner
rename (W1HhID partner_hhid) (partner_hhid W1HhID) // Swaps the person numbers of the two partners
append using "temp_couples.dta"
order NSID W1HhID partner_hhid
sort NSID W1HhID
list in 1/10
114 changes: 114 additions & 0 deletions do_files/next_steps-merging_across_sweeps.do
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
clear all
set more off

* Merging Across Sweeps
use NSID W2ghq12scr using "safeguarded_eul/wave_two_lsype_young_person_2020.dta", clear
save "temp_w2.dta", replace

use NSID W4ghq12scr using "safeguarded_eul/wave_four_lsype_young_person_2020.dta", clear
save "temp_w4.dta", replace

use "temp_w2.dta", clear
merge 1:1 NSID using "temp_w4.dta"
list NSID W2ghq12scr W4ghq12scr _merge in 1/10
// Use `drop if _merge == x` or run `merge ..., keep(x)` to keep a specific type of match/non-match.

* Appending Sweeps
use "temp_w2.dta", clear
rename W2* *
gen sweep = 2
save "temp_w2_long.dta", replace

use "temp_w4.dta", clear
rename W4* *
gen sweep = 4
save "temp_w4_long.dta", replace

use "temp_w2_long.dta", clear
append using "temp_w4_long.dta"
sort NSID sweep
list NSID sweep ghq12scr in 1/10

* Add Missing Rows
fillin NSID sweep
list * in 1/10

* Programmatic Merging
* Lookup lists: the nth word of each list gives the sweep, file and variable to use
global sweeps 2 4 8 9
global files safeguarded_eul/wave_two_lsype_young_person_2020.dta safeguarded_eul/wave_four_lsype_young_person_2020.dta safeguarded_eul/ns8_2015_derived.dta safeguarded_eul/ns9_2022_derived_variables.dta
global ghq_vars W2ghq12scr W4ghq12scr W8DGHQSC W9DGHQSC

capture program drop load_ghq_wide
program define load_ghq_wide
args i

local file : word `i' of ${files}
local ghq_var : word `i' of ${ghq_vars}

use NSID `ghq_var' using "`file'", clear
end

load_ghq_wide 3
list * in 1/10

load_ghq_wide 1

tempfile next_merge
forvalues i = 2/4 {
preserve
load_ghq_wide `i'
save "`next_merge'", replace
restore
merge 1:1 NSID using "`next_merge'", nogenerate
}

sort NSID
list * in 1/10
save "temp_ghq_wide.dta", replace

* Programmatic Appending
capture program drop load_ghq_long
program define load_ghq_long
args i

load_ghq_wide `i'
local ghq_var : word `i' of ${ghq_vars}
local sweep : word `i' of ${sweeps}
rename `ghq_var' ghq12
label values ghq12 . // Removes value labels, which differ between sweeps
gen sweep = `sweep'
end

load_ghq_long 1

tempfile next_append
forvalues i = 2/4 {
preserve
load_ghq_long `i'
save "`next_append'", replace
restore
append using "`next_append'"
}

sort NSID sweep
list * in 1/10

* Replace Negative Values with Missing
capture program drop negative_to_na
program define negative_to_na
syntax varlist(numeric)

foreach var of varlist `varlist' {
replace `var' = . if `var' < 0
}
end

negative_to_na ghq12
list * in 1/10

* Defining the Sample with the Longitudinal File
use NSID MAINBOOST W?OUTCOME using "safeguarded_eul/ns9_2022_longitudinal_file.dta", clear
tabulate MAINBOOST
merge 1:1 NSID using "temp_ghq_wide.dta", keep(master match) nogenerate
list * in 1/10
49 changes: 49 additions & 0 deletions do_files/next_steps-merging_within_sweep.do
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
clear all
set more off

* Sex and Attitude to School
use NSID W1sexYP W1yschat1 using "safeguarded_eul/wave_one_lsype_young_person_2020.dta", clear
rename (W1sexYP W1yschat1) (sex school_attitude)
save "df_yp.dta", replace

* Tenure
use NSID W1hous12HH using "safeguarded_eul/wave_one_lsype_family_background_2020.dta", clear
rename W1hous12HH tenure
save "df_tenure.dta", replace

* Family NS-SEC
use NSID W1nssecMP W1nssecSP using "safeguarded_eul/wave_one_lsype_family_background_2020.dta", clear
replace W1nssecMP = . if !inrange(W1nssecMP, 1, 8) // Values outside 1-8 denote various forms of missingness
replace W1nssecSP = . if !inrange(W1nssecSP, 1, 8)
egen family_nssec = rowmin(W1nssecMP W1nssecSP) // Lowest value of the two variables in each row
keep NSID family_nssec
save "df_nssec.dta", replace

* Mother's NS-SEC
use NSID W1mainres W1secores W1nssecMP W1nssecSP using "safeguarded_eul/wave_one_lsype_family_background_2020.dta", clear
rename (W1mainres W1secores) (W1HhIDMP W1HhIDSP)
reshape long W1HhID W1nssec, i(NSID) j(parent) string // One row per parent
save "temp_parent.dta", replace

use NSID W1HhID W1Sex W1R_1 using "household_grids/wave_one_lsype_household_grid_file_2020.dta", clear
joinby NSID W1HhID using "temp_parent.dta" // Keeps matched rows only
keep if W1R_1 == 8 & W1Sex == 2
bysort NSID: gen n = _N
keep if n == 1
gen mother_nssec = W1nssec if inrange(W1nssec, 1, 8)
keep NSID mother_nssec
save "df_mother_nssec.dta", replace

* Number of Siblings
use NSID W1HhID W1R_1 using "household_grids/wave_one_lsype_household_grid_file_2020.dta", clear
gen sibling = inrange(W1R_1, 13, 17)
collapse (sum) n_siblings = sibling, by(NSID)
save "df_siblings.dta", replace

* Merge All
use "df_yp.dta", clear
merge 1:1 NSID using "df_tenure.dta", keep(master match) nogenerate
merge 1:1 NSID using "df_nssec.dta", keep(master match) nogenerate
merge 1:1 NSID using "df_mother_nssec.dta", keep(master match) nogenerate
merge 1:1 NSID using "df_siblings.dta", keep(master match) nogenerate
list in 1/10
43 changes: 43 additions & 0 deletions do_files/next_steps-reshape_long_wide.do
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
clear all
set more off

* Load and Merge
use NSID W8HEIGHT W8WEIGHT using "safeguarded_eul/ns8_2015_main_interview.dta", clear
save "temp_w8.dta", replace

use NSID W9HEIGHT W9WEIGHT using "safeguarded_eul/ns9_2022_main_interview.dta", clear
merge 1:1 NSID using "temp_w8.dta", nogenerate
order NSID W8HEIGHT W8WEIGHT W9HEIGHT W9WEIGHT
sort NSID
list in 1/10

* Reshape Raw Wide to Long
rename (W#HEIGHT W#WEIGHT) (HEIGHT# WEIGHT#) // reshape requires the sweep number to be at the end of the name
reshape long HEIGHT WEIGHT, i(NSID) j(sweep)
list in 1/10

* Reshape Raw Long to Wide
reshape wide HEIGHT WEIGHT, i(NSID) j(sweep)
rename (HEIGHT# WEIGHT#) (W#HEIGHT W#WEIGHT)
list in 1/10

* Cleaned Data
global ages 14 15 16 17 18 19 20 25 32

rename (W#HEIGHT W#WEIGHT) (HEIGHT# WEIGHT#)
reshape long HEIGHT WEIGHT, i(NSID) j(sweep)
gen age = real(word("${ages}", sweep))
gen height = HEIGHT if HEIGHT > 0
gen weight = WEIGHT if WEIGHT > 0
keep NSID age height weight
list in 1/10

* Reshape Clean Long to Wide
reshape wide height weight, i(NSID) j(age)
rename (height# weight#) (height_# weight_#)
list in 1/10

* Reshape Clean Wide to Long
reshape long height_ weight_, i(NSID) j(age)
rename (height_ weight_) (height weight)
list in 1/10
Binary file added docs/images/next_steps-data_discovery.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading