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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1651 views
  • 6 likes
  • 2 in conversation