3

In my dataset, I have a bunch of Yes/No type variables. For some reason, "Yes" is coded as 1 and "No" is coded as 2 instead of 0. Now I want to recode 2 to 0 based on the value label "No".

How can I do it without having to check and recode every single one?

There are some complications:

  1. Each of these dummies has a value label sharing the dummy's name instead of sharing a "yesno" value label. Therefore, I can't simply loop through all variables that have a "yesno" value label.

  2. There might be reserve codes (-1 for Don’t know, -2 for Refused, etc.) in these dummies. Because of these reserve codes, I think the best way to recode is via checking value label because I know for sure that 2 is labelled as No.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
Elise
  • 87
  • 2
  • 10
  • Hi Nix, I wanted to do something along this line: foreach v of varlist _all { local u: value label `v' replace `v'=0 if `u'=="No" } I don't know which variables are Yes/No type and I don't want to check and type out the name of all of these variables. So I was wondering if there's a faster way to do this. Thanks. – Elise May 14 '15 at 16:40

1 Answers1

3

Let's suppose that you are looking for variables with a certain value label attached. You can retrieve those variables using ds and pass their names to recode.

. clear

. set obs 2
obs was 0, now 2

. forval j = 1/5 {
  2. gen y`j' = _n
  3. }

. label def yesno 1 yes 2 no

. label val y4 yesno

. label val y5 yesno

. ds, has(vall yesno)
y4  y5

. ret li

macros:
            r(varlist) : "y4 y5"

. recode `r(varlist)' (2 = 0)

After that the value label needs adjustment too:

. label def yesno 0 "No", modify 

EDIT (following helpful remarks by @Heisenberg)

If you are using more than one set of value labels, you need to apply this method repeatedly for different value labels or or consider another one.

Here is a more general method of looking for variables with values of 2 that have the value label "No" attached. Warning: This should change your dataset. Make sure you save the earlier version.

 ds, has(vall) 

 foreach v in `r(varlist)' { 

       local lbl : label (`v') 2 

       if `"`lbl'"' == "No" { 
             replace `v' = 0 if `v' == 2 
             local label : value label `v' 
             label def `label' 0 "No", modify 
       } 

  }
Nick Cox
  • 35,529
  • 6
  • 31
  • 47
  • But if I don't know which variables are the yes/no one, how do I "lab def" them all at once? – Elise May 14 '15 at 17:07
  • 1
    Please study the example. It just sets up a self-contained sandbox to show the point (given that you don't give a reproducible example yourself). You already have your own data, clearly, and so can and should start with using `ds` to find the variables. – Nick Cox May 14 '15 at 17:12
  • I think @Elise's issue is that her binary variables all follow the same pattern (having "yes" and "no" as values), but they do not necessarily all share the value label yesno. So she's asking for a way to identify those variables based on the pattern, not based on the value label. – Heisenberg May 14 '15 at 17:59
  • @Heisenberg On the contrary, to me she implies (indeed, states) that value labels are being used. Conversely, if value labels are not being used, I know no automated way to tell which values of 2 mean No and which mean something else and the question asked is not soluble without more information. This is what the question says: I want to recode 2 to 0 based on the value label "No" – Nick Cox May 14 '15 at 18:08
  • @NickCox I agree that value labels are being used, but it's not necessarily the case that all these value labels are called "yesno". In my experience, a lot of people define one value label for each variable, using the name of the variable instead of something more conceptually appropriate (like "yesno") – Heisenberg May 14 '15 at 18:21
  • @Heisenberg You're right, but a value label `yesno` was just my illustrative example and in that case evidently other sets of value labels need to be scanned. I wait for details on what didn't work before suggesting other methods. So far we have been told little on the details of the OP's dataset. – Nick Cox May 14 '15 at 18:23
  • 1
    @Heisenberg I have added some extra comments to the answer. – Nick Cox May 14 '15 at 18:41