Pollution is indeed an unfortunate reality that expats moving to Beijing have to face. This can be mitigated to an extent by installing air purifiers in your home and car (if you commute by car). It is generally recommended that face masks be worn when outside walking or cycling, though there is some debate in scientific and medical circles on whether the masks are actually effective in filtering the air.
The longer you live in Beijing, the more pronounced the effect on your health is likely to be. Two years or so is unlikely to have much of an impact in the long term, but staying longer than that may bring a risk of health problems developing.
Shanghai is definitely less polluted than Beijing, so that's a good option - however, if your commute is going to be a long one via public transport and/or walking or cycling, the effect of this should be considered too since you'll be breathing in pollution during this commute twice a day. On the other hand, going in using a car with an air filter installed should be okay.
Hope this helps. Best of luck with your move.