From a8bcd316b6a6d9cbe67c641d67048a5c7a97fedb Mon Sep 17 00:00:00 2001 From: Liam Wright <45944168+ljwright@users.noreply.github.com> Date: Fri, 27 Nov 2026 09:02:43 +0000 Subject: [PATCH] Tidy the Next Steps Stata do-files - Data discovery: build the lookup table from the three Next Steps subfolders only, so other .dta files under the working directory (such as datasets saved by the other scripts) are not listed. Put the temporary-file macros in double quotes, so the script also works when the path to Stata's temporary folder contains a space. - Household grid: remove a save of temp_mothers.dta that nothing used. - Combining data across sweeps: correct a comment (keep if, not drop if). - Reshape: reword the comment on the rename step, and create height and weight with gen double so no precision is lost. Co-Authored-By: Claude Fable 5.2 --- do_files/next_steps-data_discovery.do | 16 ++++++++-------- do_files/next_steps-household_grid.do | 1 - do_files/next_steps-merging_across_sweeps.do | 2 +- do_files/next_steps-reshape_long_wide.do | 6 +++--- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/do_files/next_steps-data_discovery.do b/do_files/next_steps-data_discovery.do index 8d75f81..a55c6e2 100644 --- a/do_files/next_steps-data_discovery.do +++ b/do_files/next_steps-data_discovery.do @@ -14,29 +14,29 @@ log close * Create a Lookup Table Across All Datasets * ssc install filelist filelist, pattern("*.dta") -keep if !strmatch(dirname, "*UKDS*") +keep if inlist(dirname, "./safeguarded_eul", "./household_grids", "./activity_histories") // Keeps files in the three Next Steps subfolders tempfile file_list -save `file_list' +save "`file_list'" clear tempfile all_vars -save `all_vars', emptyok +save "`all_vars'", emptyok -use `file_list', clear +use "`file_list'", clear local n_files = _N forvalues i = 1/`n_files' { - use `file_list' in `i', clear + 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 + append using "`all_vars'" + save "`all_vars'", replace } -use `all_vars', clear +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 diff --git a/do_files/next_steps-household_grid.do b/do_files/next_steps-household_grid.do index 8e9f425..39b5462 100644 --- a/do_files/next_steps-household_grid.do +++ b/do_files/next_steps-household_grid.do @@ -11,7 +11,6 @@ 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 diff --git a/do_files/next_steps-merging_across_sweeps.do b/do_files/next_steps-merging_across_sweeps.do index 93a4ed5..4f49c84 100644 --- a/do_files/next_steps-merging_across_sweeps.do +++ b/do_files/next_steps-merging_across_sweeps.do @@ -11,7 +11,7 @@ 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. +// Use `keep if _merge == x` or run `merge ..., keep(x)` to keep a specific type of match/non-match. * Appending Sweeps use "temp_w2.dta", clear diff --git a/do_files/next_steps-reshape_long_wide.do b/do_files/next_steps-reshape_long_wide.do index d0b9d27..251f994 100644 --- a/do_files/next_steps-reshape_long_wide.do +++ b/do_files/next_steps-reshape_long_wide.do @@ -12,7 +12,7 @@ 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 +rename (W#HEIGHT W#WEIGHT) (HEIGHT# WEIGHT#) // Moves the sweep number to the end of the name, where reshape expects it by default reshape long HEIGHT WEIGHT, i(NSID) j(sweep) list in 1/10 @@ -27,8 +27,8 @@ 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 +gen double height = HEIGHT if HEIGHT > 0 // double keeps the full precision of the raw values +gen double weight = WEIGHT if WEIGHT > 0 keep NSID age height weight list in 1/10