Help using Base SAS procedures

Generate Unique Combinations of a Series

Accepted Solution Solved
Reply
Contributor
Posts: 62
Accepted Solution

Generate Unique Combinations of a Series

I want to generate all possible unique combinations for a series. For Example,

data have;

  input combinations $1.;

  cards;

A

B

C

D

E

;

run;

I want a column that has ( i think this works out to 32 distinct combinations, 2^5, no?)

A

AB

ABC

ABCDE

B

BC

......

Thank you,


Accepted Solutions
Solution
‎08-02-2012 07:54 PM
Respected Advisor
Posts: 4,649

Re: Generate Unique Combinations of a Series

So, what you want is all possible subsets :

data test(keep=k sub);

array II{5} (5*0);

length sub $5;

k = -1;

do i = 1 to 2**5;

     call graycode (k, of II{*});

     call missing(sub);

     do j = 1 to 5;

          if II{j} then sub = cats(sub,char("ABCDE",j));

          end;

     output;

     end;

run;

PG

PG

View solution in original post


All Replies
PROC Star
Posts: 7,363

Re: Generate Unique Combinations of a Series

I think 2^5 -1

data _null_;

  array a[5] $ ('A' 'B' 'C' 'D' 'E');

  n = dim(a);

  do k=1 to n;

    do j=1 to comb(n,k);

      counter+1;

      call allcomb(j,k,of a

  • );
  •       put counter 5. +3 @;

          do i = 1 to k;

            put a $5. @;

          end;

          put;

        end;

      end;

    run;

    Contributor
    Posts: 62

    Re: Generate Unique Combinations of a Series

    Thanks Art!

    Can you show me how to modify this code such that I can have a column with all the combinations?

    PROC Star
    Posts: 7,363

    Re: Generate Unique Combinations of a Series

    Jeffrey,

    Not sure what you are asking regarding one column.  Possibly something like:

    data want(keep=want);

      array a[5] $ ('A' 'B' 'C' 'D' 'E');

      length temp $5 want $500;

      format want $500.;

      n = dim(a);

      want='';

      do k=1 to n;

        do j=1 to comb(n,k);

          counter+1;

          call allcomb(j,k,of a

  • );
  •       temp='';

          do i = 1 to k;

             temp=catt(temp, a);

          end;

          want=catx(',',want,temp);

        end;

      end;

    run;

    Respected Advisor
    Posts: 3,124

    Re: Generate Unique Combinations of a Series

    Question: for example,  will 'AB' be equivalent to 'BA'? in other words, will the order matter?

    Haikuo

    Contributor
    Posts: 62

    Re: Generate Unique Combinations of a Series

    For this i'm treating AB as equivalent to BA. Order does not matter.

    Solution
    ‎08-02-2012 07:54 PM
    Respected Advisor
    Posts: 4,649

    Re: Generate Unique Combinations of a Series

    So, what you want is all possible subsets :

    data test(keep=k sub);

    array II{5} (5*0);

    length sub $5;

    k = -1;

    do i = 1 to 2**5;

         call graycode (k, of II{*});

         call missing(sub);

         do j = 1 to 5;

              if II{j} then sub = cats(sub,char("ABCDE",j));

              end;

         output;

         end;

    run;

    PG

    PG
    Super User
    Posts: 9,681

    Re: Generate Unique Combinations of a Series

    I found an interesting way. Maybe you like it.

    data have;
      input combinations $1.;
      cards;
    A
    B
    C
    D
    E
    ;
    run;
    proc transpose data=have out=x(keep=col:);
    var combinations;
    run;
    proc means data=x noprint;
    class _all_;
    output out=want(keep=col:);
    run;
    data want;
     set want;
     combination=cats(of col:);
    run;
    
    

    Ksharp

    ☑ This topic is SOLVED.

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

    Discussion stats
    • 7 replies
    • 540 views
    • 8 likes
    • 5 in conversation