Announcement

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

  • Replace missing values by system-missing '.' if label value is "Manquant"

    Hello,

    I have a survey where most variables have a different code for missing values, I need to unify the code for missing values.
    According to my survey variables (>500 variables) the values can be 9, 97, 98, 99, 999, 9997, 9998, 9999, etc. I need to change them all to '.'
    To use 'recode' is not efficient as I need to to go through the variables one by one and group them into categories that have the same code for missing.
    Alternatively, they all have one thing in common: the label to the missing value is "Manquant" regardless of the underlying value (9, 99, 997, etc).
    An efficient solution is to be able to recode all variables at once, as soon as the label is identified to be "Maquant".

    I tried the following code:

    Code:
     foreach var in varlist E* {
     replace `var'=. if `var'== "Manquant" :`var'0valuelabel
     }
    But stata can't find the value label "Maquant" when I use "valuelabel"

    Any help?

    Thx!
    S.

  • #2
    You are alternating between Manquant and Maquant

    It is foreach var of varlist and not foreach var in varlist

    Anyhow here is an example that works. It would have been very helpful If you gave us an extract of your data using dataex. That way we know what your data looks like and we don't have to guess (and possibly guess wrong).

    Code:
    clear
    input byte(E1 E2 E3 E4 E5 E6 E7 E8 E9 E10)
    1 3 2 1 1 3 3 2 1 3
    3 3 2 3 2 1 1 2 2 2
    3 3 3 1 2 3 1 2 2 2
    2 1 3 3 2 2 2 1 3 3
    1 3 3 2 3 3 2 3 1 1
    2 1 1 3 3 2 1 1 3 2
    2 1 3 1 1 2 2 2 3 2
    3 2 2 3 1 2 1 3 2 3
    1 1 3 3 2 3 2 1 1 1
    2 3 1 3 3 1 1 2 2 2
    end
    label values E1 E1lab
    label def E1lab 2 "Manquant", modify
    label values E2 E2lab
    label def E2lab 3 "Manquant", modify
    label values E3 E3lab
    label def E3lab 1 "Manquant", modify
    label values E4 E4lab
    label def E4lab 2 "Manquant", modify
    label values E5 E5lab
    label def E5lab 3 "Manquant", modify
    label values E6 E6lab
    label def E6lab 1 "Manquant", modify
    label values E7 E7lab
    label def E7lab 2 "Manquant", modify
    label values E8 E8lab
    label def E8lab 3 "Manquant", modify
    label values E9 E9lab
    label def E9lab 1 "Manquant", modify
    label values E10 E10lab
    label def E10lab 2 "Manquant", modify
    
    foreach var of varlist E* {
        
        replace `var' = . if `var' == "Manquant":`var'lab
    }
    list
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Dear Maarten,

      The label value I am looking for is "Manquant" = 99 and I want to substitute it with system missing '.'
      (sorry for the earlier typos)

      When I try your suggested code on my dataset it gives the following error: (value label Manquant not found)

      Please find attached 5 variables of my dataset (EXAMPLE.dta) , and the relevant code:

      Code:
      label list HH10 HH18H HH19H WS1 CD13 CD14
      foreach var of varlist HH10 HH18H HH19H WS1 CD13 CD14 {
          tab `var'
          replace `var' = . if `var' == "Manquant":`var'lab
          tab `var'
      }
      Do you think it has to do with the variable type?
      Attached Files

      Comment


      • #4
        The reason is simple: in your loop you assume that the value labels are called <variable_name>lab, so for the variable HH10, the label is assumed to be called HH10lab. However, if you type desc you can see (in the column value label) that they are just called <variable_name>, i.e. the labels for the variable HH10 is called HH10.

        Code:
        label list HH10 HH18H HH19H WS1 CD13 CD14
        foreach var of varlist HH10 HH18H HH19H WS1 CD13 CD14 {
            replace `var' = .a if `var' == "Manquant":`var'
            label define `var' .a "Manquant", add
        }
        Here I use extended missing values (.a, .b, .c, ... .z), so you can still distinguish them from your regular missing values.
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          If you know the value is 99 whenever the value label is Manquant -- and nothing stops other values being labelled with the same text -- then all you need is

          Code:
          mvdecode HH10 HH18H HH19H WS1 CD13 CD14, mv(99)
          Looking at the results of label list gives you a check.

          Comment

          Working...
          X