Announcement

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

  • ds command not detecting value labels

    Hi,

    I am trying to list all variables in a dataset containing a specific value using the ds command, but it's not working.

    Here is an example:



    Code:
    . ds, has(vallabel Domestic)
    
    . `r(varlist)'
    .


    I was expecting this to return the variable 'foreign'. Can you please help me find the error in it.

  • #2
    Not an error. ds finds value lable names, not contents (i.e., integer-to-text mappings) in value labels.

    Community-contributed findname (SJ20-2 dm0048_4) does what you want.

    Code:
    . sysuse auto
    (1978 automobile data)
    
    . findname , vallabeltext(Domestic)
    foreign
    Edit: I forgot about a rather recent update. There are now two options, vallabeltextdef() and vallabeltextuse(); read the help file to see which one you want.
    Last edited by daniel klein; 29 Jun 2022, 08:46.

    Comment


    • #3
      In the -has(vallabel ...)- option of -ds-, the ... refers to the name of the value label, not any of its values. To find any variable which has a value that is labeled "Domestic" is a bit more complicated:

      Code:
      sysuse auto, clear
      
      quietly ds, has(vallabel)
      
      foreach v of varlist `r(varlist)' {
          decode `v', gen(_`v')
          capture assert _`v' != "Domestic"
          if c(rc) != 0 {
              display `"`v'"'
          }
          drop _`v'
      }
      Added: Crossed with #2, which offers a better solution.

      Comment


      • #4
        I believe that searches the "name of the label scheme" and not the actual item inside the scheme.

        So, this would work:
        Code:
        describe foreign // notice that the labeling scheme is called "origin"
        ds, has(vallabel *ori*)

        Comment


        • #5
          Originally posted by daniel klein View Post
          Not an error. ds finds value lable names, not contents (i.e., integer-to-text mappings) in value labels.

          Community-contributed findname (SJ20-2 dm0048_4) does what you want.

          Code:
          . sysuse auto
          (1978 automobile data)
          
          . findname , vallabeltext(Domestic)
          foreign
          Thanks a lot Daniel for the quick response. This is working for the variable 'foreign'.

          I also tried this with make, but this one is not working:

          Code:
          . findname , vallabeltext(Toyota)
          
          . `r(varlist)'

          Just for interest, I'd also like to know if ds is capable of doing that.

          Thank you.

          Comment


          • #6
            make is a string variable. It cannot have a value label attached.

            Code:
            . label values make labelname
            may not label strings
            r(181);
            ds does not search values; findname does

            Code:
            . findname , any(strmatch(@, "Toyota*"))
            make

            Comment


            • #7
              Well, there is no variable that meets that description. First, the auto data set has only one variable with a value label, namely foreign, which has label origin. But value label origin does not contain "Toyota." The only thing that refers to Toyota in that data set is the variable make. And make does not have a value label: it is a string variable. Moreover, none of the values of make is "Toyota", although there are related values like "Toyota Celica", etc. If you want to find any string variable that has a value that includes (but is not necessarily equal to) "Toyota":

              Code:
              . findname, any(strmatch(@, "*Toyota*") == 1)
              make
              Added: Crossed with #6. There is a slight difference between our solutions. In #6, if there is a value that begins with Toyota, it is found. In #7, if Toyota occurs anywhere in the value, it is found.

              Comment


              • #8
                Originally posted by Clyde Schechter View Post
                In the -has(vallabel ...)- option of -ds-, the ... refers to the name of the value label, not any of its values. To find any variable which has a value that is labeled "Domestic" is a bit more complicated:

                Code:
                sysuse auto, clear
                
                quietly ds, has(vallabel)
                
                foreach v of varlist `r(varlist)' {
                decode `v', gen(_`v')
                capture assert _`v' != "Domestic"
                if c(rc) != 0 {
                display `"`v'"'
                }
                drop _`v'
                }
                Added: Crossed with #2, which offers a better solution.
                Thanks Clyde. This one is a bit complex to understand. I found the ds command very convenient and wanted to use for all pattern-matching purposes. I hope they include the vallabeltext functionality to ds soon, since they call it 'hidden gem'.

                Comment


                • #9
                  Originally posted by daniel klein View Post
                  make is a string variable. It cannot have a value label attached.

                  Code:
                  . label values make labelname
                  may not label strings
                  r(181);
                  ds does not search values; findname does

                  Code:
                  . findname , any(strmatch(@, "Toyota*"))
                  make
                  This is wonderful!

                  Comment


                  • #10
                    Originally posted by Clyde Schechter View Post
                    Well, there is no variable that meets that description. First, the auto data set has only one variable with a value label, namely foreign, which has label origin. But value label origin does not contain "Toyota." The only thing that refers to Toyota in that data set is the variable make. And make does not have a value label: it is a string variable. Moreover, none of the values of make is "Toyota", although there are related values like "Toyota Celica", etc. If you want to find any string variable that has a value that includes (but is not necessarily equal to) "Toyota":

                    Code:
                    . findname, any(strmatch(@, "*Toyota*") == 1)
                    make
                    Added: Crossed with #6. There is a slight difference between our solutions. In #6, if there is a value that begins with Toyota, it is found. In #7, if Toyota occurs anywhere in the value, it is found.
                    Thanks Clyde. Actually I was not aware of 'label origin'. It sounds little strange. I thought it is enough for a variable to have a name, label, and value labels. Maybe 'label origin' is less known feature.

                    Comment


                    • #11
                      Originally posted by Sonnen Blume View Post
                      Maybe 'label origin' is less known feature.
                      I cannot comment on what is and what is not well known. The concept of value labels in Stata is documented in [D] label.

                      Comment


                      • #12
                        There is no feature "label origin." What I meant in #7 is thatorigin is the name of the value label attached to the variable foreign. You can see that by running -des foreign-.

                        Comment


                        • #13
                          The description ‘hidden gem’ was the wording of Martin Weiss and alludes to a brief phase when ds went undocumented. There is currently nothing hidden.

                          To simplify a complicated history, ds was born as an official command. I published variants of it under different names. Most but not all of the extra features were folded back into the official command.

                          Later still I came to regard some of the syntax I had introduced as awkward and had ideas for other features and that is all in
                          findname from the Stata Journal.

                          Comment


                          • #14
                            lookfor2 on SSC allows one to search value labels ( and variable labels, variable names, and notes).
                            ---------------------------------
                            Maarten L. Buis
                            University of Konstanz
                            Department of history and sociology
                            box 40
                            78457 Konstanz
                            Germany
                            http://www.maartenbuis.nl
                            ---------------------------------

                            Comment


                            • #15
                              Originally posted by Maarten Buis View Post
                              lookfor2 on SSC allows one to search value labels ( and variable labels, variable names, and notes).
                              Thank you Maarten! lookfor2 has all the functionalities I was looking for.

                              Comment

                              Working...
                              X