How to Use PROC CLUSTER in SAS (With Example)


Clustering is a technique in machine learning that attempts to find clusters of observations within a dataset.

The goal is to find clusters such that the observations within each cluster are quite similar to each other, while observations in different clusters are quite different from each other.

The easiest way to perform clustering in SAS is to use PROC CLUSTER.

The following example shows how to use PROC CLUSTER in practice.

Example: How to Use PROC CLUSTER in SAS

Suppose we have the following dataset that contains information about points, assists and rebounds for 20 different basketball players:

/*create dataset*/             
data my_data;
    input points assists rebounds;   
    datalines;
18 3 15
20 3 14
19 4 14
14 5 10
14 4 8
15 7 14
20 8 13
28 7 9
30 6 5
31 9 4
35 12 11
33 14 6
29 9 5
25 9 5
25 4 3
27 3 8
29 4 12
30 12 7
19 5 6
23 11 5
;
run;

/*view dataset*/ 
proc print data=my_data;

Suppose we would like to perform clustering to attempt to identify “clusters” of players that have similar stats to each other.

The following code shows how to use PROC CLUSTER in SAS to perform clustering:

/*perform clustering using points, assists and rebounds variables*/             
proc cluster data=my_data method=average;
    var points assists rebounds;
run;

The first tables in the output provide information about how the clustering was performed:

A dendrogram is also produced so that we can visually inspect the similarity between observations in the dataset:

The y-axis shows the individual observations and the x-axis shows the average distance between clusters.

From looking at this dendrogram, it appears that the observations naturally group themselves into three clusters:

SAS PROC CLUSTER example

We can then use the PROC TREE statement with ncl=3 to tell SAS to assign each observation in the original dataset to one of three clusters:

/*assign each observation to one of three clusters*/
proc tree data=clustd noprint ncl=3 out=clusts;
    copy points assists rebounds;
    id player_ID;
run;
proc sort;
   by cluster;
run;

/*view cluster assignments*/
proc print data=clusts;
    id player_ID;
run;

The resulting dataset shows each of the original observations along with the cluster they belong to:

For example, we can see: that players with ID’s 2, 3, 1, 4, 5, 7, 6 and 19 all belong to cluster 1.

This tells us that these eight players are “similar” across the points, assists and rebounds variables.

Note: For this example we chose to use average as the linkage method for clustering. Refer to the SAS documentation for a complete list of other linkage methods you can use.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

How to Perform Principal Components Analysis in SAS
How to Perform Multiple Linear Regression in SAS
How to Perform Logistic Regression in SAS

2 Replies to “How to Use PROC CLUSTER in SAS (With Example)”

    1. Clustering varieties in SAS can be achieved using procedures like `PROC CLUSTER`, `PROC FASTCLUS`, or `PROC HPCLUS` depending on your dataset’s size and computational needs. Here’s an overview of the steps and techniques to cluster varieties:

      ### 1. **Prepare Your Data**
      Ensure your data is cleaned and scaled. Standardizing variables is often necessary for clustering to avoid dominance by variables with large ranges.

      “`sas
      proc standard data=mydata out=standardized mean=0 std=1;
      var var1 var2 var3;
      run;
      “`

      ### 2. **Hierarchical Clustering with `PROC CLUSTER`**
      `PROC CLUSTER` performs hierarchical clustering, suitable for smaller datasets. You can use methods like Ward’s, average linkage, or complete linkage.

      “`sas
      proc cluster data=standardized method=ward outtree=tree;
      var var1 var2 var3;
      id variety_name;
      run;

      proc tree data=tree ncl=3 out=clusters; /* Choose 3 clusters */
      id variety_name;
      run;

      proc print data=clusters;
      run;
      “`

      ### 3. **K-Means Clustering with `PROC FASTCLUS`**
      `PROC FASTCLUS` is ideal for larger datasets and performs non-hierarchical clustering.

      “`sas
      proc fastclus data=standardized out=clusters maxclusters=3;
      var var1 var2 var3;
      id variety_name;
      run;

      proc print data=clusters;
      run;
      “`

      ### 4. **High-Performance Clustering with `PROC HPCLUS`**
      `PROC HPCLUS` is used for large datasets and runs in a distributed environment.

      “`sas
      proc hpclus data=standardized maxclusters=3 outstat=clus_stats;
      input var1 var2 var3;
      id variety_name;
      run;

      proc print data=clus_stats;
      run;
      “`

      ### 5. **Visualize the Clusters**
      You can visualize clusters using scatter plots with `PROC SGPLOT`.

      “`sas
      proc sgplot data=clusters;
      scatter x=var1 y=var2 / group=cluster;
      run;
      “`

      ### Tips:
      – **Choosing Number of Clusters**: Use methods like the Elbow Method or Silhouette Score to determine the optimal number of clusters.
      – **Standardizing Variables**: This step is crucial if the variables have different units or ranges.
      – **Validating Clusters**: Evaluate the quality of clustering using within-cluster sum of squares, silhouette plots, or external validation methods.

      If you share more details about your dataset (number of varieties, variables, etc.), I can tailor the instructions further.

Leave a Reply

Your email address will not be published. Required fields are marked *