Materials based on the Sm-Co system exhibit remarkable magnetic performance due to their high Curie temperature, large saturation magnetization, and strong magnetic anisotropy, which are the result of the electronic structure in Co and Sm and their arrangement in the hexagonal lattice. In this paper we show, using first-principles calculations, mean-field theory, and atomistic Monte Carlo simulations, that slight modifications of the SmCo5 crystal structure, induced by strain or partial substitution of Sm by Ce, change the exchange interaction and increase the magnetocrystalline anisotropy energy drastically. This finding shows how small changes in local-structure environments can generate substantial changes in macroscopic properties and thus enable the optimization of high-performance materials via tailoring at the atomic level.