BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
telligent
Quartz | Level 8

How do I make a bar chart with N on the y-axis, a category on the x-axis and percent as a label on the bars without creating an output data set with proc freq or something similiar. Just using raw data. . . 

 

Thank you.  

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

The hacky method that comes to mind is overlaying two plots, one with invisible bars that shows the percentages on a non-shown dual axis. So the labels will come from one plot and the data/bars from another. You will need to customize your legend using keylegend - I'm leaving that for you.

 

 

proc sgplot data=sashelp.class;
hbar sex / stat=freq;
hbar sex / stat=percent x2axis datalabel nooutline;
x2axis display=none;
run;

@telligent wrote:

How do I make a bar chart with N on the y-axis, a category on the x-axis and percent as a label on the bars without creating an output data set with proc freq or something similiar. Just using raw data. . . 

 

Thank you.  


FYI - replace HBAR with VBAR and X2AXIS with Y2AXIS for a vertical layout.

View solution in original post

7 REPLIES 7
Reeza
Super User

The hacky method that comes to mind is overlaying two plots, one with invisible bars that shows the percentages on a non-shown dual axis. So the labels will come from one plot and the data/bars from another. You will need to customize your legend using keylegend - I'm leaving that for you.

 

 

proc sgplot data=sashelp.class;
hbar sex / stat=freq;
hbar sex / stat=percent x2axis datalabel nooutline;
x2axis display=none;
run;

@telligent wrote:

How do I make a bar chart with N on the y-axis, a category on the x-axis and percent as a label on the bars without creating an output data set with proc freq or something similiar. Just using raw data. . . 

 

Thank you.  


FYI - replace HBAR with VBAR and X2AXIS with Y2AXIS for a vertical layout.

Reeza
Super User
Show your code.
I got those results when I didn't set up the x2/y2 axis correctly.
telligent
Quartz | Level 8

proc sgplot data=one noautolegend;
vbar q1 / stat=freq;
xaxis display=(nolabel) ;
vbar q1 /stat=percent x2axis datalabel nooutline;
x2axis display=none;
yaxis integer max=&n;
run;

Reeza
Super User
You changed HBAR but missed the change of X2AXIS to the Y2AXIS.
So that statement doesn't actually make sense.
telligent
Quartz | Level 8

I noticed the statement below that I added throws the whole thing off. When I do this the bar heights are wrong relative to the y axis. Do you have any suggestions for me?  I want the y-axis to go up to a certain level and the bar heights to still be correct. Thank you. 

 

yaxis integer max=100;

Reeza
Super User
I'm guessing you didn't set both axis to the same or overwrote your prior YAXIS statement. But with just a single line of code that's an example, post your code, log and the sample output please.

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
  • 7 replies
  • 1741 views
  • 6 likes
  • 2 in conversation