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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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