We report results of ab initio calculations on (GaN) n nanoclusters with n = 12, 16, 22, and 24 using generalized gradient approximation (GGA) for the exchange-correlation energy. We find cage structures of GaN clusters to be lower in energy as compared to bulk fragments which have been fully optimized. Selected cages have been doped with Gd and Nd atoms by substituting Gd and Nd on Ga sites, respectively. The atomic structures of such doped nanoclusters have been optimized using spin-polarized GGA and by including all f electrons on the rare earth atoms. We find the doped nanoclusters to be magnetic with 7 μ B (3 μ B ) magnetic moments when 1 Gd (Nd) atom is doped. When 2 Gd (Nd) atoms are substituted on different Ga sites, there is often a ferromagnetic coupling between the rare earth atoms, but we also find zero net magnetic moment in the nanocluster depending upon the separation between the doped atoms suggesting that both ferromagnetic and antiferromagnetic couplings may occur between the rare earth dopants.