binary - Recode the same value pattern for all variables in Stata -


in dataset, have bunch of yes/no type variables. reason, "yes" coded 1 , "no" coded 2 instead of 0. want recode 2 0 based on value label "no".

how can without having check , recode every single one?

there complications:

  1. each of these dummies has value label sharing dummy's name instead of sharing "yesno" value label. therefore, can't loop through variables have "yesno" value label.

  2. there might reserve codes (-1 don’t know, -2 refused, etc.) in these dummies. because of these reserve codes, think best way recode via checking value label because know sure 2 labelled no.

let's suppose looking variables value label attached. can retrieve variables using ds , pass names recode.

. clear  . set obs 2 obs 0, 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 value label needs adjustment too:

. label def yesno 0 "no", modify  

edit (following helpful remarks @heisenberg)

if using more 1 set of value labels, need apply method repeatedly different value labels or or consider one.

here more general method of looking variables values of 2 have value label "no" attached. warning: should change dataset. make sure save 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         }     } 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -