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

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

 

 

 

desireatem_1-1629130322804.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

 

 

 

desireatem_1-1629130322804.png

 


 

View solution in original post

28 REPLIES 28
PaigeMiller
Diamond | Level 26

PROC SGPLOT with the HBAR statement.

--
Paige Miller
desireatem
Pyrite | Level 9

The Hbar statement won't solve it since it will simple have mrs values on the y -axis rather than type ("no" "yes")

PaigeMiller
Diamond | Level 26

Use HBAR TYPE

--
Paige Miller
desireatem
Pyrite | Level 9

Thank you, this is extremely helpful. How do I put the number into the different shaded regions as in the plot.

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
desireatem
Pyrite | Level 9

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.??

PaigeMiller
Diamond | Level 26

Explain what number you want in the bar chart. How is this number derived/computed from the data you show?

--
Paige Miller
desireatem
Pyrite | Level 9

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

Reeza
Super User
Both of your bars there are the exact same length which implies you're showing them as a stacked percentage but labelling them with counts. Is that how you want it to show or do you want the bar lengths to be different. If you want it as shown, I suspect you may need to pre-process the data to have it work properly because the labels do not match the axis so it's not using the pre-calculated statistics. And since a count won't align with a stacked 100% chart (they would if not 100%) then I suspect you need to pre-calculate things.
Reeza
Super User

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

 

 

 

desireatem_1-1629130322804.png

 


 

desireatem
Pyrite | Level 9

This is great! Thanks.

Reeza
Super User
lol, which answer was correct, the first SGPLOT or the second? Your question was very unclear so this was a lucky guess.
desireatem
Pyrite | Level 9

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;
Reeza
Super User
There are two sgplots in that code that generate different graphs, so which one is actually right from what you wanted initially.....

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
  • 28 replies
  • 2023 views
  • 6 likes
  • 3 in conversation