Sankey Bar Charts - SAS Getting Started

Getting Started

This example illustrates a basic application of the %SankeyBarChart macro (downloadable on the Macro page).

The simplest application of the %SankeyBarChart macro requires the user to specify four parameters:

  • A dataset name
  • A x-axis variable
  • A y-axis variable
  • A subject ID variable
Using only these four inputs the macro will produce a basic Sankey bar chart.

We begin with some dummy data generation. The resulting dataset will have one record per SUBJECT and VISIT with an outcome of RISKFACTORS.


*------------------------------------------------------------;
*---------- dummy data generation ----------;
*------------------------------------------------------------;

data dummy;
   do subject = 1 to 100;
      do visit = -1, 12, 30, 60;
         random = rannor(1) + 0.5 + (visit+1)/30;
         riskfactors = min(3,floor(abs(random)));
         output;
      end;
   end;
run;

In the first call to the %SankeyBarChart macro we will use only the required parameters.


*------------------------------------------------------------;
*---------- required parameters only ----------;
*------------------------------------------------------------;

%sankeybarchart
   (data=dummy
   ,subject=subject
   ,yvar=riskfactors
   ,xvar=visit
   );

While this call did produce a plot, notice that the RISKFACTOR values are not well ordered in the legend. This is because the macro's default is to present data using the ORDER=DATA principle. Fortunately there is an optional parameter to override this default.


*------------------------------------------------------------;
*---------- adding yvarord ----------;
*------------------------------------------------------------;

%sankeybarchart
   (data=dummy
   ,subject=subject
   ,yvar=riskfactors
   ,xvar=visit
   ,yvarord=%quote(0, 1, 2, 3)
   );

Several other parameters exist to further enhance the figure aesthetics.


*------------------------------------------------------------;
*---------- more optional parameters for aesthetics ----------;
*------------------------------------------------------------;

proc format;
   value xfmt
   1 = 'Baseline'
   2 = 'Month 12'
   3 = 'Month 30'
   4 = 'Month 60'
   ;
run;

%sankeybarchart
   (data=dummy
   ,subject=subject
   ,yvar=riskfactors
   ,xvar=visit
   ,yvarord=%quote(0, 1, 2, 3)
   ,barwidth=0.45
   ,xfmt=xfmt.
   ,legendtitle=%quote(# of Risk Factors)
   );

Source Code

SAS
*------------------------------------------------------------;
*---------- dummy data generation ----------;
*------------------------------------------------------------;

data dummy;1
   do subject = 1 to 100;
      do visit = -1, 12, 30, 60;
         random = rannor(1) + 0.5 + (visit+1)/30;
         riskfactors = min(3,floor(abs(random)));
         output;
      end;
   end;
run;


*------------------------------------------------------------;
*---------- required parameters only ----------;
*------------------------------------------------------------;

%sankeybarchart
   (data=dummy
   ,subject=subject
   ,yvar=riskfactors
   ,xvar=visit
   );


*------------------------------------------------------------;
*---------- adding yvarord ----------;
*------------------------------------------------------------;

%sankeybarchart
   (data=dummy
   ,subject=subject
   ,yvar=riskfactors
   ,xvar=visit
   ,yvarord=%quote(0, 1, 2, 3)2
   );
 

*------------------------------------------------------------;
*---------- more optional parameters for aesthetics ----------;
*------------------------------------------------------------;

proc format;
   value xfmt
   1 = 'Baseline'
   2 = 'Month 12'
   3 = 'Month 30'
   4 = 'Month 60'
   ;
run;

%sankeybarchart
   (data=dummy
   ,subject=subject
   ,yvar=riskfactors
   ,xvar=visit
   ,yvarord=%quote(0, 1, 2, 3)
   ,barwidth=0.453
   ,xfmt=xfmt.4
   ,legendtitle=%quote(# of Risk Factors)5
   );

1 One record per subject and visit.

2 Put risk factors in desired order.

3 Makes the bars a bit wider.

4 Formats the x-axis tick values.

5 Adds a title to the legend.

Data

The above code generates raw data with random number functions. No data download is necessary.

top