Announcement

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

  • Is there a way to modify every value label in a data set or call the value label of a variable?

    I am working with data where the value -99 always means "Refused Question." It is currently unlabeled. Is there a way to call the value label of a variable?

    I would like to run a loop something like this:

    Code:
    foreach var of varlist _all { 
          label define <<VALUE LABEL OF `var'>> -99 "Refused Question", modify  }
    Where "<<VALUE LABEL OF `var'>>" calls the value label of the variable.

    Thanks!

  • #2
    You don't need a loop. You my wish to try this:

    Code:
    . input var1 var2
    
              var1       var2
      1. -99 2
      2. 3 -99
      3. 6 9
      4. -99 1
      5. 23 -99
      6. end
    
    . mvdecode _all, mv(-99=.a)
            var1: 2 missing values generated
            var2: 2 missing values generated
    
    . label define mymiss .a "Refused Question"
    
    . label values  _all mymiss
    
    . list
    
         +-------------------------------------+
         |             var1               var2 |
         |-------------------------------------|
      1. | Refused Question                  2 |
      2. |                3   Refused Question |
      3. |                6                  9 |
      4. | Refused Question                  1 |
      5. |               23   Refused Question |
         +-------------------------------------+
    Best regards,

    Marcos

    Comment


    • #3
      In official Stata, use

      Code:
      local lblname : value label varname
      to store the value label of a variable in local macro lblname.

      With elabel (SSC), use

      Code:
      *ssc install elabel
      elabel define * -99 "Refused Question" , modify
      to modify all value labels in the dataset.

      Best
      Daniel

      Comment


      • #4
        This splits into various parts. You can use findname (Stata Journal)

        One filter is variables with any values -99. That necessarily ignores all string variables.

        Code:
        findname, any(@==-99)  local(tochange)
        Then you can cycle over those variables

        Code:
        foreach v of local tochange {
              local lblname : value label `v'
              if "`lblname'" != "" {
                    label define `lblname' -99 "Refused question", modify
              }
              else {
                    label def `v'_lbl -99 "Refused question", modify
                    label val `v' `v'_lbl
              }
        }
        The stretch here is that -- if a value label is undefined -- a new one is created and I've optimistically assumed that a new value label name is possible as the variable name plus _lbl as suffix.
        Last edited by Nick Cox; 24 Jan 2020, 12:17.

        Comment


        • #5
          Originally posted by daniel klein View Post
          In official Stata, use

          Code:
          local lblname : value label varname
          to store the value label of a variable in local macro lblname.

          With elabel (SSC), use

          Code:
          *ssc install elabel
          elabel define * -99 "Refused Question" , modify
          to modify all value labels in the dataset.

          Best
          Daniel
          That worked, thank you Daniel!
          Also, thank you to everyone else!

          Comment

          Working...
          X