/**********************************************************************/


data profit;                
   input person $ job1-job3;
   cards;                   
A 450 920 585               
B 300 690 700               
C 420 690 660               
;                           


/**********************************************************************/


   /* solve the problem */      
proc assign maximum data=profit;
   cost job1-job3;              
   id person;                   
run;                            


/**********************************************************************/


   /* print the solution */         
proc print;                         
   sum _fcost_;                     
   title 'Basic Assignment Problem';
run;                                


/**********************************************************************/


data values;       
   input job1-job3;
   cards;          
450 670 900        
390 750 875        
420 885 800        
500 780 950        
;                  


/**********************************************************************/


proc iml;                                             
                                                      
/* This module analyzes n1 problems involving       */
/* n people each, where the people have             */
/* ratings called pvals.                            */
start assign(pvals); /* start the module ASSIGN     */
                                                      
      /* read the data set and                      */
      /* create a matrix of job values              */
   use values;                                        
   read all into xmat;                                

   n1=nrow(xmat);  /* number of problems            */      
   n=nrow(pvals);  /* number of people to assign    */      
                                                            
      /* DO loop to analyze n1 problems             */      
   do i=1 to n1;                                            
                                                            
         /* select each row of the                  */      
         /* job value matrix and sort it            */      
      newx=xmat[i,];                                        
      rnewx=newx;                                           
      newx[rank(newx)]=rnewx;                               
                                                            
         /* sort the employee ratings               */      
      rvals=pvals;                                          
      pvals[rank(pvals)]=rvals;  /* ascending sort  */      
                                                            
         /* calculate the total profit (cost) and   */      
         /* append it to a vector called PROFITS    */      
      profits=profits//sum(pvals#newx);                    
   end;                          /* of do i=1 to n1 */      
                                                            
   problem=(1:n1)`; /* label for the output   */
                                                            
      /* print the total profits for the problems   */      
   print problem profits[f=dollar10.2];                     
finish;                 /* end of the module ASSIGN */      
                                                            
   /* specify employee ratings for current example  */      
ratings={.50, .75, .90};                                    
                                                      
run assign(ratings);  /* run the module             */


/**********************************************************************/


rvals=pvals;                          
pvals [n-rank(pvals)+1]=rvals;


/**********************************************************************/


profmat=newx@pvals;                                   
create profits from profmat[colname={job1 job2 job3}];
append from profmat;                                  


/**********************************************************************/


data persons;            
   input person $ @@;    
   cards;                
A B C                    
;                        
                         
data profits;            
   merge persons profits;
run;                     


/**********************************************************************/


proc print data=profits;
   title 'Profits Data';
run;


/**********************************************************************/


proc iml;                                             
      /* employee ratings                           */
   ratings={.2 .25 .35,                               
            .45 .5 .3,                                
            .65 .75 .2};                              
   jobs={1000 2000 3000};  /* job values            */
   profmat=ratings#jobs;   /* create profits matrix */
                                                      
   print profmat;  /* print profits matrix          */
                                                      

/**********************************************************************/


   /* create the PROFITS data set from the PROFMAT matrix */
create profits from profmat[colname={job1 job2 job3}];      
   append from profmat;                                     
quit;  /* exit SAS/IML software                           */
                                                            
   /* add the PERSON variable to the PROFITS data set     */
data persons;                                               
   input person $ @@;                                       
   cards;                                                   
A B C                                                       
;                                                           
                                                            
data profits;                                               
   merge persons profits;                                   

   /* run PROC ASSIGN                                     */
proc assign maximum data=profits;                           
   cost job1-job3;                                          
   id person;                                               
run;                                                        
                                                            
   /* print the results                                   */
proc print;
   title 'Assigning People to Jobs';
   sum _fcost_;                     
run;                                

                                                 
/**********************************************************************/


proc iml;                                                
                                                         
      /* matrix of employee ratings                    */
   ratings={.2 .25 .35,                                  
            .45 .5 .3,                                   
            .65 .75 .2};                                 
                                                         
      /* matrix of job values                          */
   jobs={1000 2000 3000,                                 
         1500 1400 2000,                                 
         2500 1300 2200,                                 
         1600 2100 1300};                                
                                                         
   n=nrow(jobs);    /* number of assignment problems   */
   n1=nrow(ratings); /* number of people to assign     */
                                                         
      /* DO loop to analyze n problems                 */
   do i=1 to n;                                          
      num=j(n1,1,i); /* create the problem identifier  */
                                                         
      /* create the assignment problem matrices,       */
      /* append the identifier to each matrix,         */
      /* append each matrix to the previous one        */
      profmat=profmat//(num||(ratings#jobs[i,]));        
   end;  /* of DO loop                                 */
                                                         
   print profmat;                                        

/**********************************************************************/


create profits from profmat[colname=                 
       {problem job1 job2 job3}];            
   append from profmat;                                  
quit; /* exit SAS/IML software                         */

proc assign maximum data=profits;   
   cost job1-job3;                  
   by problem;                      
run;                                
                                    
proc print;                         
   title 'Assigning People to Jobs';
   sum _fcost_;                     
   by problem;                      
run;                                


/**********************************************************************/