The bei vs. beim question actually has the same answer as the zu vs. zum question from last week (https://members.rocketlanguages.com/members/forum/german-grammar/zu-vs-zum). So in much the same way, the contraction beim is preferred over bei dem unless the dem is acting as a pronoun.
However, the example offered by your German friend, sfpugh, gives us an interesting exception. While we would generally say Ich war beim Arzt "I was at the doctor's", if we had to put emphasis on it in conversation, we could end up saying bei dem Arzt. For example, if a friend were asking us Bei welchem Arzt? "At which doctor's?" we could point at the office and say, Bei dem Arzt, da! "At that doctor's, there!"
I hope that this is helpful!
Bis zum nächsten Mal,