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
- /
- SAS Procedures
- /
- Cluster into two even groups

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

11-06-2015 04:11 PM

Hi everyone,

I need to cluster some points into two groups OF THE SAME SIZE, or +- 1 of course if the input data set has an odd number of observations. I have no idea how to do this.

Let's take this example:

data test;

input name $ height weight sex $ Hair $;

cards;

Bob 76 175 M Brown

Joe 45 160 M Brown

Jim 70 200 M Black

Paul 72 160 M Brown

Mel 56 130 F Blond

Jill 60 125 F Brown

;

Proc fastclus data=test out=test_out maxclusters=2 noprint;

var height weight;

run;

proc print data = test_out;

run;

SAS Output

Obs | name | height | weight | sex | Hair | CLUSTER | DISTANCE |
---|---|---|---|---|---|---|---|

1 | Bob | 76 | 175 | M | Brown | 1 | 12.8550 |

2 | Joe | 45 | 160 | M | Brown | 2 | 20.9672 |

3 | Jim | 70 | 200 | M | Black | 1 | 12.8550 |

4 | Paul | 72 | 160 | M | Brown | 2 | 21.2867 |

5 | Mel | 56 | 130 | F | Blond | 2 | 13.9329 |

6 | Jill | 60 | 125 | F | Brown | 2 | 18.8315 |

So I want to force 3 observations into cluster 1, and 3 into cluster 2. I am really not picky about the clustering technique/method or even the PROC. It just needs to split data points evenly.

Thanks a bunch!

Paul

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

11-06-2015 04:29 PM

PROC SURVEYSELECT is a better method.

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

11-06-2015 04:56 PM

Do you want random assignment into two groups, in which case SURVEYSELECT would be your best bet as @Reeza pointed out, or do you want to form two homogeneous groups, in which case some clustering procedure would be better?

PG

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

11-06-2015 05:30 PM

This will add a group variable assigning alternating records to one or the other group with values of 1 or 0:

```
data paired;
set test;
group = mod(_n_, 2);
run;
```

If the are no criteria for grouping or differentiation. This is extensible to other size groups as well.

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

11-09-2015 08:35 AM

Thank you everyone for the responses. I should have been a bit more clear I what i am trying to do. I am not simply trying to split the data into two groups, I am trying to create two groups, same number of observations, which minimizes the optimal path for the sum of the two groups. So instead of height and weight, imagine latitude and longitude. Later on in my program I am going to construct the optimum route calculator. I want to split the points in such a way that when I perform a 'travelling salemen' analysis on the two groups separately, it minimizes the total distance. Clustering seems like a natural way to split the groups, I just can't figure out how to force the new groups to be the same size.

Thanks!

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

11-09-2015 08:54 AM

You might take a look at the HPSPLIT procedure (part of SAS/STAT 14.1, which is in SAS 9.4m3). It has several options, such as INTERVALBINS and MAXBRANCH, that allow you to apply limits to how the algorithm splits the data.

It also creates a pretty cool decision-tree style of output -- in addition to the regular tabular output. You might even be able to adapt an example to your particular problem.

Even though the procedure is named "HPSPLIT", you don't need access to a high-performance environment to use it.

Chris

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

11-09-2015 10:17 AM

Thanks for the suggestion. Unfortunately, I only have SAS 9.3 at work. Do you know of any comparable to these techniques that would run in 9.3?

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

11-11-2015 11:22 AM

Given your explanation of what you are trying to solve (the TSP in two regions), you might want to rethink your requirement that you split the locations into two clusters with equal number of points. The following example shows two clusters, one with 4 points, the other with 6. It seems to me that if you are trying to minimize the total distance, you'd do much better with two unequally spaced clusters and one route that connects to two clusters.If you try to put one of the points in the lower group into the top cluster, I think you'll increase the total distance.

```
data A;
input x y @@;
datalines;
-0.1 2 0 2 0.1 2 0 2.1
-0.1 -3 0 -3 0.1 -3 -0.1 -3.1 0 -3.1 0.1 -3.1
;
proc sgplot data=A;
scatter x=x y=y;
xaxis grid min=-2 max=2;
yaxis grid;
run;
```