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

Hello everyone,

 

I need to create a new binary variable (yes =1, no = 0) based on if ≥2 of a set of other binary variables are =1 per individual in the cohort.

 

In other words, I could name my new variable "multi_sport_athlete" and would set it as 1 only if this same individual played 2 or more of any sports, with variables such as "tennis" = 1, "football" = 1 , "rugby" = 1, "swimming" =1 .

 

Any help is greatly appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

If you have numeric 0/1 coded variables then you can use the SUM function to get the number of values equal to 1.

 

But the layout of your data is needed to recommend exact code. Provide an example of the needed variables in the form of a data step and what the expected result for the example data might be.

 

If you have variables (which are very seldom in quotes in SAS code) named Tennis, Football,Rugby and swimming then a data step such as:

data want;
   set have;
   Multi_sport_athlete= ( sum(tennis,football, rugby, swimming) ge 2);
run;

should work. SAS will return 1 for true and 0 for false in a comparisons such as Somevar = ( <comparison code goes here>);

Note that if all the sport variable values are missing this will assign 0. So if you don't want that behavior you need to be a bit more descriptive about rules.

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

Sum the binary variables, and test to see if this sum is greater than or equal to 2.

 

if sum(football,tennis,rugby,swimming)>=2 then multi_sport_athlete=1;
--
Paige Miller
ballardw
Super User

If you have numeric 0/1 coded variables then you can use the SUM function to get the number of values equal to 1.

 

But the layout of your data is needed to recommend exact code. Provide an example of the needed variables in the form of a data step and what the expected result for the example data might be.

 

If you have variables (which are very seldom in quotes in SAS code) named Tennis, Football,Rugby and swimming then a data step such as:

data want;
   set have;
   Multi_sport_athlete= ( sum(tennis,football, rugby, swimming) ge 2);
run;

should work. SAS will return 1 for true and 0 for false in a comparisons such as Somevar = ( <comparison code goes here>);

Note that if all the sport variable values are missing this will assign 0. So if you don't want that behavior you need to be a bit more descriptive about rules.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 1204 views
  • 1 like
  • 3 in conversation