Execute as selector if nearest entity has a tag
Use the as
parameter to switch executor to the armour stand at the end.
execute
at @e[type=armor_stand,limit=1,sort=nearest] # Move the location to the armour stand
as @e[type=pig,limit=1,sort=nearest] # NEAREST PIG NOW HAS CONTROL
if entity @s[tag=carrot] # If I have the tag carrot, continue to the next step, otherwise stop.
as @e[type=armor_stand,limit=1,sort=nearest] # ARMOUR STAND NOW HAS CONTROL
say The nearest pig to me has the carrot tag!
The execution position before the last as
is still at the armour stand, so the selector always finds the armour stand that started, except if there are multiple at the exact same coordinates.