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

Hello! I am trying to assign participants to 3 groups using a discrete uniform "table" distribution. I am wondering if there is a way to add to this code to that I get exactly 200, 200, and 100 participants in each group: 

 

data bios675.uniform (keep=treat);
call streaminit(2822);*Set seed;
p1=200/500; p2=200/500; p3=100/500;*Set probabilities of three treatment groups;
do i=1 to 500; *500 participants;
treat=rand("Table", p1, p2, p3);
output;
end;
run;

 

I'm wondering if there is somethign I can do like if a group reaches this number, than stop? Or something like that... Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

Keep track of the declining number of needed cases (N1, N2, N3) in each TREAT level, and the declining number of total available subjects:

 

data want (keep=treat);
call streaminit(2822);*Set seed; array n {3} (200,200,100); do available=sum(of n{*}) to 1 by -1; treat=rand("Table",n1/available,n2/available,n3/available); output; n{treat}=n{treat}-1; end; run;

 

Note: It can be shown that even though probabilities change over the course of the assignment process, every observation has an equal probability of being assigned to a given group.

 

Editted note: initially forgot to put in the streaminit.  It's there now.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
Ksharp
Super User
proc plan seed=17431;
factors group=10 subjid=500 / noprint;
output out=temp;
run;
data want;
 set temp;
 if subjid in (1:100) then treat=1;
 if subjid in (101:300) then treat=2;
 if subjid in (301:500) then treat=3;
run;

proc freq data=want;
table group*treat/list;
run;
mkeintz
PROC Star

Keep track of the declining number of needed cases (N1, N2, N3) in each TREAT level, and the declining number of total available subjects:

 

data want (keep=treat);
call streaminit(2822);*Set seed; array n {3} (200,200,100); do available=sum(of n{*}) to 1 by -1; treat=rand("Table",n1/available,n2/available,n3/available); output; n{treat}=n{treat}-1; end; run;

 

Note: It can be shown that even though probabilities change over the course of the assignment process, every observation has an equal probability of being assigned to a given group.

 

Editted note: initially forgot to put in the streaminit.  It's there now.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
keherder
Obsidian | Level 7
Awesome, this is exactly what I needed, and makes sense! Thank you!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1271 views
  • 3 likes
  • 3 in conversation