In a comparative study we calculated the spin and orbital moments, spin and charge densities, and 4 f crystal field ͑CF͒ parameters of the rare-earth transition-metal intermetallics Sm 2 Fe 17 , Sm 2 Fe 17 Z 3 (ZϭC,N͒, and Sm 2 Co 17 using a relativistic optimized linear combination of atomic orbitals method. The itinerant valence electron states were treated in the local-spin-density approximation ͑LSDA͒, whereas the localized 4 f states were described as open core states within the self-interaction-corrected LSDA. The calculations yield magnetic moments in good agreement with experiment. While all local moments of Sm 2 Fe 17 increase upon lattice expansion, the moments of atoms neighboring the interstitial sites decrease and those of more distant Fe atoms increase upon insertion of interstitial N or C. In N interstitial atoms all 2p ␣ orbitals are polarized antiparallel to their respective Fe and Sm neighbor atoms in the bond direction, whereas in C all 2p ␣ orbitals are polarized antiparallel to the Fe atoms neighboring the interstitial site. The second-order CF parameters A 2 0 dominating the rare-earth magnetocrystalline anisotropy are found to have the same sign and order of magnitude as those derived from magnetization data. In accordance with experiment the calculated negative A 2 0 is larger for the Co compound than for the Fe parent compound and is strongly increased upon insertion of interstitial N or C. The agreement between theory and experiment is improved by taking into account the CF contribution arising from the asphericity of the exchange-correlation potential of the non-4 f states.