BookmarkSubscribeRSS Feed
L_L
Calcite | Level 5 L_L
Calcite | Level 5
Dear all,
I'm analyzing data for an observational study and I wuold like to present
some data in a horizontal bar chart.
A data example is:

Cod Var_strat Exam1 Exam2 Exam3
1 green Yes No No
2 red Yes No Yes
3 red No Yes No
4 green Yes No Yes

I would like to create a graph with the percentage of "Yes" for the three dichotomic variables (exam1 exam2 exam3) stratified by the variable "Var_strat" (green or red).

Anyone could help me?
Thanks in advance
5 REPLIES 5
Bill
Quartz | Level 8
First you will need to produce counts for the various combinations. Here's a proc freq method for exam1

proc freq;
table var_strat*exam1/out=exam1 noprint;;
run;

Put all the exam counts together in a data step and then you will be ready to move on to producing the chart with something like this;

proc gchart;
where exam1='Yes';
hbar var_strat/group=exam1 freq=percent;
run;
L_L
Calcite | Level 5 L_L
Calcite | Level 5
Many thanks Bill, I'll try with your suggestion

Have a good time
L_L
Calcite | Level 5 L_L
Calcite | Level 5
I run a proc freq keeping the variabile I need (row percent). Now I've this file:
TEST DATA SET
Var_strat rowpercent esame
Disease1 8.771929825 Esame1
Disease2 2.788844622 Esame1
Disease1 42.98245614 Esame2
Disease2 55.97609562 Esame2
Disease1 5.219298246 Esame3
Disease2 7.199203187 Esame3

I'll try with this code:

data test1;
set test;
length color $50.;
if var_strat='Disease1' then color='Disease1';
else color='Disease2';
run;

pattern1 c=red;
pattern2 c=blue;

goptions reset=global gunit=pct border cback=white
ctext=black
ftext=swiss ftitle=swissb
htitle=6 htext=3.5;

legend1 cborder=black
label=none
mode=protect
across=1;
axis2 label=none;


proc gchart;
hbar var_strat/
group=esame
freq=rowpercent
subgroup=color
width=8
space=0
coutline=black
maxis=axis2
frame raxis=0 to 100 by 10 minor=0
legend=legend1
run;

1) I would like to add the rowpercent value inside the HBAR: how can I do it?

2) I would like not to display in axis2 the name of var_strat (disease1, disease2) but only the name of the variable "esame"

Thanks in advance

Message was edited by: L&L Message was edited by: L&L
Bill
Quartz | Level 8
L & L;

I've made a few tweaks to make it work for me.

1 Move the pattern statements to after the goptions reset (you've avoided the red / green combination - that's a good thing)
2 Remove the legend (direct labeling is superior to legend "labeling")
3 Removed the coutline - it's just ink with no added value

Hbar charts (in 9.1 at least) do not support values inside or outside of the bars. The values are already listed on the right.

DATA test;
input Var_strat $ rowpercent esame $;
cards;
Disease1 8.771929825 Esame1
Disease2 2.788844622 Esame1
Disease1 42.98245614 Esame2
Disease2 55.97609562 Esame2
Disease1 5.219298246 Esame3
Disease2 7.199203187 Esame3
;
run;


data test1;
set test;
length color $50.;
if var_strat='Disease1' then color='Disease1';
else color='Disease2';
run;


goptions reset=global gunit=pct border cback=white
ctext=black
ftext=swiss ftitle=swissb
htitle=6 htext=3.5
dev=win;

pattern1 c=red;
pattern2 c=blue;

legend1 cborder=black
label=none
mode=protect
across=1;
axis2 label=none;


proc gchart;
hbar var_strat/
group=esame
freq=rowpercent
subgroup=color
width=8
space=0
/*coutline=black*/
maxis=axis2
frame raxis=0 to 100 by 10 minor=0
nolegend;
run;
L_L
Calcite | Level 5 L_L
Calcite | Level 5
Dear Bill,

Thank you so much for your reply!

:-))

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
  • 5 replies
  • 1184 views
  • 0 likes
  • 2 in conversation