Announcement

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

  • How to store a variable's value label into a global macro?

    Hi stata masters,

    I want to store a variable's value label into a macro, and then I use this macro for further programming. But I can not find any command to store them. Here is an example, I use "label list" command to show the value labels of an variable (carClass), how can I store this value label?
    Click image for larger version

Name:	1.png
Views:	1
Size:	2.3 KB
ID:	1627258


    Thanks!





  • #2
    Install elabel (from SSC). Here is an example:

    Code:
    . elabel list carClass
    carClass:
               1 A
               2 A0
               3 A00
               4 B
               5 C
               6 D
               7 MPV
               8 SUV A
               9 Specialty
    
    . return list
    
    scalars:
                    r(min) =  1
                    r(max) =  9
               r(hasemiss) =  0
                 r(nemiss) =  0
                      r(k) =  9
    
    macros:
                   r(name) : "carClass"
                 r(values) : "1 2 3 4 5 6 7 8 9"
                 r(labels) : ""A" "A0" "A00" "B" "C" "D" "MPV" "SUV A" "Specialty""

    For more examples, see the original announcement. For an even more detailed introduction, see Klein (2019).



    Klein, D. 2019. Extensions to the label commands. The Stata Journal, 19(4), pp.. 867--882.

    Comment


    • #3
      daniel klein gave an excellent answer that doesn't do what you asked. In addition, I would never try to put either the name of a value label or (even less) the entire definition of a set of value labels in a global macro.

      The functionality detailed at

      Code:
      help macro##macro_fcn
      and at

      Code:
      help mf_st_vlexists
      is the basis for using and/or manipulating value labels.

      Comment


      • #4
        Originally posted by daniel klein View Post
        Install elabel (from SSC). Here is an example:

        Code:
        . elabel list carClass
        carClass:
        1 A
        2 A0
        3 A00
        4 B
        5 C
        6 D
        7 MPV
        8 SUV A
        9 Specialty
        
        . return list
        
        scalars:
        r(min) = 1
        r(max) = 9
        r(hasemiss) = 0
        r(nemiss) = 0
        r(k) = 9
        
        macros:
        r(name) : "carClass"
        r(values) : "1 2 3 4 5 6 7 8 9"
        r(labels) : ""A" "A0" "A00" "B" "C" "D" "MPV" "SUV A" "Specialty""

        For more examples, see the original announcement. For an even more detailed introduction, see Klein (2019).



        Klein, D. 2019. Extensions to the label commands. The Stata Journal, 19(4), pp.. 867--882.
        Thank you very much!

        Comment


        • #5
          Originally posted by Nick Cox View Post
          daniel klein gave an excellent answer that doesn't do what you asked. In addition, I would never try to put either the name of a value label or (even less) the entire definition of a set of value labels in a global macro.

          The functionality detailed at

          Code:
          help macro##macro_fcn
          and at

          Code:
          help mf_st_vlexists
          is the basis for using and/or manipulating value labels.
          Thank you !

          Comment

          Working...
          X