Published on :
Reporting INTERNAL_CREATION

Bar chart with axis breaks

This code is also available in: Deutsch Français
Awaiting validation
The script begins by creating a 'plantProd' dataset containing production data for different sites and periods. A trick is used to create breaks in the X-axis of the graph: points in the 'Time' variable are replaced by non-breaking spaces. Then, a PROC TEMPLATE procedure defines a custom graph template ('chunked') that superimposes a bar chart (BarChartParm) and a series plot (SeriesPlot). Finally, PROC SGRENDER uses this template and the data to generate the final chart, demonstrating how to visually separate groups of categories on a discrete axis.
Data Analysis

Type : INTERNAL_CREATION


The data is created directly within the SAS script via a DATA STEP and the DATALINES statement.

1 Code Block
DATA STEP Data
Explanation :
This DATA STEP block creates the 'plantProd' table. It reads raw data via 'datalines', defines variable attributes (labels, formats), and replaces periods in the 'Time' variable with non-breaking spaces ('A0'x). This substitution is a technique to force spacing on a discrete axis in an SG graph.
Copied!
1%let nbsp='A0'x; /* ASCII: ’A0’x, UTF-8: ’C2A0’x, EBCDIC: ’41’x */
2 
3DATA plantProd;
4 attrib prod label='Production'
5 prod_pct label='Production %' FORMAT=percent5.2;
6 INPUT prod prod_pct Time $ Site $;
7 Time=translate(Time,  ., '.'); /* map '.' to non-breaking space */
8 DATALINES;
9321 0.0334 w01 US64
10373 0.0173 w01 CA41
11218 0.0367 w08 US64
12420 0.0188 w08 CA41
13117 0.0163 w16 US64
14461 0.0190 w16 CA41
1564 0.0441 w24 US64
16320 0.0208 w24 CA41
17156 0.0261 w32 US64
18620 0.0116 w32 CA41
19115 0.0193 w40 US64
20700 0.0058 w40 CA41
21110 0.0091 w48 US64
22642 0.0039 w48 CA41
23157 0.0099 w52 US64
24586 0.0012 w52 CA41
25. . .. US64
26. . .. CA41
274657 0.0315 Q1 US64
282491 0.0162 Q1 CA41
291434 0.0251 Q2 US64
302147 0.0112 Q2 CA41
311696 0.0314 Q3 US64
323206 0.0155 Q3 CA41
332895 0.0399 Q4 US64
344174 0.0226 Q4 CA41
35. . ... US64
36. . ... CA41
376091 0.0294 H1 US64
384638 0.0145 H1 CA41
394591 0.0364 H2 US64
407380 0.0197 H2 CA41
41;
42RUN;
2 Code Block
PROC TEMPLATE
Explanation :
This block defines a statistical graph template named 'chunked' using PROC TEMPLATE. The template superimposes a bar chart (BarChartParm) and a series plot (SeriesPlot). The X-axis is configured as 'discrete' to treat each 'Time' value as a distinct category. The secondary Y-axis (Y2) is used for the percentage curve.
Copied!
1PROC TEMPLATE;
2 define statgraph chunked;
3 beginGraph;
4 entryTitle "Categorical Axis with Gaps" ;
5 layout overlay / xAxisopts=( display=(tickvalues line) type=discrete )
6 yAxisopts=( griddisplay=on offsetmin=0 )
7 y2Axisopts=( offsetmin=0 );
8 BarChartParm X=time Y=prod / group=Site name="bar" fillAttrs=(transparency=0.2);
9 SeriesPlot X=time Y=prod_pct / group=Site display=all break=true yaxis=y2
10 markerattrs=(symbol=squareFilled)
11 lineattrs=(pattern=solid thickness=2) ;
12
13 DiscreteLegend "bar" / title="Site:";
14 endlayout;
15 entryFootnote halign=left
16 "Uses non-breaking spaces to add axis gaps" ;
17 endgraph;
18 END;
19RUN;
3 Code Block
PROC SGRENDER
Explanation :
This procedure executes the rendering of the graph. It applies the 'chunked' template (defined previously) to the 'plantProd' dataset to generate the final graph, which will show production by site and period with visual breaks.
Copied!
1 
2PROC SGRENDER template=chunked
3DATA=plantProd;
4RUN;
5 
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.