BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rfarmenta
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

5 REPLIES 5
stat_sas
Ammonite | Level 13

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;

ballardw
Super User

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.


rfarmenta
Obsidian | Level 7

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!

ballardw
Super User

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.

rfarmenta
Obsidian | Level 7

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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