Create new variable from multiple old variables

Accepted Solution Solved
Reply
Regular Contributor
Posts: 150
Accepted Solution

Create new variable from multiple old variables

I have about 23 variables that are all coded 2=yes/1=no. I need to create a new variable that says any combo of them together=yes,  and if they only said yes on one of the variables then it the new variable=no. I thought this would work but it does not seem to be. How do I specify that is

varnum=sum(var1, var2, var3, etc...);

if varnum>1 then multivar=1;

if varnum=1 then multivar=0;

run;


Any suggestions would be greatly apprecaited.


Accepted Solutions
Solution
‎08-05-2013 01:33 AM
Super User
Super User
Posts: 7,079

Re: Create new variable from multiple old variables

Posted in reply to rfarmenta

It would be easier if you coded them 1=yes, 0=no but you can use a little arithmetic to solve this.

Sounds like you want a Boolean (0/1) flag that means they answered YES on MORE than one question.


multivar=  1 < (sum(of var1-var23) - N(of var1-var23) );


View solution in original post


All Replies
Solution
‎08-05-2013 01:33 AM
Super User
Super User
Posts: 7,079

Re: Create new variable from multiple old variables

Posted in reply to rfarmenta

It would be easier if you coded them 1=yes, 0=no but you can use a little arithmetic to solve this.

Sounds like you want a Boolean (0/1) flag that means they answered YES on MORE than one question.


multivar=  1 < (sum(of var1-var23) - N(of var1-var23) );


Regular Contributor
Posts: 150

Re: Create new variable from multiple old variables

Thank you all for your suggestions, Tom's suggestion did exactly what I wanted it to do. These forums are the best and I am thankful you all take time out of your days to help with coding and fixing problems.

Super Contributor
Posts: 644

Re: Create new variable from multiple old variables

Posted in reply to rfarmenta

Your code does not work because the minimum value of varnum (when all = no) will be the count of all vars, so it will only be 1 if you only have one var.

I can't think of a single function (unless you transpose the data and use SQL to count the yes responses) that will do what you want.

An array will meet the requirement

     Array response {*} <first-var) -- <last-var> ; 

          /* double dash - assuming all the var columns are together, otherwise list them all */

     yes_count = 0 ;

     Do i = 1 to dim (response) ;

          yes_count = (response{i} = 2) ;

     End ;

     multivar = (yes_count > 2) ;

Code relies on 1 = true, 0 = false.

Richard

Super Contributor
Posts: 297

Re: Create new variable from multiple old variables

Posted in reply to rfarmenta

Hi Rfarmenta,

I think Tom's solution is fantastic and I would never have thought of using the N function - I always forget about it to be honest.  This would have been my way of doing it.

DATA WANT;

SET HAVE;

VARNUM =  SUM(OF VAR1-VAR23);

IF VARNUM > 24 THEN MULTIVAR1=1;

ELSE MULTIVAR1=0;

RUN;

Like I said, Tom's is the best answer, but this was my initial thought when I read your post, so I thought I would put it up as well.

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 325 views
  • 6 likes
  • 4 in conversation