BookmarkSubscribeRSS Feed
Soha
Calcite | Level 5
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
3 REPLIES 3
Cynthia_sas
SAS Super FREQ
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]
BrunoMueller
SAS Super FREQ

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;
Jay54
Meteorite | Level 14

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1368 views
  • 0 likes
  • 4 in conversation