I'm currently debugging an AI script that uses similar logic (except I target the most known about enemy). The way I do it is using a small loop to check all enemy units, like this:
Set up an east present trigger over all units you want to check. In the condition line put
eastunits = thislist.
In your script insert the following loop:
_distance = 9999
_target = objnull
_i = (count eastlist) - 1
#loop
_unit = eastlist select _i
? not alive _unit : goto "next"
? player distance _unit < _distance : _distance = player distance _unit; _target = _unit
#next
_i = _i - 1
? _i >= 0 : goto "loop"
At the end of this loop your nearest unit will be called _target, unless it is further than 9999m away, in which case there will be no target.
Hope that helps.