When I estimate a confidence interval for the mean over years/groups, the calculation of the inverse t distribution uses all observations for which the variable is non missing, and not just the number of observations in the group. Stata 14.2.

Code to replicate:
Code:
clear all
webuse nlswork
mean union, over(year)
ci means union if year == 70
mean union if year == 70

*Using the number of observations in year == 1970, same as ci means or mean if
di .2230576 + invt(798, (1.95/2)) *  .014746

*Using the total number of observations in all dataset, same as mean, over(year)
di .2230576 + invt(19238, (1.95/2)) *  .014746