I have the following data and wish to produce the graph below where type is 0=no and 1=yes.
Please any help is more than welcome.
Type mrs (90 days outcome)
0 5
1 1
1 2
1 0
0 3
1 6
0 4
This should answer your question. There are two options below, not really sure which one you're after. If they don't answer it exactly they should be pretty close.
If they do not work please be very very specific about how.
data random_data;
call streaminit(99);
do RECORD_NUM = 1 to 100;
TYPE = rand("bernoulli", 0.5);
mRS = rand('integer', 0, 6);
output;
end;
run;
proc format;
value mRS_fmt
0 -1 = "mRS 0 - 1"
2 = "mRS 2"
3-5 = "mRS 5"
6 = "mRS 6";
run;
proc freq data=random_data;
table Type*MRS / out=summary_data outpct;
format mRS mRS_fmt.;
run;
proc sgplot data=summary_data;
hbar type / group = mRS response = pct_row stat=pct;
*to get counts labelled;
hbar type / group = mRS response = count stat=sum seglabel x2axis;
x2axis display=none;
run;
proc sgplot data=random_data;
hbar type / group = mRS stat=pct;
hbar type / group = mRS stat=freq seglabel x2axis;
x2axis display=none;
format mRS mRS_fmt.;
run;
@desireatem wrote:
I have the following data and wish to produce the graph below where type is 0=no and 1=yes.
Please any help is more than welcome.
Type mrs (90 days outcome)
0 5
1 1
1 2
1 0
0 3
1 6
0 4
PROC SGPLOT with the HBAR statement.
The Hbar statement won't solve it since it will simple have mrs values on the y -axis rather than type ("no" "yes")
Use HBAR TYPE
Thank you, this is extremely helpful. How do I put the number into the different shaded regions as in the plot.
I'm still not 100% sure what you are doing, as the numbers in your table don't seem to match the numbers in the plot.
There are a number of options in the HBAR documentation segment named "Label Options", probably one of them does what you want.
That is just part of the data. In the grotta bar graph that I pasted for type "no" we have number 88, 7, 41... How do I have similar plot with different n.??
Explain what number you want in the bar chart. How is this number derived/computed from the data you show?
For instant for type =no, if 88 people had mrs=1, the number 88 will show within the box in the plot, if 7 had mrs=2, the number 2 will appear in corresponding color within the box. That is how the figure above is. You see the sample size within each color, corresponding to the number of people with that mrs value
This should answer your question. There are two options below, not really sure which one you're after. If they don't answer it exactly they should be pretty close.
If they do not work please be very very specific about how.
data random_data;
call streaminit(99);
do RECORD_NUM = 1 to 100;
TYPE = rand("bernoulli", 0.5);
mRS = rand('integer', 0, 6);
output;
end;
run;
proc format;
value mRS_fmt
0 -1 = "mRS 0 - 1"
2 = "mRS 2"
3-5 = "mRS 5"
6 = "mRS 6";
run;
proc freq data=random_data;
table Type*MRS / out=summary_data outpct;
format mRS mRS_fmt.;
run;
proc sgplot data=summary_data;
hbar type / group = mRS response = pct_row stat=pct;
*to get counts labelled;
hbar type / group = mRS response = count stat=sum seglabel x2axis;
x2axis display=none;
run;
proc sgplot data=random_data;
hbar type / group = mRS stat=pct;
hbar type / group = mRS stat=freq seglabel x2axis;
x2axis display=none;
format mRS mRS_fmt.;
run;
@desireatem wrote:
I have the following data and wish to produce the graph below where type is 0=no and 1=yes.
Please any help is more than welcome.
Type mrs (90 days outcome)
0 5
1 1
1 2
1 0
0 3
1 6
0 4
This is great! Thanks.
This is the correct answer:
data random_data;
call streaminit(99);
do RECORD_NUM = 1 to 100;
TYPE = rand("bernoulli", 0.5);
mRS = rand('integer', 0, 6);
output;
end;
run;
proc format;
value mRS_fmt
0 -1 = "mRS 0 - 1"
2 = "mRS 2"
3-5 = "mRS 5"
6 = "mRS 6";
run;
proc freq data=random_data;
table Type*MRS / out=summary_data outpct;
format mRS mRS_fmt.;
run;
proc sgplot data=summary_data;
hbar type / group = mRS response = pct_row stat=pct;
*to get counts labelled;
hbar type / group = mRS response = count stat=sum seglabel x2axis;
x2axis display=none;
run;
proc sgplot data=random_data;
hbar type / group = mRS stat=pct;
hbar type / group = mRS stat=freq seglabel x2axis;
x2axis display=none;
format mRS mRS_fmt.;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.