Recode multiple binary variables

Accepted Solution Solved
Reply
Regular Contributor
Posts: 150
Accepted Solution

Recode multiple binary variables

In the BRFSS dataset the binary yes/no variables are coded as 1=Yes and 2=No, which is different than I normally use. Is there an easy way to recode all of the binary variables, or at least those I select, at the same time, 0=No and 1=Yes. This makes analysis simpler and we are giving this data to students to learn from. Thanks in advance for your help.


Accepted Solutions
Solution
‎05-28-2014 01:55 PM
Super User
Posts: 10,500

Re: Recode multiple binary variables

I use an array, do you want to REPLACE the existing values or create new variables(recommended);

Data want;

     set have;

     array q <current variables coded 1,2, (and likely 7, 9 )>;

     array Qnew <list of variables to contain the the binary values, in order matching above>;

     do _i_ = 1 to dim(q);

          if q[_i_] in (7,9) then qnew[_i_]= .;

          else qnew[_i_] = (q[_i_] = 1);

     end;

run;

If you want to overwrite the existing values then just use q[_i_] and skip the Qnew array. BUT if you do, make sure you create a new data set.


View solution in original post


All Replies
Trusted Advisor
Posts: 1,204

Re: Recode multiple binary variables

Hi,

I just put 3 variables as a templete. You can modify this to get the desired output.

Thanks,

Naeem

data have;
input x $ y $ z $;
datalines;
yes  no  no 
no   yes yes
no   no  yes
yes  yes no
no   yes no
yes  no  yes
;

data want (drop=i);
set have;
array a{3}  x y z;
array b{3}  x_ y_ z_;
do i=1 to dim(a);
  if a(i)='yes' then b(i)=1;
  else b(i)=0;
end;
run;

Using proc format.

proc format;
value $bin
'yes' = 1
'no'  = 0;
run;

data want;
set have;
format x y z $bin.;
run;

Solution
‎05-28-2014 01:55 PM
Super User
Posts: 10,500

Re: Recode multiple binary variables

I use an array, do you want to REPLACE the existing values or create new variables(recommended);

Data want;

     set have;

     array q <current variables coded 1,2, (and likely 7, 9 )>;

     array Qnew <list of variables to contain the the binary values, in order matching above>;

     do _i_ = 1 to dim(q);

          if q[_i_] in (7,9) then qnew[_i_]= .;

          else qnew[_i_] = (q[_i_] = 1);

     end;

run;

If you want to overwrite the existing values then just use q[_i_] and skip the Qnew array. BUT if you do, make sure you create a new data set.


Regular Contributor
Posts: 150

Re: Recode multiple binary variables

Thank you, I will try the code shortly. For the purposes of this I am going to replace the existing values but normally I would create new variables. Thank you!

Super User
Posts: 10,500

Re: Recode multiple binary variables

The reason I emphasized making a new data set is that I have inherited data that someone ran similar recodes on multiple times replacing the variables. In at least one case the base data was no longer available so those variables were useless.

Regular Contributor
Posts: 150

Re: Recode multiple binary variables

I was able to get it to work, thank you. And I agree with you, I always keep the old dataset when I am manipulating data. Thank you for your help.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 640 views
  • 0 likes
  • 3 in conversation