Announcement

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

  • Display label variable value using local and foreach

    Hi all

    I am trying to get label variable value using local and foreach, I did try with:

    Code:
    sysuse auto.dta, clear
    levelsof foreign, local(levels)
    local label `v' = foreign
    
    foreach v of local levels {
    
    di `v'
    }
    0
    1
    Also I try with var string, but not display properly:

    Code:
    decode foreign, gen(foreign2)
    levelsof foreign2, local(levels)
    local label `v' = foreign2
    
    foreach v of local levels {
    
    di `v'
    }
    Domestic not found
    I grateful any coments about this issue.
    Thanks in advance

  • #2
    It sounds like you want to display the value label for each value of a variable.

    Try this:
    Code:
    sysuse auto, clear
    levelsof foreign, local(levels)
    local vlname: value label foreign
    foreach L of local levels {
       local vl: label `vlname' `L'
       display "value label = `vl'"
    }
    There are a number of problems in your use of locals. For example, "local label `v' = foreign" will fail badly because: 1) `v' refers to the contents of the macro v. However, you have not yet defined any macro called v, so it will be empty. 2) The extended macro functions require the use of ":", not "=" . See _help extended_fcn_. 3) Also, assigning a variable, without a subscript, to a local, will result in the first observation's value of the variable being assigned to that local. This is almost never what one wants to do. Your command "local label `v' = foreign" gets seen by Stata as "local label = foreign". To Stata, this means: "Create a new local macro named label, and assign to it the value that the first observation has for the variable foreign."

    Comment


    • #3
      Dear Mike,

      Thanks for you reply, your code works great!.
      Sorry for my mistake, but fortunately you understand my question!
      Regards

      Comment


      • #4
        For the underlying problem, also see elabel (recently released on SSC).

        Code:
        sysuse auto
        elabel list (foreign)
        return list
        returns

        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""
        
        . 
        end of do-file
        Best
        Daniel

        Comment


        • #5
          Thanks Daniel!

          Great news! get value label always its a problem!
          Regards

          Comment

          Working...
          X