Announcement

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

  • Changing values of yes/no binary variables from 1/2 to 1/0 using loop

    Hello,

    I believe this is a common problem but I have not found an answer online. I have a large dataset with multiple numeric binary variables with "no" response options coded as 2 instead of 0. Additionally, there are no value labels in the dataset. Fortunately, when a variable has values equal to either 1 or 2, it is always a yes/no question. Therefore, I would like to create a loop that changes the value 2 to 0 only for binary variables with response options equal to 1 or 2 (or have a max value of 2). I would also like to create value labels. I provided the code I have so far and an example dataset below. In the example dataset, all the variables are binary except for 'numkids', so I would not want to change the values of that variable. Could you help me fix the blue part of my code?

    Thank you,
    Tom

    local imputedvars *_i
    foreach var of `imputedvars’ {
    recode `var' (2=0) if `var' has values equal to 1, 2, or .
    label define yesno 0 “No” 1 “Yes”
    label values `var’ yesno if `var' has values equal to 0, 1, or .
    }


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(id male white older21 numkids married)
    1 1 2 1 0 1
    2 2 1 1 3 2
    3 1 2 2 2 1
    4 2 2 2 0 1
    5 1 1 2 1 2
    6 2 2 1 1 1
    7 1 1 2 2 2
    8 2 1 1 0 1
    end


  • #2
    findname from the Stata Journal can help here. I note that you want to exclude the identifier too and that you should define the value labels just once, outside the loop (indeed, you'll get an error trying to redefine it otherwise).

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(id male white older21 numkids married)
    1 1 2 1 0 1
    2 2 1 1 3 2
    3 1 2 2 2 1
    4 2 2 2 0 1
    5 1 1 2 1 2
    6 2 2 1 1 1
    7 1 1 2 2 2
    8 2 1 1 0 1
    end
    
    findname, all(inlist(@, 1, 2)) local(wanted) 
    label define yesno 0 "No" 1 "Yes" 
    
    foreach v of local wanted { 
        replace `v' = `v' == 1 
        label val `v' yesno 
    }

    Comment


    • #3
      Hi Nick,

      Thank you very much. I will have to remember that great command. I tried your code but the number of cases in the yes and no conditions were wrong. It was my fault for not including missing values in my example dataset. I used the following code and it seems to have worked. Thanks again!

      findname, all(inlist(@, ., 1, 2)) local(wanted)
      label define yesno 0 "No" 1 "Yes"

      foreach v of local wanted {
      replace `v' = 0 if `v' == 2
      label val `v' yesno
      }

      Comment


      • #4
        Indeed: missing values would oblige a change in the recipe.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          Indeed: missing values would oblige a change in the recipe.
          Could you please show us how to solve this problem when missing values take place?
          * Example generated by -dataex-.
          To install: ssc install dataex
          clear
          input byte(id male white older21 numkids married)
          1 1 2 1 0 1
          2 2 1 . 3 2
          3 1 2 2 . 1
          4 . . . 0 1
          5 1 1 2 1 2
          6 2 2 1 . 1
          7 . 1 2 . 2
          8 2 . . 0 1
          end

          Thank you!
          Last edited by smith Jason; 15 Jul 2023, 22:24.

          Comment


          • #6
            Originally posted by smith Jason View Post
            Could you please show us how to solve this problem when missing values take place?
            I believe that Nick was alluding to the approach shown immediately before, in #3. Have you tried it?

            Comment


            • #7
              The mapping for whatever 2 to 0, 1 to 1 is accomplished by 2 - whatever. Notice that this works for missings too as 2 - . is returned as . (missing).

              Here is some other technique too. See also https://journals.sagepub.com/doi/pdf...36867X19830921

              Clearly one technique that works and is congenial is enough.

              Code:
              clear
              input byte(id male white older21 numkids married)
              1 1 2 1 0 1
              2 2 1 . 3 2
              3 1 2 2 . 1
              4 . . . 0 1
              5 1 1 2 1 2
              6 2 2 1 . 1
              7 . 1 2 . 2
              8 2 . . 0 1
              end
              
              findname , all(inlist(@, 1, 2, .)) local(tofix) 
              
              foreach v of local tofix { 
                  gen `v'_1 = 2 - `v' 
                  gen `v'_2 = cond(missing(`v'), ., `v' == 1)
                  assert `v'_1 == `v'_2 
              }
              
              label def yesno 1 "yes" 0 "no"
              label val *_? yesno 
              
              list *_1 , sep(0)
              
                   +----------------------------------------+
                   | male_1   white_1   older~_1   marrie~1 |
                   |----------------------------------------|
                1. |    yes        no        yes        yes |
                2. |     no       yes          .         no |
                3. |    yes        no         no        yes |
                4. |      .         .          .        yes |
                5. |    yes       yes         no         no |
                6. |     no        no        yes        yes |
                7. |      .       yes         no         no |
                8. |     no         .          .        yes |
                   +----------------------------------------+

              Comment


              • #8
                Thank you very much!

                Comment

                Working...
                X