I would like to create a random schedule for x number of players, from multiple teams, with many different abilities, and possibly other attributes.
Also the previous matches should be taken into account so that there are no repeat matches from previous matches.
Player profiles (Have):
Name | Team | Ability |
Player1 | Team1 | Level1 |
Player2 | Team1 | Level1 |
Player3 | Team1 | Level1 |
Player4 | Team1 | Level1 |
Player5 | Team1 | Level2 |
Player6 | Team1 | Level2 |
Player7 | Team1 | Level2 |
Player8 | Team1 | Level2 |
Player9 | Team2 | Level1 |
Player10 | Team2 | Level1 |
Player11 | Team2 | Level1 |
Player12 | Team2 | Level1 |
Player13 | Team2 | Level2 |
Player14 | Team2 | Level2 |
Player15 | Team2 | Level2 |
Player16 | Team2 | Level2 |
Previous Matches (Have2):
Player1 vs Player9
Player2 vs Player10
Player3 vs Player11
Player4 vs Player12
Player5 vs Player13
Player6 vs Player14
Player7 vs Player15
Player8 vs Player16
Want:
Random output of matches. For example:
Player1 vs Player10
Player2 vs Player11
etc
Any help is truly appreciated
Code:
data players;
input Name $ Team $ Ability $;
cards;
Player1 Team1 Level1
Player2 Team1 Level1
Player3 Team1 Level1
Player4 Team1 Level1
Player5 Team1 Level2
Player6 Team1 Level2
Player7 Team1 Level2
Player8 Team1 Level2
Player9 Team2 Level1
Player10 Team2 Level1
Player11 Team2 Level1
Player12 Team2 Level1
Player13 Team2 Level2
Player14 Team2 Level2
Player15 Team2 Level2
Player16 Team2 Level2
;
run;
data matches;
input Pl1 $ Pl2$;
cards;
Player1 Player9
Player2 Player10
Player3 Player11
Player4 Player12
Player5 Player13
Player6 Player14
Player7 Player15
Player8 Player16
;
run;
proc sql;
create table possible_matches as
select a.Name as Pl1, b.Name as Pl2/*, a.Team as Pl1Team, b.Team as Pl2Team, a.Ability as Pl1Ability, b.Ability as Pl2Ability*/
from players (where=( Team='Team1')) a, players (where =(Team='Team2')) b
except
select Pl1, Pl2 from matches;
quit;
data possible_matches;
set possible_matches;
fred = ranuni(0);
run;
proc sort data=possible_matches;
by Pl1 fred;
run;
data next_match(drop=fred Pl2_exustive_lst f);
length Pl2_exustive_lst $30000;
set possible_matches;
retain Pl2_exustive_lst f;
by Pl1 fred;
if first.Pl1 then do;
f=1;
if f=1 and findw(Pl2_exustive_lst,Pl2) =0 then do;
Pl2_exustive_lst=cats(strip(Pl2_exustive_lst)||' , '||Pl2);
f=0;
output;
end;
end;
if f=1 and findw(Pl2_exustive_lst,Pl2) =0 then do;
Pl2_exustive_lst=cats(strip(Pl2_exustive_lst)||' , '||Pl2);
f=0;
output;
end;
run;
/*
data matches;
set matches next_match;
run;
*/
Please let us know if it worked.
1) Create a list of all possible pairs (self join, cartesian product)
2) Remove pairs from previous matches
3) Pick random pairs from remaining set
Not 100% sure but what Proc Plan provides would eventually also be worth investigating.
Code:
data players;
input Name $ Team $ Ability $;
cards;
Player1 Team1 Level1
Player2 Team1 Level1
Player3 Team1 Level1
Player4 Team1 Level1
Player5 Team1 Level2
Player6 Team1 Level2
Player7 Team1 Level2
Player8 Team1 Level2
Player9 Team2 Level1
Player10 Team2 Level1
Player11 Team2 Level1
Player12 Team2 Level1
Player13 Team2 Level2
Player14 Team2 Level2
Player15 Team2 Level2
Player16 Team2 Level2
;
run;
data matches;
input Pl1 $ Pl2$;
cards;
Player1 Player9
Player2 Player10
Player3 Player11
Player4 Player12
Player5 Player13
Player6 Player14
Player7 Player15
Player8 Player16
;
run;
proc sql;
create table possible_matches as
select a.Name as Pl1, b.Name as Pl2/*, a.Team as Pl1Team, b.Team as Pl2Team, a.Ability as Pl1Ability, b.Ability as Pl2Ability*/
from players (where=( Team='Team1')) a, players (where =(Team='Team2')) b
except
select Pl1, Pl2 from matches;
quit;
data possible_matches;
set possible_matches;
fred = ranuni(0);
run;
proc sort data=possible_matches;
by Pl1 fred;
run;
data next_match(drop=fred Pl2_exustive_lst f);
length Pl2_exustive_lst $30000;
set possible_matches;
retain Pl2_exustive_lst f;
by Pl1 fred;
if first.Pl1 then do;
f=1;
if f=1 and findw(Pl2_exustive_lst,Pl2) =0 then do;
Pl2_exustive_lst=cats(strip(Pl2_exustive_lst)||' , '||Pl2);
f=0;
output;
end;
end;
if f=1 and findw(Pl2_exustive_lst,Pl2) =0 then do;
Pl2_exustive_lst=cats(strip(Pl2_exustive_lst)||' , '||Pl2);
f=0;
output;
end;
run;
/*
data matches;
set matches next_match;
run;
*/
Please let us know if it worked.
Thanks Satish. The Ability though is not taken into consideration. How do I add Ability into the where statement:
select a.Name as Pl1, b.Name as Pl2, a.Team as Pl1Team, b.Team as Pl2Team, a.Ability as Pl1Ability, b.Ability as Pl2Ability
from players (where=( Team='Team1')) a, players (where =(Team='Team2')) b
It should be fairly simple to Customize this code to your need.
e.g:
If you want next Matches to be only higher/Lower skilled players. -You can add a where clause in that join statement.
Please mark the Post as result if your issue is solved.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.