KCLUS Procedure

Example 16.4 Clustering Mixed Variables

(View the complete code for this example.)

In this example, PROC KCLUS uses the k-prototypes clustering algorithm to cluster mixed input data that contain both interval and nominal variables in the Baseball data set, which is the same data set that is used in Example 16.3. You can execute the following SAS code to load the input data table, mylib.baseball.

data mylib.baseball;
  Set sashelp.baseball;
  Keep CrAtBat CrHits CrRuns CrRbi CrBB Team League Division Position Div;
Run;

These statements assume that your libref is named mylib, but you can substitute any appropriately defined libref.

The following statements run the k-prototypes clustering algorithm on mixed input data. The first INPUT statement uses the LEVEL=INTERVAL option to specify the input variables CrAtBat, CrHits, CrRuns, CrRbi, and CrBB as the interval input variables; the second INPUT statement uses the LEVEL=NOMINAL option to specify the input variables Team, League, Division, Position, and Div as the nominal input variables.

The KPROTOTYPEPARAMS=USERGAMMA(VALUE=10) option specifies for the k-prototypes clustering algorithm. The NOC=ABC option uses the ABC algorithm to search for the best number of clusters in a range of 2 to 10. The PRINTALLDISTANCES option prints two additional variables, _DISTANCEINT_ and _DISTANCENOM_, in the output table kclusout4.


proc kclus data=mylib.baseball maxiter=10 maxc=10 distancenom=relativefreq
   outstat(outiter)=mylib.kclusOutstat4 printalldistances
   noc=abc(B=10 minclusters=2 align=none criterion=all)
   kprototypeparams=usergamma(value=10);
   input CrAtBat CrHits CrRuns CrRbi CrBB / level=interval;
   input Team League Division Position Div / level=nominal;
   score out=mylib.kclusOut4 copyvars=
   (CrAtBat CrHits CrRuns CrRbi CrBB Team League Division Position Div);
   ods output FreqNom=FreqNom4;
run;

Output 16.4.1 shows the "Model Information" table. The clustering algorithm in use is the k-prototypes, and the value is 10. Since PROC KCLUS is implementing the k-prototypes algorithm on mixed input data, the distance measures for both the interval and nominal variables are displayed in the table, as Euclidean and RelativeFreq, respectively.

Output 16.4.1: Model Information for k-Prototypes Clustering Algorithm

The KCLUS Procedure

Model Information
Clustering AlgorithmK-prototypes
Gamma Value10
Maximum Iterations10
Stop CriterionCluster Change
Stop Criterion Value0
Clusters2
InitializationForgy
Seed1065607171
Distance for Interval VariablesEuclidean
Distance for Nominal VariablesRelativeFreq
Number of Clusters EstimationABC
StandardizationNone
Interval ImputationNone
Nominal ImputationNone


Output 16.4.2 shows the cluster summary table that is produced for the k-prototypes algorithm.

Output 16.4.2: Cluster Summary Table for k-Prototypes Clustering Algorithm

Cluster Summary for Mixed Variables
ClusterFrequencyDistance from Cluster Centroid
to Observation
Within
Cluster
Distance
Nearest
Cluster
Distance
to
Nearest
Cluster
Centroid
MinimumMaximumAverage
19416703.37767393233779583.1753E8221203061
22284081.1514875410810232.4647E8121203059


Last updated: May 14, 2026