Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Re:Assigning Students Randomly to Classes of Heterogeneous Sizes

From   Mike Lacy <>
Subject   st: Re:Assigning Students Randomly to Classes of Heterogeneous Sizes
Date   Sun, 24 Apr 2011 12:29:34 -0600

Andy Baxter <> wrote:

>I am using Stata 11 on a dataset of students and their class
>assignments that is structured like this:
>_studentid  _year _school _grade _classid
>Now, I want to randomly assign the students to classes--within the
>same year, school, and grade--while keeping the existing class sizes
>the same.
>I would be grateful for any code that would accomplish this.

If I understand the question correctly, this can be accomplished by permuting the class listings
within year, school, and grade, and merging them back onto the original data.

// example data
set obs 60
gen int id = _n
gen byte year = 1 + int(2 * runiform())
gen byte school = 1 + int(2 * runiform())
gen byte grade = 1 + int(3 * runiform())
gen byte class = 1 + int(5 * runiform())
// A little program to save the data that saves the data that -permute- feeds it.
cap prog drop saveit
prog saveit, rclass
  args file
  keep id class
  rename class newclass
  save `file', replace
  return scalar something = -1  // permute wants something
tempfile temp
permute class r(something), reps(1) strata(year school grade): saveit `temp'
merge 1:1 id using `temp'
// eyeball to check
sort year school grade


Mike Lacy, Assoc. Prof.
Soc. Dept., Colo. State. Univ.
Fort Collins CO 80523 USA

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index