BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

Using SAS 9.4

 

I am running the following code to randomize 273 units into 3 groups with 91 units per groups. However, is it possible to set up the distribution in such a way that every 50 units is evenly distributed among the 3 groups? 

meaning the first 50 units would have group 1-17 units, group 2-17 units, group 3-16 units. Thank you for any help

 

data Unrandomized;
do unit=1 to 273;
if (unit <= 91) then Group=1;
else if (91 < unit <= 182) then Group=2;
else Group=3;
output;
end;
run;

/* Randomize the design */
proc plan seed=22042;
factors unit=273;
output data=Unrandomized out=Randomized;
run;

proc sort data=Randomized;
by unit;
proc print data=Randomized;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Watts
SAS Employee

One way to randomly assign units to groups is the GROUPS= option in PROC SURVEYSELECT.

For example, this code creates 3 equal (or near-equal) size groups for each batch of 50 observations. 

data UnRandomized;
do unit=1 to 273;
   if (unit <= 50) then Batch=1;
   else if (unit <= 100) then Batch=2;
   ....
   output;
end; 
run; 

proc surveyselect data=UnRandomized groups=3 seed=123
   out=randomGroups;
strata Batch;
run;

proc freq data=randomGroups;
tables Batch*groupID;
run;

 

View solution in original post

3 REPLIES 3
Watts
SAS Employee

One way to randomly assign units to groups is the GROUPS= option in PROC SURVEYSELECT.

For example, this code creates 3 equal (or near-equal) size groups for each batch of 50 observations. 

data UnRandomized;
do unit=1 to 273;
   if (unit <= 50) then Batch=1;
   else if (unit <= 100) then Batch=2;
   ....
   output;
end; 
run; 

proc surveyselect data=UnRandomized groups=3 seed=123
   out=randomGroups;
strata Batch;
run;

proc freq data=randomGroups;
tables Batch*groupID;
run;

 

GS2
Obsidian | Level 7 GS2
Obsidian | Level 7
So that worked for my original question, which is why I accepted the solution. However, in using this method the ending total between the groups is not even. Meaning group 1 has 87 and groups 2 and 3 have 93. Is there a way to run this to ensure that each group has 91 in the final summation? So, basically the group with the small number ( ie 16 vs 17 vs 17) needs to rotate between the 3 groups instead of always being group 1. Is this possible? Thank you

data UnRandomized;
do unit=1 to 273;
if (unit <= 50) then Batch=1;
else if (unit <= 100) then Batch=2;
else if (unit <= 150) then Batch=3;
else if (unit <= 200) then Batch=4;
else if (unit <= 250) then Batch=5;
else if (unit <= 273) then Batch=6;
output;
end;
run;

proc sort data = UnRandomized;
by batch;
run;
proc surveyselect data=UnRandomized groups=3 seed=22042
out=randomized;
strata Batch;
run;

proc freq data=randomized;
tables Batch*groupID;
run;

Watts
SAS Employee

This blog post by @Rick_SAS shows how to use the RandContingency function to control both the row and column sums (the Batch and Group sums in your example):

Simulate contingency tables with fixed row and column sums in SAS