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

Topic Options

- 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
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

09-03-2017 09:00 PM

Hi,

I have the following code:

```
proc iml;
do k = 1 to 3;
combk = allcomb(5, k);
print combk;
end;
quit;
```

in order to get all the possible 1,2 and 3 number combinations of the values 1 to 5. I would like to have the following:

1) Have the names of the generated tables/matrices be comb1, comb2 and comb3 (right now they are all combk).

2) Merge the 3 tables/matrices into one single table/matrix

Thanks!

Accepted Solutions

Solution

09-06-2017
08:05 AM

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

Posted in reply to ilikesas

09-05-2017 03:49 AM

You must have an old version of IML that does not have EXPANDGRID, in any case it will give permutations rather than combinations.

If you prefer an IML solution, then your original idea of a loop over the allcomb function is sound, the tricky part of the problem is working out how many combinations of each size there are, and where to place them in the final matrix c. Here is my suggestion for achieving this:

```
proc iml;
n = 5;
maxk = 3;
nc = comb( n, 1:maxk ); /* ncomb for each size k */
c = j(sum(nc), maxk, .); /* matrix to hold everything */
r2 = cusum( nc ); /* last row in c of size k */
r1 = r2 - nc + 1; /* first row in c of size k */
do k = 1 to maxk; /* fill c with combinations */
c [ r1[k]:r2[k], 1:k] = allcomb(n, k);
end;
print c;
quit;
```

All Replies

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

Posted in reply to ilikesas

09-03-2017 09:15 PM

ilikesas wrote:

Hi,

I have the following code:

`proc iml; do k = 1 to 3; combk = allcomb(5, k); print combk; end; quit;`

in order to get all the possible 1,2 and 3 number combinations of the values 1 to 5. I would like to have the following:

1) Have the names of the generated tables/matrices be comb1, comb2 and comb3 (right now they are all combk).

2) Merge the 3 tables/matrices into one single table/matrix

Thanks!

Do it in a data step instead of PROC IML. Eliminate the requirement #1, just save the value of the loop variable, and you've got your final data table.

--

Paige Miller

Paige Miller

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

Posted in reply to PaigeMiller

09-03-2017 09:41 PM

Hi PaigeMiller,

I tried to do the following code using the Data Step:

```
data want;
array x[5] $3 ('ant' 'bee' 'cat' 'dog' 'ewe');
n=dim(x);
do k= 1 to 3;
ncomb=comb(n,k);
do j = 1 to ncomb;
call allcomb(k, 5, of x[*]);
output;
end;
end;
run;
```

This calculated the number of combinations for each k given the array of 5 items, but didn't generate the actual combination???

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

Posted in reply to ilikesas

09-04-2017 03:51 AM

The call to allcomb is incorrect as it is always requesting a subset of size 5, also the 1st parameter should be j rather than k. The following code should work - I am generating numbers rather than animals and I have also transferred the combinations to the array y, in order to get missing values values where the subset is less than the maximum size.

```
data want;
array x[5] _temporary_ (1:5) ;
array y[3] ;
n = dim(x);
do k = 1 to 3;
ncomb = comb(n,k);
do j = 1 to ncomb;
call allcomb(j, k, of x[*]);
do i = 1 to k;
y[i] = x[i];
end;
output;
end;
end;
run;
```

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

Posted in reply to ilikesas

09-04-2017 07:55 AM

I would imagine that if you're going to follow the example in the documentation, you shouldn't be adding in steps and code that isn't in the example. You shouldn't be adding in

` do k= 1 to 3;`

https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a003112305.htm

But, you need to explain further:

but didn't generate the actual combination

What does it do? What is the error in the log?

--

Paige Miller

Paige Miller

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

Posted in reply to ilikesas

09-04-2017 11:42 AM

Expandgrid() is what you are looking for ?

```
proc iml;
x=1:5;
want=expandgrid(x,0,0)//expandgrid(x,x,0)//expandgrid(x,x,x);
print want;
quit;
```

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

Posted in reply to Ksharp

09-04-2017 02:39 PM

Hi Ksharp,

get an error message: invocation of unresolved module EXPANDGRID

Solution

09-06-2017
08:05 AM

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

Posted in reply to ilikesas

09-05-2017 03:49 AM

You must have an old version of IML that does not have EXPANDGRID, in any case it will give permutations rather than combinations.

If you prefer an IML solution, then your original idea of a loop over the allcomb function is sound, the tricky part of the problem is working out how many combinations of each size there are, and where to place them in the final matrix c. Here is my suggestion for achieving this:

```
proc iml;
n = 5;
maxk = 3;
nc = comb( n, 1:maxk ); /* ncomb for each size k */
c = j(sum(nc), maxk, .); /* matrix to hold everything */
r2 = cusum( nc ); /* last row in c of size k */
r1 = r2 - nc + 1; /* first row in c of size k */
do k = 1 to maxk; /* fill c with combinations */
c [ r1[k]:r2[k], 1:k] = allcomb(n, k);
end;
print c;
quit;
```

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

Posted in reply to ilikesas

09-05-2017 10:26 AM

OK. How about this one ? proc iml; do k = 1 to 3; x='comb'+strip(char(k)); call valset(x,allcomb(5, k)); end; print comb1 ,comb2 ,comb3; want=(comb1||repeat(0,nrow(comb1),2)) // (comb2||repeat(0,nrow(comb2),1)) // comb3; print want; quit;

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

Posted in reply to Ksharp

09-05-2017 10:11 PM

Hi KSharp, still not working.

Get the first error message right after the x='comb'+strip(char(k)); saying that the matrix has not been set to a value