From 247704c9bfa22ecddaee3324ba2edfaa2916cd48 Mon Sep 17 00:00:00 2001 From: Dominic Ricottone Date: Thu, 1 Jul 2021 16:14:35 -0400 Subject: [PATCH] Initial commit --- cf_vallabels.ado | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ cf_varlabels.ado | 43 ++++++++++++++++++++++++++++++++++++++++ strlongest.ado | 22 +++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 cf_vallabels.ado create mode 100644 cf_varlabels.ado create mode 100644 strlongest.ado diff --git a/cf_vallabels.ado b/cf_vallabels.ado new file mode 100644 index 0000000..243e50b --- /dev/null +++ b/cf_vallabels.ado @@ -0,0 +1,51 @@ +// Dominic R, 7/1/2020 + +program cf_vallabels + args file1 file2 + + clear + use "`file1'" + tempfile labels1 + quietly log using "`labels1'", text + label list + quietly log close + + clear + quietly import delimited "`labels1'" + quietly { + generate flag_variable=strpos(v1,":")==strlen(v1) + generate flag_value=strpos(v1," ")==1 + keep if flag_variable==1 | flag_value==1 + generate variable=v1 if flag_variable==1 + replace variable=variable[_n-1] if flag_variable==0 + generate value=v1 if flag_value==1 + keep if flag_value==1 + } + keep variable value + tempfile dictionary1 + save "`dictionary1'" + + clear + use "`file2'" + tempfile labels2 + quietly log using "`labels2'", text + label list + quietly log close + + clear + quietly import delimited "`labels2'" + quietly { + generate flag_variable=strpos(v1,":")==strlen(v1) + generate flag_value=strpos(v1," ")==1 + keep if flag_variable==1 | flag_value==1 + generate variable=v1 if flag_variable==1 + replace variable=variable[_n-1] if flag_variable==0 + generate value=v1 if flag_value==1 + keep if flag_value==1 + } + keep variable value + + merge 1:1 variable value using "`dictionary1'" + list _merge variable value if _merge!=3 +end + diff --git a/cf_varlabels.ado b/cf_varlabels.ado new file mode 100644 index 0000000..956a043 --- /dev/null +++ b/cf_varlabels.ado @@ -0,0 +1,43 @@ +// Dominic R, 7/1/2020 + +program cf_varlabels + args file1 file2 + if "`file1'"=="" { + display as error "No such file '`file1''" + error(601) + } + if "`file2'"=="" { + display as error "No such file '`file2''" + error(601) + } + use "`file1'", clear + foreach v of varlist _all { + if strlen("`v'")==31 { + local shortened = substr("`v'",1,30) + local _`shortened': variable label `v' + } + else { + local _`v': variable label `v' + } + } + use "`file2'", clear + foreach v of varlist _all { + local file2label: variable label `v' + + if strlen("`v'")==31 { + local shortened = substr("`v'",1,30) + local file1label `_`shortened'' + } + else { + local file1label `_`v'' + } + + capture assert "`file1label'"=="`file2label'" + if _rc!=0 { + display "Labels changed in `v':" + display " File 1: `file1label'" + display " File 2: `file2label'" + } + } +end + diff --git a/strlongest.ado b/strlongest.ado new file mode 100644 index 0000000..b89c821 --- /dev/null +++ b/strlongest.ado @@ -0,0 +1,22 @@ +// Dominic R, 7/1/2020 + +program strlongest + syntax [varlist(string default=none)] [, id(varname)] + + if "`varlist'"=="" { + quietly ds, has(type string) + local varlist `r(varlist)' + } + + foreach v of varlist `varlist' { + display "Examining `v'..." + + quietly generate Length = ustrlen(`v') + quietly generate Last10 = usubstr(`v', -10, .) + gsort -Length + list `id' Length Last10 in 1/3 + + drop Length Last10 + } +end + -- 2.45.2