From a value label I need to retrieve the value associated with a particular label. That is, I need the inverse of:
Given the string "Foreign" and the value label , I need to retrieve the value 1. There is the usage
But there doesn't seem to be an extended macro function, or anything else I can find, that uses the "string":labname construct.
I've created this to do the job, but I wonder if I'm missing somethign inbuilt?
Code:
sysuse auto local lab : label domestic 1 di "`lab'"
Code:
sum foreign if foreign=="Foreign":origin
I've created this to do the job, but I wonder if I'm missing somethign inbuilt?
Code:
program getvalfromlab, rclass version 14.2 syntax anything , [ local(string) ] gettoken vl rest : anything // trim local rest `rest' local foundit 0 qui la list `vl' forval i=`r(min)'/`r(max)' { if `"`: label `vl' `i''"'==`"`rest'"' { di `"{txt}"{res}`rest'{txt}" --> {res}`i'"' return scalar value = `i' return local label "`rest'" if !mi(`"local"') { c_local `local' = `i' } local foundit 1 continue, break } } if !`foundit'{ if !mi(`"local"') { c_local `local' } di `"{res}`rest'{txt} not found `i'"' } end
Comment