From 5734724608ecc639a5fc0f3098815fa2ae69f70f Mon Sep 17 00:00:00 2001 From: Arthur Shaw <47256431+arthur-shaw@users.noreply.github.com> Date: Mon, 8 Jan 2024 20:30:34 -0500 Subject: [PATCH 1/3] Flag variables without variable labels Closes #2 --- run-adodown-util.do | 5 ++ src/ado/lbl_assert_var_have_lbl.ado | 33 ++++++++ src/ado/lbl_list_no_var_lbl.ado | 37 +++++++++ src/labeller.pkg | 4 + src/mdhlp/lbl_assert_var_have_lbl.md | 47 +++++++++++ src/mdhlp/lbl_list_no_var_lbl.md | 47 +++++++++++ src/tests/test-lbl_list_no_var_lbl.do | 108 ++++++++++++++++++++++++++ 7 files changed, 281 insertions(+) create mode 100644 src/ado/lbl_assert_var_have_lbl.ado create mode 100644 src/ado/lbl_list_no_var_lbl.ado create mode 100644 src/mdhlp/lbl_assert_var_have_lbl.md create mode 100644 src/mdhlp/lbl_list_no_var_lbl.md create mode 100644 src/tests/test-lbl_list_no_var_lbl.do diff --git a/run-adodown-util.do b/run-adodown-util.do index c333e90..8a650f0 100644 --- a/run-adodown-util.do +++ b/run-adodown-util.do @@ -2,7 +2,11 @@ if "`c(username)'" == "wb462869" { global clone "C:/Users/wb462869/github/labeller" } + else if "`c(username)'" == "wb393438" { + global clone "C:\Users\wb393438\stata_funs\labeller\labeller" + } + /* ad_setup, adf("${clone}") /// name("labeller") /// description("A packge with utility commands related to lables. Particularly, but not exclusively, in relation to data sets collected using SurveySolutions.") /// @@ -10,6 +14,7 @@ contact("lsms@worldbank.org") /// url("https://github.com/lsms-worldbank/labeller") /// github + */ ad_sthlp , adf("${clone}") diff --git a/src/ado/lbl_assert_var_have_lbl.ado b/src/ado/lbl_assert_var_have_lbl.ado new file mode 100644 index 0000000..3d91e95 --- /dev/null +++ b/src/ado/lbl_assert_var_have_lbl.ado @@ -0,0 +1,33 @@ +*! version XX XXXXXXXXX ADAUTHORNAME ADCONTACTINFO + +cap program drop lbl_assert_var_have_lbl + program define lbl_assert_var_have_lbl, rclass + + version 13 + + syntax [varlist] + + qui { + + * look for variables without a label + lbl_list_no_var_lbl `varlist' + local any_wo_var_lbl = `r(count_matches)' > 0 + local which_no_var_lbl = "`r(varlist)'" + + * return results + return local varlist = "`which_no_var_lbl'" + return local any_matches = "`any_wo_var_lbl'" + + * if any variables without labels found, message and error + if (`any_wo_var_lbl' == 1) { + di as error "Variables found without a variable label :", + di as error "`which_no_var_lbl'" + error 9 + } + else { + di as result "No variables found with a variable label." + } + + } + +end diff --git a/src/ado/lbl_list_no_var_lbl.ado b/src/ado/lbl_list_no_var_lbl.ado new file mode 100644 index 0000000..e3bad04 --- /dev/null +++ b/src/ado/lbl_list_no_var_lbl.ado @@ -0,0 +1,37 @@ +*! version XX XXXXXXXXX ADAUTHORNAME ADCONTACTINFO + +cap program drop lbl_list_no_var_lbl + program define lbl_list_no_var_lbl, rclass + + version 13 + + syntax [varlist] + + qui { + + * get all variables that lack a variable label + ds `varlist', not(varlabel) + local vars = "`r(varlist)'" + + * reset varlist to avoid collision with varlist in syntax + local varlist "" + + * compute the number of matches + local n_matches : list sizeof vars + + * return the varlist and count of matches + return local varlist = "`vars'" + return local count_matches = "`n_matches'" + + * message about outcome + if (`n_matches' >= 1) { + noi di as result "Variables found without a variable label :" + noi di as result "`vars'" + } + else if (`n_matches' == 0) { + noi di as result "No variables found without a label" + } + + } + +end diff --git a/src/labeller.pkg b/src/labeller.pkg index a26fe12..e5e5356 100644 --- a/src/labeller.pkg +++ b/src/labeller.pkg @@ -20,9 +20,13 @@ d d Distribution-Date: 20231109 d *** adofiles +f ado/lbl_assert_var_have_lbl.ado +f ado/lbl_list_no_var_lbl.ado f ado/labeller.ado *** helpfiles +f sthlp/lbl_assert_var_have_lbl.sthlp +f sthlp/lbl_list_no_var_lbl.sthlp f sthlp/labeller.sthlp *** ancillaryfiles diff --git a/src/mdhlp/lbl_assert_var_have_lbl.md b/src/mdhlp/lbl_assert_var_have_lbl.md new file mode 100644 index 0000000..339597b --- /dev/null +++ b/src/mdhlp/lbl_assert_var_have_lbl.md @@ -0,0 +1,47 @@ +# Title + +__lbl_assert_var_have_lbl__ - List variables without a variable label. + +# Syntax + +__lbl_assert_var_have_lbl__ [varlist] + +# Description + +For small data sets, visual inspection can identify variables without a variable label. For larger data sets (or repeat encounters with data sets), it is better to have a tool variables, if any, that remain without a variable label. + +This command does that. If any variables without variable labels are found, it returns and error and lists which variables are missing variable labels. If all variables have variable labels, it reports this fact. In this way, the user knows whether action is needed, and for which variables. + +By default, the command combs over all variables in memory when compiling a list. By providing a variable list, users can restrict the scope of the search to the user-specified range. + +# Examples + +``` +* create set of variables +gen var1 = . +gen var2 = . +gen var3 = . +gen var4 = . + +* apply variable labels to only some variables +label variable var1 "Some label" +label variable var4 "Another label" + +* assert that all variables have variable labels, globally +lbl_assert_var_have_lbl + +* assert that all variables have variable labels, in the varlist +lbl_assert_var_have_lbl var3 - var4 +``` + +# Feedback, bug reports and contributions + +Read more about the commands in this package at https://github.com/lsms-worldbank/labeller. + +Please provide any feedback by opening an issue at https://github.com/lsms-worldbank/labeller/issues. + +PRs with suggestions for improvements are also greatly appreciated. + +# Authors + +LSMS Team, The World Bank lsms@worldbank.org diff --git a/src/mdhlp/lbl_list_no_var_lbl.md b/src/mdhlp/lbl_list_no_var_lbl.md new file mode 100644 index 0000000..70a801a --- /dev/null +++ b/src/mdhlp/lbl_list_no_var_lbl.md @@ -0,0 +1,47 @@ +# Title + +__lbl_list_no_var_lbl__ - List variables without a variable label. + +# Syntax + +__lbl_list_no_var_lbl__ [varlist] + +# Description + +For small data sets, visual inspection can identify variables without a variable label. For larger data sets (or repeat encounters with data sets), it is better to have a tool variables, if any, that remain without a variable label. + +This command does that. If any variables without variable labels are found, it lists them. If all variables have variable labels, it says so. That way, the user knows whether action is needed, and for which variables. + +By default, the command combs over all variables in memory when compiling a list. By providing a variable list, users can restrict the scope of the search to the user-specified range. + +# Examples + +``` +* create set of variables +gen var1 = . +gen var2 = . +gen var3 = . +gen var4 = . + +* apply variable labels to only some variables +label variable var1 "Some label" +label variable var4 "Another label" + +* list variables without variable labels globally +lbl_list_no_var_lbl + +* list variables without a label in the varlist +lbl_list_no_var_lbl var3 - var4 +``` + +# Feedback, bug reports and contributions + +Read more about the commands in this package at https://github.com/lsms-worldbank/labeller. + +Please provide any feedback by opening an issue at https://github.com/lsms-worldbank/labeller/issues. + +PRs with suggestions for improvements are also greatly appreciated. + +# Authors + +LSMS Team, The World Bank lsms@worldbank.org diff --git a/src/tests/test-lbl_list_no_var_lbl.do b/src/tests/test-lbl_list_no_var_lbl.do new file mode 100644 index 0000000..8753d5f --- /dev/null +++ b/src/tests/test-lbl_list_no_var_lbl.do @@ -0,0 +1,108 @@ +* Kristoffer's root path +if "`c(username)'" == "wb462869" { + global clone "C:\Users\wb462869\github\labeller" +} +else if "`c(username)'" == "wb393438" { + global clone "C:\Users\wb393438\stata_funs\labeller\labeller" +} + +* Set global to ado_fldr +global src_fldr "${clone}/src" +global test_fldr "${src_fldr}/tests" +global data_fldr "${test_fldr}/testdata" + +* Install the version of this package in +* the plus-ado folder in the test folder +cap mkdir "${test_fldr}/plus-ado" +repado , adopath("${test_fldr}/plus-ado") mode(strict) + +cap net uninstall labeller +net install labeller, from("${src_fldr}") replace + +* output version of this package +labeller + +* ============================================================================== +* Setup +* ============================================================================== + +* create set of variables +gen var1 = . +gen var2 = . +gen var3 = . +gen var4 = . + +* apply variable labels to only some variables +label variable var1 "Some label" +label variable var4 "Another label" + +* ============================================================================== +* List +* ============================================================================== + +* ------------------------------------------------------------------------------ +* Lists variables without labels globally +* ------------------------------------------------------------------------------ + +* list variables without variable labels globally +lbl_list_no_var_lbl +local vars_wo_lbl = r(varlist) +local vars_wo_lbl : list clean vars_wo_lbl + +* test +capture assert "`vars_wo_lbl'" == "var2 var3" +di as result "lbl_list_no_var_lbl lists variables without labels globally" +if _rc != 0 { + di as error "❌ Test failed" + error 0 +} +else { + di as result "✅ Test passed" +} + +* ------------------------------------------------------------------------------ +* lists variables with without labels in varlist +* ------------------------------------------------------------------------------ + +* list variables without a label in the varlist +lbl_list_no_var_lbl var3 - var4 +local vars_wo_lbl_in_varlist = r(varlist) +local vars_wo_lbl_in_varlist : list clean vars_wo_lbl_in_varlist + +* test +capture assert "`vars_wo_lbl_in_varlist'" == "var3" +di as result "vars_wo_lbl_in_varlist lists variables with without labels in varlist" +if _rc != 0 { + di as error "❌ test failed" + error 0 +} +else { + di as result "✅ test passed" +} + +* ============================================================================== +* Assert +* ============================================================================== + +* ------------------------------------------------------------------------------ +* Assertion fails if violations present +* ------------------------------------------------------------------------------ + +* assert that all variables have a label +capture lbl_assert_var_have_lbl + +* test +di as result "lbl_assert_var_have_lbl errors if any var is without a label" +if _rc == 0 { + di as error "❌ Test failed" + error 0 +} +else { + di as result "✅ Test passed" +} + +* ============================================================================== +* Teardown +* ============================================================================== + +drop var1 - var4 From 89abf45d5886619a3e934f2bafc35fb5b46dcadc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristoffer=20Bj=C3=A4rkefur?= Date: Tue, 9 Jan 2024 05:16:27 -0500 Subject: [PATCH 2/3] small fixes --- src/ado/lbl_assert_var_have_lbl.ado | 14 +++++++------- src/ado/lbl_list_no_var_lbl.ado | 12 ++++++------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/ado/lbl_assert_var_have_lbl.ado b/src/ado/lbl_assert_var_have_lbl.ado index 3d91e95..55c38d6 100644 --- a/src/ado/lbl_assert_var_have_lbl.ado +++ b/src/ado/lbl_assert_var_have_lbl.ado @@ -11,21 +11,21 @@ cap program drop lbl_assert_var_have_lbl * look for variables without a label lbl_list_no_var_lbl `varlist' - local any_wo_var_lbl = `r(count_matches)' > 0 - local which_no_var_lbl = "`r(varlist)'" + local any_wo_var_lbl = (`r(count_matches)' > 0) + local which_no_var_lbl "`r(varlist)'" * return results - return local varlist = "`which_no_var_lbl'" - return local any_matches = "`any_wo_var_lbl'" + return local varlist "`which_no_var_lbl'" + return local any_matches "`any_wo_var_lbl'" * if any variables without labels found, message and error if (`any_wo_var_lbl' == 1) { - di as error "Variables found without a variable label :", - di as error "`which_no_var_lbl'" + di as error "{pstd}Variables found without a variable label :{p_end}", + di as error "{phang}`which_no_var_lbl'{p_end}" error 9 } else { - di as result "No variables found with a variable label." + di as result "{pstd}No variables found with a variable label.{p_end}" } } diff --git a/src/ado/lbl_list_no_var_lbl.ado b/src/ado/lbl_list_no_var_lbl.ado index e3bad04..4f490d5 100644 --- a/src/ado/lbl_list_no_var_lbl.ado +++ b/src/ado/lbl_list_no_var_lbl.ado @@ -11,7 +11,7 @@ cap program drop lbl_list_no_var_lbl * get all variables that lack a variable label ds `varlist', not(varlabel) - local vars = "`r(varlist)'" + local vars "`r(varlist)'" * reset varlist to avoid collision with varlist in syntax local varlist "" @@ -20,16 +20,16 @@ cap program drop lbl_list_no_var_lbl local n_matches : list sizeof vars * return the varlist and count of matches - return local varlist = "`vars'" - return local count_matches = "`n_matches'" + return local varlist "`vars'" + return local count_matches "`n_matches'" * message about outcome if (`n_matches' >= 1) { - noi di as result "Variables found without a variable label :" - noi di as result "`vars'" + noi di as result "{pstd}Variables found without a variable label :{p_end}" + noi di as result "{phang}`vars'{p_end}" } else if (`n_matches' == 0) { - noi di as result "No variables found without a label" + noi di as result "{pstd}No variables found without a label{p_end}" } } From 3fc2011c3950fd1e57f747450fe8be4d781d63bc Mon Sep 17 00:00:00 2001 From: Arthur Shaw <47256431+arthur-shaw@users.noreply.github.com> Date: Wed, 10 Jan 2024 10:35:25 -0500 Subject: [PATCH 3/3] Return number of matches as well --- src/ado/lbl_assert_var_have_lbl.ado | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ado/lbl_assert_var_have_lbl.ado b/src/ado/lbl_assert_var_have_lbl.ado index 55c38d6..6b4bc1c 100644 --- a/src/ado/lbl_assert_var_have_lbl.ado +++ b/src/ado/lbl_assert_var_have_lbl.ado @@ -12,11 +12,13 @@ cap program drop lbl_assert_var_have_lbl * look for variables without a label lbl_list_no_var_lbl `varlist' local any_wo_var_lbl = (`r(count_matches)' > 0) + local n_wo_var_lbl = "`r(count_matches)'" local which_no_var_lbl "`r(varlist)'" * return results return local varlist "`which_no_var_lbl'" return local any_matches "`any_wo_var_lbl'" + return local count_matches "`n_wo_var_lbl'" * if any variables without labels found, message and error if (`any_wo_var_lbl' == 1) {