Data visualization with SAS programming

Vbar in SGPLOT

Reply
Regular Contributor
Regular Contributor
Posts: 166

Vbar in SGPLOT

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 );
RUN; QUIT;

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
SAS Super FREQ
Posts: 1,081

Re: Vbar in SGPLOT

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.

Example:
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;
run;
Regular Contributor
Regular Contributor
Posts: 166

Re: Vbar in SGPLOT

Thaks a lot Sanjay,
Sadly we have not upgraded to M3 hence the DISCRETEOFFSET function doesnot work for us.
SAS Super FREQ
Posts: 8,721

Re: Vbar in SGPLOT

Hi:
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:
[pre]
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
[/pre]

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.

cynthia
[pre]
** 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;
output;

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

proc print data=classgen;
title ' classgen';
run;

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

proc sgplot data=classgen;
title 'Use only TYPE variable in SGPLOT';
vbar type / response=colval group=type;
run;
[/pre]
Ask a Question
Discussion stats
  • 3 replies
  • 243 views
  • 0 likes
  • 3 in conversation