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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.