Well assigning them based on sequence is very simple:
data students;
name="abc"; output;
name="def"; output;
name="egh"; output;
name="wer"; output;
name="lhj"; output;
run;
data teachers;
teacher="xyz"; teach=1; output;
teacher="iop"; teach=2; output;
run;
data temp;
set students;
if _n_<=3 then teach=1;
else teach=2;
run;
proc sql;
create table WANT as
select A.TEACHER,
B.NAME
from TEACHERS A
left join TEMP B
on A.TEACH=B.TEACH;
quit;
Just assign them an id, each teacher has a unique id, each student is grouped based on _n_. However I feel your other rules are going to be more complicated, i.e. if 90% of students want subject A, then how to arrive at which % of them go into one or the other.