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

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):

NameTeamAbility
Player1Team1Level1
Player2Team1Level1
Player3Team1Level1
Player4Team1Level1
Player5Team1Level2
Player6Team1Level2
Player7Team1Level2
Player8Team1Level2
Player9Team2Level1
Player10Team2Level1
Player11Team2Level1
Player12Team2Level1
Player13Team2Level2
Player14Team2Level2
Player15Team2Level2
Player16Team2Level2

 

 

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

1 ACCEPTED SOLUTION

Accepted Solutions
Satish_Parida
Lapis Lazuli | Level 10

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.

View solution in original post

5 REPLIES 5
PGStats
Opal | Level 21

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

PG
Patrick
Opal | Level 21

Not 100% sure but what Proc Plan provides would eventually also be worth investigating.

Satish_Parida
Lapis Lazuli | Level 10

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.

hypart
Calcite | Level 5

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

Satish_Parida
Lapis Lazuli | Level 10

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.

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
  • 5 replies
  • 542 views
  • 0 likes
  • 4 in conversation