Announcement

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

  • How to extract label text given a variable value?

    If I can access different values of a variable using -levelsof-, is there a way to extract the label text associated with that value?

  • #2
    At the command line in Stata, type
    Code:
    help macro##macro_fcn
    scroll to the heading Macro functions for extracting data attributes and look at the syntax for value label and label.

    Comment


    • #3
      Also see elabel (SSC).

      Example:

      Code:
      . sysuse auto
      (1978 Automobile Data)
      
      . elabel list (foreign)
      origin:
                 0 Domestic
                 1 Foreign
      
      . return list
      
      scalars:
                      r(min) =  0
                      r(max) =  1
                 r(hasemiss) =  0
                   r(nemiss) =  0
                        r(k) =  2
      
      macros:
                     r(name) : "origin"
                   r(values) : "0 1"
                   r(labels) : ""Domestic" "Foreign""
      
      . elabel list (foreign) iff # == 0
      origin:
                 0 Domestic
      
      . return list
      
      scalars:
                      r(min) =  0
                      r(max) =  0
                 r(hasemiss) =  0
                   r(nemiss) =  0
                        r(k) =  1
      
      macros:
                     r(name) : "origin"
                   r(values) : "0"
                   r(labels) : ""Domestic""

      Comment


      • #4
        I cannot install SSC because I don't have administrative privileges. I think r(labels) would solve my problem but when I type list return, here's what I get

        Code:
        - return list
        
        scalars:
                          r(N) =  1482
                          r(r) =  2
        
        macros:
                     r(levels) : "0 1"
        If I was able to store r(labels) in a local variable, I would like to be able to retrieve the "male" and "female" by iterating through the local variable. Here's the codebook for what I'm dealing with:

        Code:
        rsex                                               combined & complete sex data
        -------------------------------------------------------------------------------
        
                          type:  numeric (float)
                         label:  rsex_complete
        
                         range:  [0,1]                        units:  1
                 unique values:  2                        missing .:  0/1,482
        
                    tabulation:  Freq.   Numeric  Label
                                   901         0  male
                                   581         1  female

        Comment


        • #5
          Originally posted by Sehajroop Bath View Post
          I cannot install SSC because I don't have administrative privileges.
          Then you're going to have to go with official Stata, which is what is recommended in #2 above.

          If I was able to store r(labels) in a local variable, I would like to be able to retrieve the "male" and "female" by iterating through the local variable.
          Adopt the following to suit your case.


          .ÿclearÿ*

          .ÿlocalÿline_sizeÿ`c(linesize)'

          .ÿsetÿlinesizeÿ72

          .ÿquietlyÿsysuseÿauto

          .ÿcodebookÿforeign

          ------------------------------------------------------------------------
          foreignÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿCarÿtype
          ------------------------------------------------------------------------

          ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿtype:ÿÿnumericÿ(byte)
          ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿlabel:ÿÿorigin

          ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿrange:ÿÿ[0,1]ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿunits:ÿÿ1
          ÿÿÿÿÿÿÿÿÿuniqueÿvalues:ÿÿ2ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿmissingÿ.:ÿÿ0/74

          ÿÿÿÿÿÿÿÿÿÿÿÿtabulation:ÿÿFreq.ÿÿÿNumericÿÿLabel
          ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ52ÿÿÿÿÿÿÿÿÿ0ÿÿDomestic
          ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ22ÿÿÿÿÿÿÿÿÿ1ÿÿForeign

          .ÿlocalÿlabelsÿ`:ÿlabelÿ(foreign)ÿ1'ÿ`:ÿlabelÿ(foreign)ÿ0'

          .ÿdisplayÿinÿsmclÿasÿtextÿ"`labels'"
          ForeignÿDomestic

          .ÿsetÿlinesizeÿ`line_size'

          .

          Comment


          • #6
            Originally posted by Joseph Coveney View Post
            Then you're going to have to go with official Stata, which is what is recommended in #2 above.

            Adopt the following to suit your case.


            .ÿclearÿ*

            .ÿlocalÿline_sizeÿ`c(linesize)'

            .ÿsetÿlinesizeÿ72

            .ÿquietlyÿsysuseÿauto

            .ÿcodebookÿforeign

            ------------------------------------------------------------------------
            foreignÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿCarÿtype
            ------------------------------------------------------------------------

            ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿtype:ÿÿnumericÿ(byte)
            ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿlabel:ÿÿorigin

            ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿrange:ÿÿ[0,1]ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿunits:ÿÿ1
            ÿÿÿÿÿÿÿÿÿuniqueÿvalues:ÿÿ2ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿmissingÿ.:ÿÿ0/74

            ÿÿÿÿÿÿÿÿÿÿÿÿtabulation:ÿÿFreq.ÿÿÿNumericÿÿLabel
            ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ52ÿÿÿÿÿÿÿÿÿ0ÿÿDomestic
            ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ22ÿÿÿÿÿÿÿÿÿ1ÿÿForeign

            .ÿlocalÿlabelsÿ`:ÿlabelÿ(foreign)ÿ1'ÿ`:ÿlabelÿ(foreign)ÿ0'

            .ÿdisplayÿinÿsmclÿasÿtextÿ"`labels'"
            ForeignÿDomestic

            .ÿsetÿlinesizeÿ`line_size'

            .
            That worked great thanks a lot!

            Comment

            Working...
            X