I am working on a program that needs to know whether the incoming variables have value labels, since it will treat them differently on that basis. How can I detect programmatically whether a variable has value labels? Obviously I can look at describe or codebook to see with my eyes, but these don't seem to offer a way to use the results in code.
The only way I can think to do this is to loop through the values and check if each one is labeled, but this is untenable because I may be passed a continuous variable. Or maybe doing something weird with checking whether decode changes any values, but that seems like a bad idea. I haven't dug around in the labutil package, so there may be something there, but ideally I can do this without adding a dependency to my package.
An example of the kind of thing I'd like to do (obviously this code is missing a line to make it work):
I don't need the name of the labels, just whether it's labeled or not, in case that makes it easier (although if I do have the name, obviously that tells me whether it's labeled).
Thank you
The only way I can think to do this is to loop through the values and check if each one is labeled, but this is untenable because I may be passed a continuous variable. Or maybe doing something weird with checking whether decode changes any values, but that seems like a bad idea. I haven't dug around in the labutil package, so there may be something there, but ideally I can do this without adding a dependency to my package.
An example of the kind of thing I'd like to do (obviously this code is missing a line to make it work):
Code:
cap prog drop labelfinder prog def labelfinder syntax varlist foreach var in `varlist' { local label = SOMETHING HERE if `label' == 1 { di "`var' has value labels" } } end sysuse auto.dta, clear labelfinder *
Thank you
Comment