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.
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.
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;
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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: