turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- Analytics
- /
- Stat Procs
- /
- Determine all unique permutations of all possible ...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-10-2017 11:54 AM - edited 02-10-2017 11:55 AM

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!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-10-2017 12:25 PM

use uniqueby or iterate through lexperm instead of allperm

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-10-2017 01:27 PM

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;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-10-2017 01:33 PM - edited 02-10-2017 01:46 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-10-2017 03:41 PM

Guess I should have looked at the documentation.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-10-2017 12:59 PM

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,]) ;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-10-2017 01:00 PM - edited 02-10-2017 01:03 PM

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;