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

I need to write code to create a reproducible randomization scheme for a three site clinical trial. At each site, the randomization is 1:1 treatment:control but in blocks of 6. 24 subjects are assigned to each site. In other words, there are a total of 72 subjects, 24 subjects randomly assigned to each site, 4 blocks with a size of 6 at each site, with 3 treatment subjects and 3 control subjects within each block. The code has to be reproducible in a sense that you can change the number of sites, number of subjects per site, number of blocks, size of blocks and the ratio of control to treatment.

 

I am fine with randomly assigning subjects to different sites. Where I struggle is randomly assigning subjects within each site to blocks with a ratio of treatment to control. For some reason, when I run the code to assign subjects to blocks with treatment and control, each subject within each site produces 24 observations (4 blocks of 6). So a total of 576 observations in each site. What I am trying to accomplish is randomly assigning each subject within a site to one of 4 blocks of 6 with 3 treatment and 3 control subjects in each block (or whatever the specified block size or treatment/control ratio is).

 

Here is what I have so far:

 

data values;
nsites=3;
subjectspersite=24;
/* to calculate the total sample size based on the number of sites and number of subjects assigned to each site*/
samplesize=(nsites*subjectspersite);
blocksize=6;
TreatmentR=1;
nblocks=round((subjectspersite)/(blocksize));
na=round(blocksize/(1/(treatmentR/(1+treatmentR))));
nb=blocksize-na;
run;
data values1;
set values;
/*to assign each subject*/
DO Subject = 1 to (samplesize) by 1;
OUTPUT;
END;
RUN;
DATA values2;
set values1;
 
/*to randomly assign a number 1 to 0 to each subject*/
 
random=ranuni(27407349);
RUN;
/*to sort each subject by their randomly assigned number created in values2*/
PROC SORT data=values2;
by random;
RUN;
 
PROC FORMAT;
 
/*to format values to different sites*/
 
value siteno 1 = 'Site 1'
2 = 'Site 2'
3 = 'Site 3'
4 = 'Site 4'
5 = 'Site 5'
6 = 'Site 6'
7 = 'Site 7'
8 = 'Site 8'
9 = 'Site 9'
10 = 'Site 10';
RUN;
data values3;
set values2;
/*if else statements to assign subjects to their sites, based on the number of sites and total sample size*/
/*for 1 site*/
if nsites=1 then site=1;
/*for 2 sites*/
if _N_ le ((1/nsites)*samplesize) and nsites=2 then site=1;
else if _ gt ((1/nsites)*samplesize) and nsites=2 then site=2;
/*for 3 sites*/
if _N_ le ((1/nsites)*samplesize) and nsites=3 then site=1;
else if _N_ gt ((1/nsites)*samplesize) and _N_ le ((2/nsites)*samplesize) then site=2;
else if _N_ gt ((2/nsites)*samplesize) then site=3;
/*for 4 sites*/
if _N_ le ((1/nsites)*samplesize) and nsites=4 then site=1;
else if _N_ gt ((1/nsites)*samplesize) and _N_ le ((2/nsites)*samplesize) then site=2;
else if _N_ gt ((2/nsites)*samplesize) and _N_ le ((3/nsites)*samplesize) then site=3;
else if _N_ gt((3/nsites)*samplesize) then site=4;
/*for 5 sites*/
if _N_ le ((1/nsites)*samplesize) and nsites=5 then site=1;
else if _N_ gt ((1/nsites)*samplesize) and _N_ le ((2/nsites)*samplesize) then site=2;
else if _N_ gt ((2/nsites)*samplesize) and _N_ le ((3/nsites)*samplesize) then site=3;
else if _N_ gt((3/nsites)*samplesize) and _N_ le ((4/nsites)*samplesize) then site=4;
else if _N_ gt((4/nsites)*samplesize) then site=5;
/*for 6 sites*/
if _N_ le ((1/nsites)*samplesize) and nsites=6 then site=1;
else if _N_ gt ((1/nsites)*samplesize) and _N_ le ((2/nsites)*samplesize) then site=2;
else if _N_ gt ((2/nsites)*samplesize) and _N_ le ((3/nsites)*samplesize) then site=3;
else if _N_ gt((3/nsites)*samplesize) and _N_ le ((4/nsites)*samplesize) then site=4;
else if _N_ gt((4/nsites)* samplesize) and _N_ le ((5/nsites)*samplesize) then site=5;
else if _N_ gt((5/nsites)*samplesize) then site=6;
/*for 7 sites*/
if _N_ le ((1/nsites)*samplesize) and nsites=7 then site=1;
else if _N_ gt ((1/nsites)*samplesize) and _N_ le ((2/nsites)*samplesize) then site=2;
else if _N_ gt ((2/nsites)*samplesize) and _N_ le ((3/nsites)*samplesize) then site=3;
else if _N_ gt((3/nsites)*samplesize) and _N_ le ((4/nsites)*samplesize) then site=4;
else if _N_ gt((4/nsites)* samplesize) and _N_ le ((5/nsites)*samplesize) then site=5;
else if _N_ gt((5/nsites)*samplesize) and _N_ le ((6/nsites)*samplesize) then site=6;
else if _N_ gt((6/nsites)*samplesize)then site=8;
/*for 8 sites*/
if _N_ le ((1/nsites)*samplesize) and nsites=8 then site=1;
else if _N_ gt ((1/nsites)*samplesize) and _N_ le ((2/nsites)*samplesize) then site=2;
else if _N_ gt ((2/nsites)*samplesize) and _N_ le ((3/nsites)*samplesize) then site=3;
else if _N_ gt((3/nsites)*samplesize) and _N_ le ((4/nsites)*samplesize) then site=4;
else if _N_ gt((4/nsites)* samplesize) and _N_ le ((5/nsites)*samplesize) then site=5;
else if _N_ gt((5/nsites)*samplesize) and _N_ le ((6/nsites)*samplesize) then site=6;
else if _N_ gt((6/nsites)*samplesize) and _N_ le ((7/nsites)*samplesize) then site=7;
else if _N_ gt((7/nsites)*samplesize) then site=8;
/*for 9 sites*/
if _N_ le ((1/nsites)*samplesize) and nsites=9 then site=1;
else if _N_ gt ((1/nsites)*samplesize) and _N_ le ((2/nsites)*samplesize) then site=2;
else if _N_ gt ((2/nsites)*samplesize) and _N_ le ((3/nsites)*samplesize) then site=3;
else if _N_ gt((3/nsites)*samplesize) and _N_ le ((4/nsites)*samplesize) then site=4;
else if _N_ gt((4/nsites)* samplesize) and _N_ le ((5/nsites)*samplesize) then site=5;
else if _N_ gt((5/nsites)*samplesize) and _N_ le ((6/nsites)*samplesize) then site=6;
else if _N_ gt((6/nsites)*samplesize) and _N_ le ((7/nsites)*samplesize) then site=7;
else if _N_ gt((7/nsites)*samplesize) and _N_ le ((8/nsites)*samplesize) then site=8;
else if _N_ gt((8/nsites)*samplesize) then site=9;
 
