  To study how nitrogen contributes to perpendicular magnetocrystalline
anisotropy (PMA) in the ferrimagnetic antiperovskite Mn$_4$N, we examined both
the fabrication of epitaxial Mn$_4$N films with various nitrogen contents and
first-principles density-functional calculations. Saturation magnetization
($M_{\rm s}$) peaks of 110 mT and uniaxial PMA energy densities ($K_{\rm u}$)
of 0.1 MJ/m$^3$ were obtained for a N$_2$ gas flow ratio ($Q$) of $\sim 10 \%$
during sputtering deposition, suggesting nearly single-phase crystalline
$\epsilon$-Mn$_4$N. Segregation of $\alpha$-Mn and nitrogen-deficient Mn$_4$N
grains was observed for $Q \approx 6\%$, which was responsible for a decrease
in the $M_{\rm s}$ and $K_{\rm u}$. The first-principles calculations revealed
that the magnetic structure of Mn$_4$N showing PMA was "type-B" having a
collinear structure, whose magnetic moments couple parallel within the c-plane
and alternating along the c-direction. In addition, the $K_{\rm u}$ calculated
using Mn$_{32}$N$_x$ supercells showed a strong dependence on nitrogen
deficiency, in qualitative agreement with the experimental results. The
second-order perturbation analysis of $K_{\rm u}$ with respect to the
spin-orbit interaction revealed that not only spin-conserving but also
spin-flip processes contribute significantly to the PMA in Mn$_4$N. We also
found that both contributions decreased with increasing nitrogen deficiency,
resulting in the reduction of $K_{\rm u}$. It was noted that the decrease in
the spin-flip contribution occurred at the Mn atoms in face-centered sites.
This is one of the specific PMA characteristics we found for
antiperovskite-type Mn$_4$N.
