BookmarkSubscribeRSS Feed
P_2
Fluorite | Level 6 P_2
Fluorite | Level 6

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

 

7 REPLIES 7
Reeza
Super User
PROC SURVEYSELECT is a better method.
PGStats
Opal | Level 21

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
ballardw
Super User

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.

 

P_2
Fluorite | Level 6 P_2
Fluorite | Level 6

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!

ChrisHemedinger
Community Manager

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

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
P_2
Fluorite | Level 6 P_2
Fluorite | Level 6

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?

Rick_SAS
SAS Super FREQ

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

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
  • 7 replies
  • 1229 views
  • 2 likes
  • 6 in conversation