There are some uncertainties in what you have posted so I won't convert your numbers into a data step and write code.
In general, it occurs to me that you have two kinds of records; 1) those with an id and a grp and 2) those with just an id. Within the second group you also have two kinds of records, 3) those with an id that matches a record with that id that has a grp, and 4) those that do not match a record on id.
1 is done.
3 is a match merge, find records that match the id in question and give them the group from the matching record.
4 strikes me as something that might could be done with proc surveyselect. It will certainly randomly assign essentially equal groups to all the unassigned ids.
Whether you would then need to do some additional evening out of group numbers I couldn't say.
... View more