Programming the statistical procedures from SAS

Determine all unique permutations of all possible treatments

Reply
New Contributor
Posts: 3

Determine all unique permutations of all possible treatments

[ Edited ]

I am using SAS 9.3. 

 

I am interested in generating a dataset containing all possible permutations of 3 drugs in a block size of 6, with equal numbers of each trial (1:1:1).  That is, if the treatments are A, B, C and I have a block size of 6 with 2 As, 2 Bs and 2 Cs, then some possible blocks include the following: 

 

AABBCC

AACCBB

ABACCB

AABCCB

etc.

 

The important thing is that I must have each treatment repeating twice and only twice per block. Because each treatment must be present twice in each block--this is usually where I have been running into problems with other code that i have tried.  I have unsucessfuly tried to generate this in IML and PROC PLAN (using the TREATMENTS statement).  

 

I have tried to implement a process in IML, which is great, but it provides duplicates when I only want to see the unique permutations.  Here is that code: 

 

 

proc iml;
call randseed(1234);
tName = "Treat1":"Treat6";
perms = allperm({A A B B C C}); 

 

Any help would be appreciated! 

 

 

Trusted Advisor
Posts: 1,297

Re: Determine all unique permutations of all possible treatments

use uniqueby or iterate through lexperm instead of allperm
Respected Advisor
Posts: 3,773

Re: Determine all unique permutations of all possible treatments


FriedEgg wrote:
use uniqueby or iterate through lexperm instead of allperm

Is this what you had in mind and does it work?

 

data lexperm;
   array _l[6] $ ('A' 'A' 'B' 'B' 'C' 'C');
   do i = 1 by 1;
      if i gt 1 then do;
         rc=lexperm(6,of _l[*]);
         if rc lt 0 then stop;
         end;
      output;
      *put i 5. rc 2. @20 _l[*];
      end;
   run;
proc sort;
   by _L:;
   run;
proc print;
   run;
Trusted Advisor
Posts: 1,297

Re: Determine all unique permutations of all possible treatments

[ Edited ]

Yes, and yes

 

Can be simplified to the following:

 

data lexperm;
array i[6] $ 1 (2*('A' 'B' 'C'));
do _n_=1 to fact(dim(i));
  rc=lexperm(_n_, of i[*]);
  if rc<0 then leave;
  else output;
end;
stop;
run;

 

lexperm also already outputs in order, so the proc sort shouldn't be necessary

Respected Advisor
Posts: 3,773

Re: Determine all unique permutations of all possible treatments

Guess I should have looked at the documentation.

SAS Super FREQ
Posts: 3,319

Re: Determine all unique permutations of all possible treatments

I think you an use PROC PLAN for this, but since you asked about PROC IML....

 

You can use indices 1:6 to generate the permutations and then attach whatever labels you want to tose permutations.  The resulting sets might not be unique, but maybe that is your intention?

 

levels = {A A B B C C};
perms = allperm(1:6); 
plan = shape(levels[perms], 0, ncol(level));

print (perms[1:5,])  (plan[1:5,]) ;
Trusted Advisor
Posts: 1,297

Re: Determine all unique permutations of all possible treatments

[ Edited ]

There is likely to be a better way to do this with PROC PLAN too, just as @Rick_SAS showed with IML

 

ods output plan=plan(drop=rep);
proc plan seed=1234;
factors rep=%sysfunc(fact(6)) col=6;
run;

data plan;
set plan;
array col [6];
array i   [6] _temporary_ (2*(1:3));

do _n_=1 to 6;
col[_n_]=i[col[_n_]];
end;
run;

proc sort data=plan nodupkey;
by col:;
run;

proc iml;
perm = allperm({1 1 2 2 3 3});
cols = 1:6;
call sortndx(ndx, perm, cols);
uidx = uniqueby(perm, cols, ndx);
uval = perm[ndx[uidx], cols];
create planIml from uval;
append from uval;
close planIml;
quit;
Ask a Question
Discussion stats
  • 6 replies
  • 128 views
  • 1 like
  • 4 in conversation