Nearest is actually not that hard. Just needs some thinking to get straight.
First you enumerate(pick every unit in group) a group of units.
Set a real variable to 999999 or any other high number and a unit variable to "no unit".
Check for each unit in the group(pick every unit in group) if they are closer than the real variable, if so, then set the real to distance between them and your point of reference.