## Generate random order within a set of number.

Occasional Contributor
Posts: 8

# Generate random order within a set of number.

[ Edited ]

Hi,

I have an experiment with 6 conditions and 180 participants. I want to generate numbers to randomly assign participants to one of the conditions. In the end I would like to get the same number of people (30) per condition. Is there a sas function that I can use to randomly generate integers from 1 to 6 to assign to them 180 participants?

Thanks,

Mickie

Posts: 3,799

30*8=240 not 180
SAS Super FREQ
Posts: 3,752

## Re: Generate random order within a set of number.

8 does not evenly divide into 180.  You say you want 30 people per condition, but that requires 240 participants.

At any rate, one way to do what you want is to use PROC PLAN in SAS/STAT software.  There is a Getting Started example that shows exactly what you ask.  Change the macro values in the following code to get random assignment for any number of participants and treatments.

``````%let numUnits = 180;    /* number of participants */
%let numTreat =   8;    /* number of treatments */

data Unrandomized;
do Unit=1 to &numUnits;
Treatment = 1 + mod(Unit-1, &numTreat);
output;
end;
run;

/* Randomize the design */
proc plan seed=27371;
factors Unit=&numUnits;
output data=Unrandomized out=Randomized;
run;

proc sort data=Randomized;
by Unit;
run;

proc freq data=Randomized;
tables Treatment;
run;

``````
Posts: 4,919

## Re: Generate random order within a set of number.

You can also use proc surveyselect with the groups= option:

``````%let numUnits = 180;    /* number of participants */
%let numTreat = 6;      /* number of treatments */

data units;
do Unit = 1 to &numUnits;
output;
end;
run;

proc surveyselect data=units groups=&numTreat seed=86787
out=assignedUnits(rename=groupId=treatment);
run;

proc freq data=assignedUnits ;
tables treatment;
run;
``````
PG
New Contributor
Posts: 2

## Re: Generate random order within a set of number.

A non-STAT solution:

DATA shuffle ;

SET subjectlist ;

rand_position = RAND('UNIFORM') ;

RUN ;

PROC SORT DATA=shuffle ;

BY rand_position ;

RUN ;

DATA grouped_subjects ;

SET shuffle ;

group_id = CEIL(_N_ / 30) ;

RUN ;

Happy provider of untested code
Super User
Posts: 10,018

## Re: Generate random order within a set of number.

Here are two distribution you can use.

``````data _null_;
call streaminit(1234);
do i=1 to 10;
rank=ceil(6*rand('uniform'));
put i=1 rank=;
end;
run;

/*****************/
data _null_;
call streaminit(1234);
do i=1 to 10;
rank=rand('table',1/6,1/6,1/6,1/6,1/6,1/6);
put i=1 rank=;
end;
run;
``````
Discussion stats
• 5 replies
• 636 views
• 4 likes
• 6 in conversation