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.
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.
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;
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.
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!
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.