How can I randomly assign observations to groups in Stata? | Stata FAQ

The trick here is to create a random variable, sort the dataset by that random variable,
and then assign the observations to the groups. Let’s use the hsb2 dataset as an example by
randomly assigning 50 observations to each of four groups.