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

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,

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

7 REPLIES 7
art297
Opal | Level 21

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;

    SAShole
    Pyrite | Level 9

    Thanks Art!

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

    art297
    Opal | Level 21

    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;

    Haikuo
    Onyx | Level 15

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

    Haikuo

    SAShole
    Pyrite | Level 9

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

    PGStats
    Opal | Level 21

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

    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

    hackathon24-white-horiz.png

    The 2025 SAS Hackathon has begun!

    It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

    Latest Updates

    What is Bayesian Analysis?

    Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

    Find more tutorials on the SAS Users YouTube channel.

    SAS Training: Just a Click Away

     Ready to level-up your skills? Choose your own adventure.

    Browse our catalog!

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