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!
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;
@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.
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???
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;
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?
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;
Hi Ksharp,
get an error message: invocation of unresolved module EXPANDGRID
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;
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;
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.