Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Detecting value labels

    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):

    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 *
    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
    Last edited by Nick Huntington-Klein; 07 Apr 2021, 13:15.

  • #2
    Here is a revised version of your code

    Code:
    cap prog drop labelfinder
    prog def labelfinder
        syntax varlist
        
        foreach var of local varlist {
            local has_label = ("`: value label `var''" != "")
            if `has_label' == 1 {
                di "`var' has value labels"
            }
        }
    end
    More generally, you seek extended macro functions

    Code:
    help macro

    There is much more to say, probably. For example, value labels might be attached to variables but not contain any information. Not all values need to be labeled. Not all labeled values need to occur in the data. There is too little information in the initial post to guess which details might or might not be necessary.
    Last edited by daniel klein; 07 Apr 2021, 13:23.

    Comment


    • #3
      That is perfect, thank you very much!

      Comment


      • #4

        Code:
        if "`: value label `varname''" != ""
        is a test, without the bother of defining a separate program, of whether a value label is defined given a local macro varname. Naturally it works fine if a particular name is used instead of a macro reference.

        The quoted string being empty is conversely a test of whether a value label is not defined.

        Comment


        • #5
          Being a bit pedantic: Nick's code test whether a variable has a value label attached (in the current label language); it does not, strictly speaking, test whether that label is defined. As I have noted above, Stata is perfectly happy to assign undefined (in the sense of not being stored in memory) value labels to variables.

          Comment


          • #6
            Being pedantic is fine by me. It's my brand too.

            It seems to me that in practice this is the major issue, but Nick H-K may want to decide quite what he needs in his program. The stuff at help macrolists is generally valuable for similar purposes.

            Comment

            Working...
            X