Data visualization with SAS programming

Creating Horizontal Bars for two variables in one chart

Reply
Occasional Contributor
Posts: 17

Creating Horizontal Bars for two variables in one chart

Hi,

I need help in creating a chart where I can see the bars for two variables (say sales last year and sales this year).

For example if I have a variable called region and it has 4 values - North, South, East and West and each of this regions has values of last year sales and this year sales ( this sales are in different variables) Now I want the chart with horizontal bars such that I can see the bar for this year and last year one below the other for each region. Can some one please help with the proc gchart code for this?

Thanks
Sohail Mohammad
SAS Super FREQ
Posts: 8,819

Re: Creating Horizontal Bars for two variables in one chart

Hi:
If you have data that is structured like this:
[pre]
Region Sales2009 Sales2010
East 543 487
North 500 565
South 825 750
West 600 700
[/pre]

Then you need to use something like PROC TRANSPOSE or a DATA step program to restructure the data so it looks like this:
[pre]
Region Year Sales
East sales2009 543
East sales2010 487
North sales2009 500
North sales2010 565
South sales2009 825
South sales2010 750
West sales2009 600
West sales2010 700
[/pre]

PROC GCHART is more able to plot the bars in groups and subgroups when the numeric variable has the same name and the grouping or subgrouping variable has the value that will make up the separate bars and/or separate groups.

For example, if you have the restructured data (as shown above) in a file called WORK.GRF_OUT, then a PROC GCHART that would give you a horizontal bar chart with the differing years and regions grouped together would be code similar to this. Change the SPACE and GSPACE= values to adjust the space between the individual bars and the bars for each group.

cynthia

[pre]
proc gchart data=grf_out;
hbar year / type=sum sumvar=sales
group=region subgroup=year
space=0 coutline=same
gspace=5;
label year='Year'
region='Region'
sales='Sales';
run;
quit;
[/pre]
SAS Super FREQ
Posts: 703

Re: Creating Horizontal Bars for two variables in one chart

Ho Soha

You can also use the SG... procs to achieve the result, see example below. They allow to overlay plots with some settings for barwidth and discreteoffset you get two bar beside each other. With this example there is no need to change our data

data plotData;
  infile cards;
 
input
    Region $
    Sales2009
    Sales2010
  ;
cards;
East  543 487
North 500 565
South 825 750
West  600 700
;

proc sgplot data=plotData;
  hbar region /
   
response=sales2009
    discreteoffset=-
0.2
   
barwidth=0.4
  ;
  hbar region /
   
response=sales2010
    discreteoffset=
0.2
   
barwidth=0.4
   
transparency=0.5
  ;
run;
SAS Super FREQ
Posts: 1,139

Re: Creating Horizontal Bars for two variables in one chart

DISCRETEOFFSET is supported with SAS 9.3 SGPLOT.  You can turn off stat labels to remove the (Sum) suffix to the label in the legend.  You can also creatively set bar width and discrete offsets to get an overlap effect or separate the bars completely.

title 'Sales by Region';

ods listing style=listing;

proc sgplot data=plotData cycleattrs;

  hbar region / nostatlabel response=sales2009 discreteoffset=-0.2 barwidth=0.5;

  hbar region / nostatlabel response=sales2010 discreteoffset=0.2  barwidth=0.5;

  xaxis display=(nolabel);

run;

Ask a Question
Discussion stats
  • 3 replies
  • 279 views
  • 0 likes
  • 4 in conversation