There is no native function for that.
Instead, you can either:
A) Check whichever attribute is the highest. (but this only works for certain heroes, since the primary stat may not necessarily be the highest)
B) Create a database. (just store data for each hero, storing their primary attribute)
I recommend option B since option A doesn't work too often anyway.
Basically, for each hero on your map, you simply store whichever attribute is their primary attribute into a hashtable saved under their unit ID. For example, you could save under the paladin's unit-id "strength". Or something along those lines.
It is tedious, but once you have them all saved, then you can retrieve it just by loading the value stored under a unit's id and see whether it is strength, agility, or intelligence.