TSNE Procedure

Getting Started: TSNE Procedure

Note: Input data must be in a CAS table that is accessible in your CAS session. You must refer to this table by using a two-level name. The first level must be a CAS engine libref, and the second level must be the table name. For more information, see the sections Using CAS Sessions and CAS Engine Librefs and Loading a SAS Data Set onto a CAS Server in Chapter 2, Shared Concepts.

This example shows how to use the TSNE procedure to obtain an embedding from observations in a data table. The example uses the Iris data from Fisher (1936), which contain morphological measurements of 50 specimens from each of three different species of iris flowers: Iris setosa, I. versicolor, and I. virginica. Mezzich and Solomon (1980) discuss a variety of cluster analyses that use the Iris data. PROC TSNE returns a two-dimensional representation of each observation. The analysis uses four variables: SepalLength, SepalWidth, PetalLength, and PetalWidth. The remaining variables in the data table are not used.

You can load the Sashelp.Iris data set into your CAS session by specifying your CAS engine libref in the first statement in the following DATA step. These statements assume that your CAS engine libref is named mylib, but you can substitute any appropriately defined CAS engine libref.

data mylib.iris;
    set sashelp.iris;
    id=_n_;
run;

The following statements run PROC TSNE and output the results to ODS tables:

proc tsne
   data            = mylib.iris
   nDimensions     = 2
   perplexity      = 5
   learningRate    = 100
   maxIters        = 500;
   input             SepalLength SepalWidth PetalLength PetalWidth;
   output out      = mylib.tsne_out copyvars=(id species);
run;

The NDIMENSIONS=2 option requests that the model return two embedding dimensions; the PERPLEXITY=5 option specifies the perplexity value; the LEARNINGRATE=100 option specifies the learning rate for the optimization; and the INPUT statement specifies that the SepalLength, SepalWidth, PetalLength, and PetalWidth variables be used as inputs. The OUTPUT statement requests that the embedding be written to the data table mylib.tsne_out, and the COPYVARS= option requests that the ID and Species variables be copied to the output.

The following statements download the data table mylib.tsne_out from CAS to the local SAS data set tsne_out:

data tsne_out;
    set mylib.tsne_out;
run;

The following PROC SGPLOT statements plot embedding dimension _DIM_2_ against embedding dimension _DIM_1_:

 proc sgplot data=tsne_out;
    title "Iris embedding";
    title1 "Scatter plot of iris embedding";
    scatter x=_DIM_1_ y=_DIM_2_ / group=species
                                  markerattrs=(symbol=CircleFilled);
 run;

Figure 1 shows the results. The colors in the scatter plot indicate three distinct clusters, which correspond to the three iris species.

Figure 1: Scatter Plot of Iris Data Embedding

Scatter Plot of Iris Data Embedding


Last updated: August 06, 2026