Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

SAS pie chart


May 27, 2021 SAS


Table of contents


A pie chart is a slice that represents a value as a circle with a different color. The slices are marked, and the numbers corresponding to each slice are also represented in the chart.
In SAS, pie charts are created using PROC TEMPLATE, which uses parameters to control percentages, labels, colors, titles, and so on.

Grammar

The basic syntax to create a pie chart for SAS is:

PROC TEMPLATE;
  DEFINE STATGRAPH pie;
    BEGINGRAPH;
      LAYOUT REGION;
        PIECHART CATEGORY = variable /
          DATALABELLOCATION = OUTSIDE
          CATEGORYDIRECTION = CLOCKWISE
          START = 180 NAME = 'pie';
        DISCRETELEGEND 'pie' /
          TITLE = ' ';
      ENDLAYOUT;
    ENDGRAPH;
  END;
RUN;

The following is a description of the parameters used:

  • The variable is the value for which we created the pie chart.

Simple pie chart

In this pie chart, we get a single variable from the data set. W hen you create a pie chart, use a slice value that represents the fraction of the variable count relative to the total value of the variable.

Cases

In the following example, each slice represents the ratio of the car type to the total number of cars.

PROC SQL;
create table CARS1 as
SELECT make,model,type,invoice,horsepower,length,weight
 FROM 
SASHELP.CARS
WHERE make in ('Audi','BMW')
;
RUN;

PROC TEMPLATE;
  DEFINE STATGRAPH pie;
    BEGINGRAPH;
      LAYOUT REGION;
        PIECHART CATEGORY = type /
          DATALABELLOCATION = OUTSIDE
          CATEGORYDIRECTION = CLOCKWISE
          START = 180 NAME = 'pie';
        DISCRETELEGEND 'pie' /
          TITLE = 'Car Types';
      ENDLAYOUT;
    ENDGRAPH;
  END;
RUN;
PROC SGRENDER DATA = cars1
          TEMPLATE = pie;
RUN;

When we execute the code above, we get the following output:

SAS pie chart

A pie chart with a data label

In this pie chart, we represent the score value and percentage value of each slice. W e also changed the position of the label to within the chart. U se the DATASKIN option to modify the appearance style of the chart. I t uses one of the built-in styles provided in the SAS environment.

Cases

PROC TEMPLATE;
  DEFINE STATGRAPH pie;
    BEGINGRAPH;
      LAYOUT REGION;
        PIECHART CATEGORY = type /
          DATALABELLOCATION = INSIDE
          DATALABELCONTENT=ALL
          CATEGORYDIRECTION = CLOCKWISE
          DATASKIN= SHEEN 
          START = 180 NAME = 'pie';
        DISCRETELEGEND 'pie' /
          TITLE = 'Car Types';
      ENDLAYOUT;
    ENDGRAPH;
  END;
RUN;
PROC SGRENDER DATA = cars1
          TEMPLATE = pie;
RUN;

When we execute the code above, we get the following output:

SAS pie chart

Group pie charts

In this pie chart, the values of the variables rendered in the figure are grouped relative to another variable in the same dataset. E ach group becomes a circle, and the graph has as many con center circles as the number of available groups.

Cases

In the following example, we group the chart relative to a variable named Make. S ince there are two values available ("Audi" and "BMW"), we get two contelliar circles, each representing a slice of its own car type.

PROC TEMPLATE;
  DEFINE STATGRAPH pie;
    BEGINGRAPH;
      LAYOUT REGION;
        PIECHART CATEGORY = type / Group = make
          DATALABELLOCATION = INSIDE
          DATALABELCONTENT=ALL
          CATEGORYDIRECTION = CLOCKWISE
          DATASKIN= SHEEN 
          START = 180 NAME = 'pie';
        DISCRETELEGEND 'pie' /
          TITLE = 'Car Types';
      ENDLAYOUT;
    ENDGRAPH;
  END;
RUN;
PROC SGRENDER DATA = cars1
          TEMPLATE = pie;
RUN;

When we execute the above code, we get the following output:

SAS pie chart