Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Randomly Match 2 lists of names

Status
Not open for further replies.

tweaked2005

Technical User
Jun 21, 2005
20
US
I'm stumped. I need to create a SAS program that randomly matches names for dinner. There are a few criteria to consider:

There are 3 categories of folks, those that can go to dinner in groups of 3 only, can go to dinner in groups of 2 only, or are indifferent.

The program needs to match folks up for dinner randomly, and then record (perhaps in a new variable) that those folks have gone to dinner. This is so they will not be matched again in the near future.

I've listed the code I have completed below. There are some obvious problems - someone in the "indifferent" category could very well be matched with someone else in the "indifferent" category - but as it stands that is not happening. For now I'm trying to chip away at the different pieces of this program. Any help would be greatly apreciated.

The dataset out.participants contains the following variables:

firstName, lastName, group (the category each person is in), center, division


Code:
libname out 'c:\';                                                             
                                                                              
data group1; set out.participants;                                            
        if group=1;                                                           
        rannum=ranuni(0);                                                     
                                                                              
data group2; set out.participants;                                            
        if group=2;                                                           
        rannum=ranuni(0);                                                     
                                                                              
data group3; set out.participants;                                            
        if group=3;                                                           
        rannum=ranuni(0);                                                     
                                                                              
proc sort data=group1; by rannum;                                             
proc sort data=group2; by rannum;                                             
proc sort data=group3; by rannum;                                             
                                                                              
data group1  (rename=(                                                        
                                firstName=fname1                              
                                lastName=lname1                               
                                group=g1                                      
                                center=c1                                     
                                division=d1)                                  
                                        ); set group1;                        
        n=_n_;                                                                
                                                                              
data group2  (rename=                                                         
                                (                                             
                                firstName=fname2                              
                                lastName=lname2                               
                                group=g2                                      
                                center=c2                                     
                                division=d2                                   
                                )                                             
                          ); set group2;                                      
        n=_n_;                                                                
                                                                              
data group3  (rename=(                                                        
                                firstName=fname3                              
                                lastName=lname3                               
                                group=g3                                      
                                center=c3                                     
                                division=d3)                                  
                                        ); set group3;                        
        n=_n_;                                                                
                                                                              
proc sort data=group1; by n;                                                  
proc sort data=group2; by n;                                                  
proc sort data=group3; by n;                                                  
run;                                                                          
                                                                              
data match1; merge group1 group2;                                             
        by n;                                                                 
run;                                                                          
proc print; run;
 
How about this method.
1 - Split the people into 2 groups, people who will only go in 3s (group1) and people who will only go in 2s (group2).
2 - Divide the indifferent people equally between the two.
3 - Sort each by a random number.
4 - start to go through each dataset running a counter, and aggregate up the names into person 1, person 2 (person3) and output the record when you have the right number of people.

This should give you a completely random mix. If you use a different seed in the RanUni function each time you run it, you should end up with different groupings each time.
 
Also, here's some programming tips.

1 - This:-
Code:
data group3;
  set out.participants;
  if group=3;

Is more efficient if coded like this:-
Code:
Data group3;
  set out.participants(where=(group=3));

2 - Always end your steps and procs with a "run;". I know that you don't HAVE to put them there, but it makes it easier to read, and also avoids issues on those occasions when you DO have to have the run statement there. ODS for instance can leave you with a blank output page if you don't put your run statement before the "ODS ... close;". It's best practice to always put in your run statements.

3 - Look up "Match Merge" in the doco. It's basically doing a merge, but not specifying a "BY" statement. IT merges record 1 on the first dataset to record 1 on the second dataset etc.
 
Thanks so much for the help. Any thoughts on how to still make sure indifferent could match up with other indifferents?

Again - thanks for the logic help - super appreciated.
 
Using my method would still allow for indifferents to match up with indifferents, as they'd be randomly mixed in with the others. If you don't have many indifferents compared to 2s and 3s then you'll end up with very few ind-ind matches.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top