Help using Base SAS procedures

Cluster into two even groups

Reply
Occasional Contributor P_2
Occasional Contributor
Posts: 10

Cluster into two even groups

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

 

Super User
Posts: 19,815

Re: Cluster into two even groups

PROC SURVEYSELECT is a better method.
Respected Advisor
Posts: 4,925

Re: Cluster into two even groups

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
Super User
Posts: 11,343

Re: Cluster into two even groups

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.

 

Occasional Contributor P_2
Occasional Contributor
Posts: 10

Re: Cluster into two even groups

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!

Community Manager
Posts: 2,954

Re: Cluster into two even groups

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

Occasional Contributor P_2
Occasional Contributor
Posts: 10

Re: Cluster into two even groups

Posted in reply to ChrisHemedinger

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?

SAS Super FREQ
Posts: 3,753

Re: Cluster into two even groups

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;
Ask a Question
Discussion stats
  • 7 replies
  • 415 views
  • 2 likes
  • 6 in conversation