Which are the advantages of using a ramjet compared to a rocket engine in an air-to-surface missile? I suppose the cost rises a lot, so why do some countries use ramjets as a way of propelling ASM missiles?
A rocket-powered missile carries both fuel and oxidiser and oxidiser is the heavier part. For example a gram of jet fuel needs almost 3.4 grams of oxygen to fully burn! Increasing the fuel load requires also increasing the power, which means even more fuel and this limits the practical range of a missile.
Ramjets are air-breathing, so they don't need to carry the oxidiser, which means the same amount of fuel gives them significantly longer range.
Longer range means the aircraft can fire the missile further away and avoid coming into range of the defenses placed around the target, which might make difference between being able to attack a heavily defended target and not, so it is worth the expense to have some in stock.
Also note that while ramjet is only more complicated, and expensive, than a solid-fuel rocket motor. Compared to liquid-fuel rocket it is simpler, because it only needs easy-to-handle kerosene while liquid-fuel rockets either need liquid oxygen, or nasty stuff like peroxide or hydrazine. It is also simpler compared to a full turbofan or turbojet (a small solid rocket booster is usually used to get it up to speed).
Simplicity of propulsion translates to cost savings, but there are tradeoffs in using a ramjet in a missile. The reason most aircraft-carried missiles do not use ramjet engines is because such a missile has to be accelerated to a sufficient speed for the engine to begin producing thrust.
"A ramjet equipped aircraft requires another type of propulsion to accelerate it to a speed at which the ramjet is capable of producing thrust. A ramjet can theoretically be started at speeds as low as 100 knots but it does not start to produce any significant thrust until the airspeed reaches approximately mach 0.5. Even at this speed, efficiency is very low and peak efficiency will not be attained until reaching supersonic speeds in the realm of mach 3."
What this means is that such a missile can only be carried by aircraft that can fly at mach 0.5 or above, or they have to be equipped with some other means of propulsion to accelerate the missile so the ramjet can start operating. It is much simpler to equip it with a rocket or turbine engine, depending on the requirements of the design.
Speed, size and cost. Rockets engines are smaller and faster then any air breathing engine. In retrospect this would be ok engine for a smart missile that can track slower fighter aircraft like a fast drone.