Announcement

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

  • value labels by variable name

    Is a Stata or user command available to display a list of all the value labels in a data set by variable.

    For example, the following two commands list the variable names and the value labels in the data set:

    .de
    digital label1
    animal label2
    astronomy label3

    label list
    label1:
    0 No
    1 Yes
    label2:
    1 Dogs
    2 Cats
    3 Birds
    label3:

    1 Earth
    2: Sun
    3 Moon

    The hypothetical command would display:

    .command

    digital
    0 No
    1 Yes

    animal
    1 Dogs
    2 Cats
    3 Birds

    astronomy
    1 Earth
    2 Sun
    3 Moon


  • #2
    how about,
    Code:
    label list _all

    Comment


    • #3
      Not sure about the details, but tabulate

      Code:
      tabulate varname , nolabel
      or fre (SSC)

      Code:
      *ssc install fre
      fre varname
      might be what you are looking for.


      Edit:

      The following resembles more closely the output in the original example

      Code:
      foreach var of varlist * {
          local valuelabel : value label `var'
          if ("`valuelabel'" != "") {
              display "`var'"
              label list `valuelabel'
          }
      }
      Last edited by daniel klein; 04 Mar 2022, 03:18.

      Comment


      • #4
        Here I used findname and groups from the Stata Journal. You could get very similar results with just official commands.


        Code:
        webuse educ3
        
        findname , vallabel local(vars)
        
        foreach v of local vars {
            local label : value label `v'
            numlabel `label', add
            di "{title:`v'}"
            groups `v', show(none) sep(0)
            numlabel `label', remove
        }
        Code:
        state
        
          +--------------+
          |        state |
          |--------------|
          |   1. ALABAMA |
          |    2. ALASKA |
          |   4. ARIZONA |
          |       5. ARK |
          |     6. CALIF |
          |      8. COLO |
          |      9. CONN |
          |     10. DELA |
          |  12. FLORIDA |
          |  13. GEORGIA |
          |   15. HAWAII |
          |    16. IDAHO |
          |      17. ILL |
          |      18. IND |
          |     19. IOWA |
          |   20. KANSAS |
          |      21. KEN |
          |       22. LO |
          |    23. MAINE |
          | 24. MARYLAND |
          |     25. MASS |
          |     26. MICH |
          |     27. MINN |
          |     28. MISS |
          |       29. MS |
          |  30. MONTANA |
          |     31. NEBR |
          |   32. NEVADA |
          |       33. NH |
          |     34. N.J. |
          | 35. N MEXICO |
          |     36. N.Y. |
          |    37. N CAR |
          | 38. N DAKOTA |
          |     39. OHIO |
          |       40. OK |
          |   41. OREGON |
          |     42. PENN |
          |     44. R.I. |
          |    45. S CAR |
          | 46. S DAKOTA |
          |     47. TENN |
          |    48. TEXAS |
          |     49. UTAH |
          |       50. VT |
          | 51. VIRGINIA |
          |     53. WASH |
          |    54. W VIR |
          |     55. WISC |
          |  56. WYOMING |
          +--------------+
        division
        
          +-------------+
          |    division |
          |-------------|
          |  1. N. Eng. |
          |  2. Mid Atl |
          |   3. E.N.C. |
          |   4. W.N.C. |
          |  5. S. Atl. |
          |   6. E.S.C. |
          |   7. W.S.C. |
          | 8. Mountain |
          |  9. Pacific |
          +-------------+
        region
        
          +------------+
          |     region |
          |------------|
          |      1. NE |
          | 2. N Cntrl |
          |   3. South |
          |    4. West |
          +------------+
        Note: This just shows value labels present in the data. But this simpler loop may be close enough to what you want:


        Code:
        webuse educ3 
        
        findname , vallabel local(vars)
        
        foreach v of local vars { 
            local label : value label `v'
            di "{title:`v'}" 
            label list `label'
        }
        Last edited by Nick Cox; 04 Mar 2022, 04:24.

        Comment


        • #5
          ds was born an official command. I cloned it under different names and added functionality, most of which was folded back into official Stata within Stata 8.

          Then in 2010 -- partly given some misgivings about the syntax and more positively to add yet more functionality -- I released findname as a separate community-contributed command and have updated it at various times since, most recently in 2020. So, I feel at liberty to give little advertisements on its behalf.

          That said, the last block of code can be rewritten to avoid it

          Code:
          webuse educ3
          
          ds, has(vallabel)
          local vars `r(varlist)'
          
          foreach v of local vars {
              local label : value label `v'
              di "{title:`v'}"
              label list `label'
          }
          It's an exercise for anyone so inclined to cut out local vars as what has been called a middle macro.

          Edit:

          Somewhere along the line I missed daniel klein 's edit to #3, which boils down to the same result.
          Last edited by Nick Cox; 04 Mar 2022, 09:06.

          Comment

          Working...
          X