Wieght, yardage & grams--waht is more imp. for substitut

If I am going to look for a substitute yarn, I usually first look to see if the sub I want is the same weight (DK, worsted, etc). After that, however, which is more important, to buy the same yardage as the original pattern calls for, or the buy the same total grams given? It seems to me that if I go by grams, then I might not have enough yarn to make the size I want–unless there is something I do not understand about yarn (which is quite likely, seeing as I don’t know much!)
thanks for your insights!

I always check the weight (thickness), too. Then I check how many yards are in the original to make sure I buy enough of the substitute. I don’t bother with the actual weight (heaviness) of the yarn since different materials have different weights.