May 27, 2021 SAS
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.
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:
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.
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:
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.
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:
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.
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: