BookmarkSubscribeRSS Feed
Sbon08
Calcite | Level 5

Hi everyone, my dataset in excel has multiple columns for race. Each column has a 1 if someone chose that race and a blank they did not (they would have a 1 in another column). I am trying to combine these all into one race variable for a logistic regression. Attached is a picture of how the raw data looks: Screen Shot 2019-03-08 at 1.53.25 PM.png

1 REPLY 1
ballardw
Super User

@Sbon08 wrote:

Hi everyone, my dataset in excel has multiple columns for race. Each column has a 1 if someone chose that race and a blank they did not (they would have a 1 in another column). I am trying to combine these all into one race variable for a logistic regression. Attached is a picture of how the raw data looks: Screen Shot 2019-03-08 at 1.53.25 PM.png


Is this going to be a dependent variable or independent in the model?

What would the values of your one value look like? Are all of the Q2_1 through Q2_9 to be combined or does one of those mean something such that it should not be combined such as a "refused to answer" code that really should not occur with other values?

And are the variables numeric or character?

By any chance did a previous step replace zero (0) values with missing? If so remove that step and something like this would work:

data example;
   input Q2_1 - Q2_4;
   length r $ 4;
   r = cats(of Q2_:);

datalines;
1 0 0 0
1 0 1 0
0 0 0 1
; 
run;

Using only 4 variables as I'm too lazy to make 10 when 4 demonstrates what goes on.

You would need a place holder in any case.

The following code shows one way to replace 0 for missing values.

data example;
   informat Q2_1 - Q2_4 $1.;
   input Q2_1 - Q2_4 ;
   array q  Q2_1 - Q2_4;
   do i= 1 to dim(q);
      if missing(q[i]) then q[i]='0';
   end;
   length r $ 4;
   r = cats(of Q2_:);
   drop;
datalines;
1 . . .
1 . 1 .
. . . 1
; 
run;

If you variables are numeric the only real change to the code (other than not reading data) is to use =0; instead of ='0';

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 635 views
  • 0 likes
  • 2 in conversation