BookmarkSubscribeRSS Feed
Soula
Calcite | Level 5

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! 

 

 

6 REPLIES 6
FriedEgg
SAS Employee
use uniqueby or iterate through lexperm instead of allperm
data_null__
Jade | Level 19

@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;
FriedEgg
SAS Employee

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

data_null__
Jade | Level 19

Guess I should have looked at the documentation.

Rick_SAS
SAS Super FREQ

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,]) ;
FriedEgg
SAS Employee

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1815 views
  • 1 like
  • 4 in conversation