www.thecompleteuniversityguide.co.uk states that Edinburgh has research quality of 82% for Economics and St Andrews has 74%. 91% of Economics graduates from Edinburgh are currently doing something related to their future plans, 84% at St Andrews. St Andrews manages to get higher than Edinburgh overall for this subject because of entry standards and student satisfaction. Because St Andrews is a much smaller university, and because it is increasingly popular, they can afford to ask for higher grades sometimes than Edinburgh because St Andrews won't have any trouble filling spaces because they're well known in America as a very old, attractive, quaint looking, more affordable, alternative to somewhere similar looking like Princeton. St Andrews require ABB minimum (based on A-Level there) but at present AAA is usually the typical entry standard. Edinburgh seem to be less easy to find what their minimum or typical entry standard are.
It's not just size that hinders St Andrews QS rankings. Edinburgh is just technically academically better overall anyway. 76% of Edinburgh's staff conduct high quality research, with an average quality standard of 85%. At St Andrews it's 71% of staff and 83% quality.
So why do so many people go on about St Andrews when the likes of Lancaster, Sheffield, Bath, Southampton, Bristol and KCL are technically better in research?
Because it looks pretty, because of its traditions, because it's very old and respected enough to get noticed, because its relatively small size and the nature of the place mean some people say it's like joining a family, because of who it attracts. 40% of its intake are from private schools, more than any other UK university.