BookmarkSubscribeRSS Feed
uzma03505621
Obsidian | Level 7

Hello everyone,

I want to create a bar chart as shown in the picture. Will I be able to do it using SAS. If yes, can someone help me with the syntax for the same? 

 

Thank you for your time and consideration.

 

I used the following syntax but did not get the desired output as I have 2 response variables (mental&physical), also I want to know how I can add different colors?

 

PROC SGPLOT DATA = testrun;
VBAR category / RESPONSE = mental stat=mean barwidth=0.1 ;
TITLE 'Mental activity score';
RUN;

 

Blue: Category1   Red=category2  green=category3sasques.png

2 REPLIES 2
ballardw
Super User

To make the bars as you show you need at least one additional variable to use as a Group variable.

The code would look something like:

PROC SGPLOT DATA = testrun;
   VBAR category / RESPONSE = mental stat=mean barwidth=0.1
                             group=groupingvariablename groupdisplay=cluster
    ;
TITLE 'Mental activity score';
RUN;

The Groupdisplay=cluster is needed because the default is to stack the elements.

There are several ways to control colors based on a grouping variable. One way is to add a STYLEATTRS statement to override the default colors your current ODS style would set. The basic way would look like:

PROC SGPLOT DATA = testrun;
   styleattrs
       datacolors(red blue green)
   ;
   VBAR category / RESPONSE = mental stat=mean barwidth=0.1
                             group=groupingvariablename groupdisplay=cluster
    ;
TITLE 'Mental activity score';
RUN;

The order of the values of the group variable in the data set will determine which color gets with which value. If the color order doesn't match what you want then change the order of the colors in the datacolors list.

Be aware that if you change the order of the data that the values may not align next time.

Which brings up the method to control graphic attributes based on the value: DATTRMAP, which stands for Discrete Attribute Map, and is specially structured data with specifically named variables that correspond to the values of your group variable, and identifier to tell which set of options are applied to which graph and the options. This is a "read the documentation" question as it is moderately long.

GraphGuy
Meteorite | Level 14

If you pre-summarize your data (which is something I prefer to do in bar charts), then you could create a similar chart like this:

 

data my_data;
length type $10 cat $10;
label score='Mean Score';
input type cat score;
datalines;
Mental Category1 37.50
Mental Category2 36.20
Mental Category3 37.90
Physical Category1 41.80
Physical Category2 42.80
Physical Category3 38.00
;
run;

 

title1 "My Bar Chart";
proc sgplot data=my_data;
styleattrs datacolors=(cx4f81bc cxc0504e cx9bbb58);
vbarparm category=type response=score / datalabel=score
group=cat groupdisplay=cluster;
xaxis display=(nolabel);
yaxis values=(32 to 44 by 2);
keylegend / title='' position=topleft;
run;

 

bar_bad.png

 

*But* - that bar chart (above) is what I would call a bad/deceptive one. With very rare exceptions, the axis for the bar heights should start at zero. Here is the code to do it that way:

 

title1 "My Bar Chart";
proc sgplot data=my_data;
styleattrs datacolors=(cx4f81bc cxc0504e cx9bbb58);
vbarparm category=type response=score / datalabel=score
group=cat groupdisplay=cluster;
xaxis display=(nolabel);
keylegend / title='' position=topleft;
run;

 

bar_good.png

 

 

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
  • 2 replies
  • 651 views
  • 3 likes
  • 3 in conversation