Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Random group generation

    Dear Statalists,

    I would like to generate 67 random groups for 271 observations. I know the below code might work:

    set obs 271
    range no 1 271
    gen r= uniform()
    gen group=1
    sort r
    replace group=2 in 5/8
    ...

    However, is there any better way that can be used to generate 67 random groups simultaneously?

    Thanks in advance.

    Best,
    Cong

  • #2
    Code:
    set obs 271
    range no 1 271
    set seed 1234 // OR YOUR FAVORITE SEED
    gen r = uniform()
    sort r
    gen group = ceil(_n/4)
    sort group no
    Note: 271 != 67*4, so you will actually have 68 groups, with the last group having only 3 members. In fact, 271 is a prime number, so there is no possibility of dividing 271 observations into groups of exactly equal size. If you prefer to have only 67 groups, with the last group containing 7 members, just add -replace group = 67 if group == 68- at the end of the code shown above.

    Comment


    • #3
      Dear Clyde, many thanks for the reply. That is very helpful! Best, Cong

      Comment

      Working...
      X