BookmarkSubscribeRSS Feed
Quartz | Level 8 NN
Quartz | Level 8
Guys i was using the below code to create a bar line graph.

proc sgplot data = Test;
vbar 'XCOLUMN'N / response = 'Y1COLUMN'N;
vbar 'XCOLUMN'N / response = 'Y2COLUMN'N;
vline 'XCOLUMN'N /response = 'Y3COLUMN'N y2axis;
yaxis values= (0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1);
y2axis values = (0 .05 .1 .15 .2 .25 .3 .35 .4 );

My bars appear one on top of the other instead of being next to each other. Is it possible to get the bars side by side. Please Guide
Meteorite | Level 14
Using SGPLOT, it is not possible to have overlaid bars to be positioned side by side. However, if you are using SAS 9.2M3 (released recently), you can use GTL to have overlaid bars be placed side by side using the new DISCRETEOFFSET option for discrete date.

Normally, each bar is placed centered on the midpoint, with a default barwidth of 0.85 (85% of the mid point spacing). With SAS 9.2M3, you can specify a DISCRETEOFFSET=fraction (-0.5 to +0.5). This offsets the bars for that statement to left or right of the midpoint. Simultaneously, you can adjust the bar width to a smaller value.

BarChart x=x y=y1 / discreteoffset=-0.25 barwidth=0.5;
BarChart x=x y=y2 / discreteoffset= 0.25 barwidth=0.5;
SeriesPlot x=x y=Y3;

This will cause the bars from the two statements to appear side by side and touching.If you don't want them to touch, reduce the bar width a bit. Since no discreteoffset is specified for SeriesPlot, it will still be drawn with points at the X midpoints. It can be shifted too if needed. SeriesPlot does not summarize data. If you have one value per category of X, you are fine. Else, you have to summarize the data.

Use the tmplout='filename' option to get the GTL template from your SGPLOT step. Then, add the above options to the syntax, and run the template using the sgrender procedure. Template name is on the "Define Statgraph templatename;" statement.

proc sgrender data=foo template=templatename;
Quartz | Level 8 NN
Quartz | Level 8
Thaks a lot Sanjay,
Sadly we have not upgraded to M3 hence the DISCRETEOFFSET function doesnot work for us.
If you don't have M3 yet and you're willing to have a little DATA step dummy up some extra variables for category variables, you may be able to get side-by-side bars from SGPLOT.

In this code, I just used SASHELP.CLASS to dummy up some variables, so that the final data looks like this (only 10 obs shown). Note how every student now has 2 obs -- each with a numeric variable called "COLVAL" and the TYPE variable identifies whether the variable is for a y1column - M or a y1column - F or a y2column - M or a y2column - F:
Obs Name Sex type colval
1 Alfred M y1column - M 0.1380
2 Alfred M y2column - M 0.1400
3 Alice F y1column - F 0.1130
4 Alice F y2column - F 0.1300
5 Barbara F y1column - F 0.1306
6 Barbara F y2column - F 0.1300
7 Carol F y1column - F 0.1256
8 Carol F y2column - F 0.1400
9 Henry M y1column - M 0.1270
10 Henry M y2column - M 0.1400

This may give you something close to what you want until you get to M3. For simplicity of example, I didn't add a VLINE to this, but your datapoints would need to be manipulated the same way for VLINE.

** there is one obs for y1column and one obs for y2column;
** type var is string for y1 or y2 concatenated with sex for obs;
data classgen (keep=name colval type sex);
set sashelp.class;
type = catx(' ', 'y1column -', sex);
colval = height/500;

type = catx(' ', 'y2column -', sex);
colval = age / 100;

proc print data=classgen;
title ' classgen';

proc sgplot data=classgen;
title 'Use TYPE and SEX variables in SGPLOT';
vbar type / response=colval group=sex;

proc sgplot data=classgen;
title 'Use only TYPE variable in SGPLOT';
vbar type / response=colval group=type;



Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg



Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 3 in conversation