~dricottone/ado-personal

247704c9bfa22ecddaee3324ba2edfaa2916cd48 — Dominic Ricottone 3 years ago
Initial commit
3 files changed, 116 insertions(+), 0 deletions(-)

A cf_vallabels.ado
A cf_varlabels.ado
A strlongest.ado
A  => cf_vallabels.ado +51 -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


A  => cf_varlabels.ado +43 -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


A  => strlongest.ado +22 -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