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: repeatedly shuffle number sequence

From   Clinton Thompson <>
Subject   st: repeatedly shuffle number sequence
Date   Tue, 25 Oct 2011 10:08:10 +0200

Hello All,

I'm using Stata/SE 11.2 for WIndows.

This is a question that is part programming, part efficiency, and part
style.  Consider a sequence of numbers, say [1,10], that I want to
shuffle/randomize several times such that I end up w/ k variables
where each of the variables created contains a random shuffling of the
values [1,10].  I approached this using a rather simple and
rudimentary -foreach- loop:

>>>>>>>>>>>>> BEGIN >>>>>>>>>>>

set obs 10
set seed 20111025

foreach num of numlist 1/5 {
  gen int seq`num' = _n
  gen rand`num' = runiform()
  sort rand`num'
  drop rand`num'

<<<<<<<<<< END <<<<<<<<<<<<<

This approach works -- in the sense that k variables are created where
each variable contains a random shuffling of the numbers from 1-10 --
but I'm not sure if this the best way to approach this kind of
problem.  Does the creation of a -wide- dataset (as in my approach)
make the most sense (I'll be expanding this to 20-25 variables instead
of the 5 currently programmed)?  And I can easily change the sequences
of the values for all of the seq* variables depending on which of the
rand* variables is sorted but this doesn't seem too robust.  Any
thoughts or advice on whether this is the best (read:  correct and
most efficient?) approach to this problem is most appreciated.

Many thanks!
~Clint Thompson
*   For searches and help try:

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