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

# Re: st: Unique Randomization within groups

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: Unique Randomization within groups Date Thu, 31 Jan 2013 12:25:30 +0000

Abstracting from the complicated context

clear
set obs 3
set seed <whatever>
gen y = runiform()
sort y
gen wanted = _n

gives you -wanted- that is 1, 2, 3. Which observations are assigned 1,
2, 3 is random by virtue of shuffling according to previously
generated random numbers. So use the sort order, not the random
numbers themselves.

(There still is a minute probability of ties. I wouldn't expect that
to bite in practice.)

On Thu, Jan 31, 2013 at 12:14 PM, Jonah Rexer <jorexer@gmail.com> wrote:
> Hello,
>
> I am doing a randomization among 3 groups (2 treatment and 1 control) for a
> micro experiment.
> Our sample is small, N=99 with the unit being a cluster (a group of 2-3
> respondents).
> I have merged clusters to assign each a random number. But instead of
> stratifying, since our sample is so small, I have organized each cluster
> into a group of 3 clusters (so 33 groups, each with 3 clusters) based on
> our stratification variables.
>
> What I want to do is assign a random integer, 1-3, within each group. I've
> used the command:
>
> set seed 4109
>
> bysort cluster_group : gen random1=floor((3-1+1)*runiform()+1)
>
> The problem is that this doesn't give me a unique number. Within each
> group, i need one observation (cluster) to be 1, one to be 2, and one to be
> 3. This is why I used bysort, but it doesn't seem to be making any
> difference. Is there any way to ensure that the number assigned is unique
> within the group?
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/