Turn on suggestions

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

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- Re: Cluster into two even groups

Options

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 11-06-2015 04:11 PM
(1355 views)

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

- Tags:
- proc fastclus

7 REPLIES 7

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

PROC SURVEYSELECT is a better method.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

Check out SAS Innovate on-demand content! Watch the main stage sessions, keynotes, and over 20 technical breakout sessions!

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

**Available on demand!**

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

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.