DATA Step, Macro, Functions and more

Generating combinations from a list of values

Accepted Solution Solved
Reply
Regular Contributor
Posts: 169
Accepted Solution

Generating combinations from a list of values

Hi,

 

Given a set of values, say A B and C, I need to generate the following from it:

 

A

B

C

A B

A C

B C

A B C

 

I've looked at the permutation functions that those don't seem to fit.  Not sure about Proc Plan either.  Data step would be prefereable but not required.

 

Thanks!

 

--Ben


Accepted Solutions
Solution
‎07-11-2017 12:10 PM
Super User
Posts: 10,500

Re: Generating combinations from a list of values

 

is the output supposed to be a data set or text output? Do you want a single variable with the "value" or something such as a series of variables that is populated with the desired pieces?

 

It may help to indicate how you are getting the values that you need to combine. Do you have list or a data set? And how many actual values are you going to be concerned with?

 

This comes pretty close:

data junk;
   array x[3] $3 ('a' 'b' 'c' );
   array h[3] $3 ;
   n=dim(x);
   do k=1 to 3;
   ncomb=comb(n, k);
      call sortc(of x(*));
      /* empty target array*/
      call missing (of h(*));
      do j=1 to ncomb;
         rc=allcomb(j, k, of x[*]);
         if rc<0 then leave;
         do i=1 to k;
            h[i]=x[i];
         end;
         longstr= cats(of h(*));
         output;
      end;
   end;
   drop i j k n ncomb rc;
run;

if the want is a single variable look at the longstr, if array look at h. You may want to sort the data set if order is critical.

 

View solution in original post


All Replies
Solution
‎07-11-2017 12:10 PM
Super User
Posts: 10,500

Re: Generating combinations from a list of values

 

is the output supposed to be a data set or text output? Do you want a single variable with the "value" or something such as a series of variables that is populated with the desired pieces?

 

It may help to indicate how you are getting the values that you need to combine. Do you have list or a data set? And how many actual values are you going to be concerned with?

 

This comes pretty close:

data junk;
   array x[3] $3 ('a' 'b' 'c' );
   array h[3] $3 ;
   n=dim(x);
   do k=1 to 3;
   ncomb=comb(n, k);
      call sortc(of x(*));
      /* empty target array*/
      call missing (of h(*));
      do j=1 to ncomb;
         rc=allcomb(j, k, of x[*]);
         if rc<0 then leave;
         do i=1 to k;
            h[i]=x[i];
         end;
         longstr= cats(of h(*));
         output;
      end;
   end;
   drop i j k n ncomb rc;
run;

if the want is a single variable look at the longstr, if array look at h. You may want to sort the data set if order is critical.

 

Regular Contributor
Posts: 169

Re: Generating combinations from a list of values

Actually that's perfect!  I was going to chuck the values into an array statement for the real problem and this is what I was looking for.

 

Thank you so much!

 

--Ben

Super User
Posts: 9,681

Re: Generating combinations from a list of values

data _null_;
array c[3] $ ('a' 'b' 'c');
array x[3];
length comb $ 400;
n=dim(x);
k=-1;
nsubs=2**n;
do i=1 to nsubs;
 rc=graycode(k, of x[*]);
 call missing(comb);
 do j=1 to n;
  if x[j]=1 then comb=catx(' ',comb,c[j]);
 end;
 if not missing(comb) then putlog comb= ;
end;
run;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 120 views
  • 0 likes
  • 3 in conversation