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

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

 

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

3 REPLIES 3
ballardw
Super User

 

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.

 

BenConner
Pyrite | Level 9

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

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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