turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Generating combinations from a list of values

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-11-2017 10:36 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to BenConner

07-11-2017 11:39 AM

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.

All Replies

Solution

07-11-2017
12:10 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to BenConner

07-11-2017 11:39 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

07-11-2017 12:09 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to BenConner

07-12-2017 10:02 AM

```
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;
```