BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ilikesas
Barite | Level 11

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!

1 ACCEPTED SOLUTION

Accepted Solutions
IanWakeling
Barite | Level 11

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;

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

@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
ilikesas
Barite | Level 11

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???

 

 

 

IanWakeling
Barite | Level 11

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;
PaigeMiller
Diamond | Level 26

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
Ksharp
Super User

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;
ilikesas
Barite | Level 11

Hi Ksharp,

 

get an error message: invocation of unresolved module EXPANDGRID

IanWakeling
Barite | Level 11

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;
Ksharp
Super User
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;


ilikesas
Barite | Level 11

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

Multiple Linear Regression in SAS

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.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 9 replies
  • 1736 views
  • 7 likes
  • 4 in conversation