/*for 10 sites*/
if _N_ le ((1/nsites)*samplesize) and nsites=10 then site=1;
else if _N_ gt ((1/nsites)*samplesize) and _N_ le ((2/nsites)*samplesize) then site=2;
else if _N_ gt ((2/nsites)*samplesize) and _N_ le ((3/nsites)*samplesize) then site=3;
else if _N_ gt((3/nsites)*samplesize) and _N_ le ((4/nsites)*samplesize) then site=4;
else if _N_ gt((4/nsites)* samplesize) and _N_ le ((5/nsites)*samplesize) then site=5;
else if _N_ gt((5/nsites)*samplesize) and _N_ le ((6/nsites)*samplesize) then site=6;
else if _N_ gt((6/nsites)*samplesize) and _N_ le ((7/nsites)*samplesize) then site=7;
else if _N_ gt((7/nsites)*samplesize) and _N_ le ((8/nsites)*samplesize) then site=8;
else if _N_ gt((8/nsites)*samplesize) and _N_ le ((9/nsites)*samplesize) then site=9;
else if _N_ gt((9/nsites)*samplesize) then site=10;
 
format site siteno.;
run;
 
PROC SORT data=values3;
by site;
RUN;
 
data sites1 sites2 sites3 sites4 sites5 sites6 sites7 sites8 sites9 sites10;
set values3;
if nsites=1 then output sites1;
else if nsites=2 then output sites2;
else if nsites=3 then output sites3;
else if nsites=4 then output sites4;
else if nsites=5 then output sites5;
else if nsites=6 then output sites6;
else if nsites=7 then output sites7;
else if nsites=8 then output sites8;
else if nsites=9 then output sites9;
else if nsites=10 then output sites10;
run;
 
Data site1of1;
set sites1;
if site=1 then output site1of1;
run;
 
data site1of2 site2of2;
set sites2;
if site=1 then output site1of2;
else if site=2 then output site2of2;
run;
 
data site1of3 site2of3 site3of3;
set sites3;
if site=1 then output site1of3;
else if site=2 then output site2of3;
else if site=3 then output site3of3;
run;
 
