SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
desireatem
Pyrite | Level 9

Hello 

 

I have the bar chart below for same variable. I have plot two graphs below with a where statement "where are_you_the=1;".

 

Please, can you show me how to plot them side by side on the same code or same graph separated by different colors.??

 

 

proc format;
value Age
1='18-25' 2='26-35' 3='36-45' 4='46-55' 5='56-65' 6='66-75';
run;

*title "Vertical Bar Chart";
title2 "Age Distribution";
proc sgplot data=JJ noborder;
vbar Your_Age/ stat=Percent colormodel=twocolorramp;
where are_you_the=1;
xaxis label='Age' values=(1 to 6 by 1);
format Your_Age Age.;
xaxis display=(nolabel noline noticks);
yaxis display=(noline) grid;
keylegend / noborder;
*format mpg_city 8.0;
run;
title;

*title "Vertical Bar Chart";
title2 "Age Distribution";
proc sgplot data=JJ noborder;
vbar Your_Age/ stat=Percent colormodel=twocolorramp;
where are_you_the=2;
xaxis label='Age' values=(1 to 6 by 1);
format Your_Age Age.;
xaxis display=(nolabel noline noticks);
yaxis display=(noline) grid;
keylegend / noborder;
*format mpg_city 8.0;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Not sure you want two individual side by side graphs or clustered bar graph. 

If side by side use @ballardw solution. If clustered bar graph try below. 

Change this

vbar Your_Age/ stat=Percent colormodel=twocolorramp;
where are_you_the=2;

To something like:

 

vbar Your_Age/ stat=Percent colormodel=twocolorramp group = are_you_the groupdisplay = cluster;

Specify the GROUP = variable and add the GROUPDISPLAY type to control if they're stacked or clustered. See the documentation for the details - guessed at code so check docs if there's any error, I may have the terms wrong. 

View solution in original post

3 REPLIES 3
ballardw
Super User

No data provided so can't test but Proc SGPANEL is designed to create row/columns or matrix of similar graphs separated by the value of variable(s) indicated on the PANELBY statement.

I am assuming that you want two entire graphs.

From you plot perhaps:

title2 "Age Distribution";
proc sgpanel data=JJ noborder;
   panelby are_you_the /columns=2
   vbar Your_Age/ stat=Percent colormodel=twocolorramp;
 
   format Your_Age Age.;
   Colaxis label='Age' values=(1 to 6 by 1) display=(nolabel noline noticks);
   rowaxis display=(noline) grid;
   keylegend / noborder;
run;

Since there are multiple graphs the axis are controlled with colaxis, the axis at the bottom of the graph, and rowaxis, the one on the side. There are some options related to whether all graphs in the panel have the same or different axis displays.

 

This assumes you only have two values for your You_are_the variable. If you have more you may get more than one row, or if there are many values a plot that won't fit in the graph space.

 

NOTE: You want to be careful with multiple XAXIS or YAXIS statements in a single procedure call. One may overwrite options in the other.

 

If you meant that you want the 1 and 2 valued bars next to each other then you wanted a Group option:

proc sgplot data=JJ noborder;
vbar Your_Age/ stat=Percent group=are_you_the groupdisplay=cluster
      colormodel=twocolorramp colorstat=percent;

xaxis label='Age' values=(1 to 6 by 1);
format Your_Age Age.;
xaxis display=(nolabel noline noticks);
yaxis display=(noline) grid;
keylegend / noborder;
*format mpg_city 8.0;
run;

Group means to display values of the grouping variable, the groupdisplay=cluster means the bars are side-by-side instead of the default vertical stack. The Colorramp needs a value to apply colors with, in this case the Colorstat=percent says to use the statistic percent for the color to "ramp" the shades. If you want the colors of the Group variable to be the same for each bar of the group don't use the colorramp options, the default will assign different colors to each value of the group variable.

Reeza
Super User

Not sure you want two individual side by side graphs or clustered bar graph. 

If side by side use @ballardw solution. If clustered bar graph try below. 

Change this

vbar Your_Age/ stat=Percent colormodel=twocolorramp;
where are_you_the=2;

To something like:

 

vbar Your_Age/ stat=Percent colormodel=twocolorramp group = are_you_the groupdisplay = cluster;

Specify the GROUP = variable and add the GROUPDISPLAY type to control if they're stacked or clustered. See the documentation for the details - guessed at code so check docs if there's any error, I may have the terms wrong. 

Reeza
Super User
I would suggest bookmarking this page. Tons of examples that you can follow:
http://robslink.com/SAS/ods2/aaaindex.htm

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 2238 views
  • 2 likes
  • 3 in conversation