Hello - I know questions like mine have been asked before, but I wasn't able to find an answer that I could adapt to my situation. I would like to create a single bar chart with individual bars for several variables in my dataset, where the values for each variable are either 0 or 1 for each record in the dataset.
I created a series of binary (0/1) variables from questions on a survey where respondents could select one more options (e.g. bn_option1, bn_option2, ...) . Now I'd like to create bar charts for the % of '1's for each option (binary variable) of the corresponding question. One bar chart per question with as many bars as there were originally options. Thanks.
data have;
input Id Option1 Option2 Option3 Option4;
cards;
1 0 0 0 1
2 0 0 1 0
3 0 1 0 1
4 1 0 1 1
;
run;
ods select none;
ods output summary=summary_results;
proc means data=have stackods mean;
var option1-option4;
run;
ods select all;
proc sgplot data=summary_results;
vbarparm response=mean category=variable;
format mean percent12.1;
label mean = 'Response';
run;
First of all, are you looking for help doing this in SAS?
I created a series of binary (0/1) variables from questions on a survey where respondents could select one more options (e.g. bn_option1, bn_option2, ...) . Now I'd like to create bar charts for the % of '1's for each option (binary variable) of the corresponding question. One bar chart per question with as many bars as there were originally options.
Please show us a portion of this data, provided as working SAS data step code (Examples and Instructions)
Yes, Paige, I am.
That would be better to demonstrate your question by posing some data and the picture you want to see.
data have;
call streaminit(123);
array x{*} opt1-opt6;
do id=1 to 100;
do i=1 to dim(x);
x{i}=rand('bern',i/10);
end;
output;
end;
drop i;
run;
proc transpose data=have out=want;
by id;
var opt:;
run;
proc sgplot data=want pctlevel=group;
vbar _name_/group=col1 stat=percent ;
run;
Thank you. Here's sample data and a desired bar chart (without axis labeling).
Id | Option1 | Option2 | Option3 | Option4 |
1 | 0 | 0 | 0 | 1 |
2 | 0 | 0 | 1 | 0 |
3 | 0 | 1 | 0 | 1 |
4 | 1 | 0 | 1 | 1 |
data have;
input Id Option1 Option2 Option3 Option4;
cards;
1 0 0 0 1
2 0 0 1 0
3 0 1 0 1
4 1 0 1 1
;
run;
ods select none;
ods output summary=summary_results;
proc means data=have stackods mean;
var option1-option4;
run;
ods select all;
proc sgplot data=summary_results;
vbarparm response=mean category=variable;
format mean percent12.1;
label mean = 'Response';
run;
Thank you. That's perfect!
Hi Reeza,
Your solution to my bar chart question was elegant and works. One minor additional question. Is it possible to display the labels I have for options1-options4 for the x-axis labeling rather than the variable names?
My code based on your answer is what I have below. Both q_7760_bn1 and q7760_bn2 have labels associated with them from the original dataset (ips.allvars3) that I see with a proc contents on summary_results.
Thanks.
ods select none;
ods output summary=summary_results;
proc means data=ips.allvars3 stackods mean;
var q7760_bn1 q7760_bn2;
run;
ods select all;
proc sgplot data=summary_results;
vbarparm response=mean category=variable;
format mean percent12.1;
label mean = 'Response';
run;
Change category= variable from variable to label, in the PROC SGPLOT code.
data have;
input Id Option1 Option2 Option3 Option4;
label option1 = 'Question #99' option2 = 'Random Text #2' option3 = 'Demoe' option4 = 'House';
cards;
1 0 0 0 1
2 0 0 1 0
3 0 1 0 1
4 1 0 1 1
;
run;
ods select none;
ods output summary=summary_results;
proc means data=have stackods mean;
var option1-option4;
run;
ods select all;
proc sgplot data=summary_results;
vbarparm response=mean category=label;
format mean percent12.1;
label mean = 'Response';
run;
@NeilH wrote:
Hi Reeza,
Your solution to my bar chart question was elegant and works. One minor additional question. Is it possible to display the labels I have for options1-options4 for the x-axis labeling rather than the variable names?
My code based on your answer is what I have below. Both q_7760_bn1 and q7760_bn2 have labels associated with them from the original dataset (ips.allvars3) that I see with a proc contents on summary_results.
Thanks.
ods select none;
ods output summary=summary_results;
proc means data=ips.allvars3 stackods mean;
var q7760_bn1 q7760_bn2;
run;
ods select all;
proc sgplot data=summary_results;
vbarparm response=mean category=variable;
format mean percent12.1;
label mean = 'Response';
run;
Worked perfectly, Reeza!
My one issue with the current labels I have, is that a few of them are quite long and crowd out the chart. I'll research this, but if you know of a word wrap option that can be applied to the labels in sgplot or an alternative, that would be great. I think I see how to add a line break in the label itself when it's set - (*ESC*){unicode '000a'x}. But it's not appearing as I'd hope in the chart itself. Below is an example of a lengthy label without a newline in it. The chart displays perfectly for shorter labels.
FITPOLICY and SPLITCHAR on XAXIS statement.
data have;
input Id Option1 Option2 Option3 Option4;
label option1 = 'Question really long text # to force split 99' option2 = 'Random Text with #another random split' option3 = 'Demoe' option4 = 'House';
cards;
1 0 0 0 1
2 0 0 1 0
3 0 1 0 1
4 1 0 1 1
;
run;
ods select none;
ods output summary=summary_results;
proc means data=have stackods mean;
var option1-option4;
run;
ods select all;
proc sgplot data=summary_results;
vbarparm response=mean category=label;
format mean percent12.1;
label mean = 'Response';
xaxis fitpolicy=split splitchar='#';
run;
@NeilH wrote:
Worked perfectly, Reeza!
My one issue with the current labels I have, is that a few of them are quite long and crowd out the chart. I'll research this, but if you know of a word wrap option that can be applied to the labels in sgplot or an alternative, that would be great. I think I see how to add a line break in the label itself when it's set - (*ESC*){unicode '000a'x}. But it's not appearing as I'd hope in the chart itself. Below is an example of a lengthy label without a newline in it. The chart displays perfectly for shorter labels.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.