data site1of4 site2of4 site3of4 site4of4;
set sites4;
if site=1 then output site1of4;
else if site=2 then output site2of4;
else if site=3 then output site3of4;
else if site=4 then output site4of4;
run;
 
data site1of5 site2of5 site3of5 site4of5 site5of5;
set sites5;
if site=1 then output site1of5;
else if site=2 then output site2of5;
else if site=3 then output site3of5;
else if site=4 then output site4of5;
else if site=5 then output site5of5;
run;
 
data site1of6 site2of6 site3of6 site4of6 site5of6 site6of6;
set sites6;
if site=1 then output site1of6;
else if site=2 then output site2of6;
else if site=3 then output site3of6;
else if site=4 then output site4of6;
else if site=5 then output site5of6;
else if site=6 then output site6of6;
run;
 
data site1of7 site2of7 site3of7 site4of7 site5of7 site6of7 site7of7;
set sites7;
if site=1 then output site1of7;
else if site=2 then output site2of7;
else if site=3 then output site3of7;
else if site=4 then output site4of7;
else if site=5 then output site5of7;
else if site=6 then output site6of7;
else if site=7 then output site7of7;
run;
 
data site1of8 site2of8 site3of8 site4of8 site5of8 site6of8 site7of8 site8of8;
set sites8;
if site=1 then output site1of8;
else if site=2 then output site2of8;
else if site=3 then output site3of8;
else if site=4 then output site4of8;
else if site=5 then output site5of8;
else if site=6 then output site6of8;
else if site=7 then output site7of8;
else if site=8 then output site8of8;
run;
 
 
data site1of9 site2of9 site3of9 site4of9 site5of9 site6of9 site7of9 site8of9 site9of9;
set sites9;
if site=1 then output site1of9;
else if site=2 then output site2of9;
else if site=3 then output site3of9;
else if site=4 then output site4of9;
else if site=5 then output site5of9;
else if site=6 then output site6of9;
else if site=7 then output site7of9;
else if site=8 then output site8of9;
else if site=9 then output site9of9;
run;
 
data site1of10 site2of10 site3of10 site4of10 site5of10 site6of10 site7of10 site8of10 site9of10 site10of10;
set sites10;
if site=1 then output site1of10;
else if site=2 then output site2of10;
else if site=3 then output site3of10;
else if site=4 then output site4of10;
else if site=5 then output site5of10;
else if site=6 then output site6of10;
else if site=7 then output site7of10;
else if site=8 then output site8of10;
else if site=9 then output site9of10;
else if site=10 then output site10of10;
run;
 
 
proc print data=site1of3;
run;
 
data treatmentsite1of3;
set site1of3;
do block=1 to nblocks by 1;
nna=0;
nnb=0;
do i=1 to blocksize;
subject2=i+((block-1)*blocksize);
if nna=na then treatment="B";
if nnb=nb then treatment="A";
else do;
aprob=(na-nna)/(na+nb-nna-nnb);
u=ranuni(0);
if (0<=u<=aprob) then do;
treatment="A";
nna=nna+1;
end;
if (aprob<u<=1) then do;
treatment="B";
nnb=nnb+1;
end;
end;
keep subject2 treatment site subject site nblocks block;
output;
end;
end;
run;
 
proc print data=treatmentsite1of3;
id subject2;
var treatment site subject block;
title1 'Site 1 of 3 sites';
title2 "Randomization Schema for Specified Ratio of Treatments A to Control B";
run;
 
Any help would be greatly appreciated. Also, Any correction or suggestions with my current code would be extremely helpful.
 
Thank you.

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Would proc plan do what you want? e.g.:

 

data Unrandomized;
  do site=1 to 3;
   do block=1 to 4;
     do subject=1 to 6;
       id+1;
       if (subject <= 3) then treatment=1;
       else                   treatment=2;
       output;
     end;
   end;
 end;
run;

proc plan seed=27371;
   factors site=3 block=6 treatment=2;
   output data=Unrandomized out=Randomized;
run;

proc sort data=Randomized;
   by id;
run;

Art, CEO, AnalystFinder.com

View solution in original post

2 REPLIES 2
art297
Opal | Level 21

Would proc plan do what you want? e.g.:

 

data Unrandomized;
  do site=1 to 3;
   do block=1 to 4;
     do subject=1 to 6;
       id+1;
       if (subject <= 3) then treatment=1;
       else                   treatment=2;
       output;
     end;
   end;
 end;
run;

proc plan seed=27371;
   factors site=3 block=6 treatment=2;
   output data=Unrandomized out=Randomized;
run;

proc sort data=Randomized;
   by id;
run;

Art, CEO, AnalystFinder.com

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1971 views
  • 3 likes
  • 2 in conversation