I trying to create a stacked bar chart comparing MA and TM groups on 8 different binary variables (I've only shown 2 here for simplicity) and 1 discrete (0-8) continuous variable. I've created a chart for the continuous variable, and it's pretty close to what I need. Here's what I need to change:
1) The bars are the same color, so it's not clear which group is higher/lower.
2) I'd like to add the name of the group that goes to each color.
3) I need to replace the variable name total_qual_care_score with a label such as "Total Quality of Care Score".
For the binary variables, I'd like to have both in a single chart. I haven't been able to create an example chart, but imagine that only the 0 and 1 bars exist. And instead of 0 and 1, the columns represent ACC_HCTROUBL_r and ACC_HCDELAY_r (with the labels "Trouble getting care" and "Delay in getting care", respectively).
Here's my current code with sample data:
data have;
infile datalines dsd dlm=',' truncover;
input Obs cohort_flag MA_non_ADRD_group TM_non_ADRD_group total_qual_care_score ACC_HCTROUBL_r ACC_HCDELAY_r;
datalines;
1,1,1,0,7,1,0
2,1,0,1,7,0,1
3,1,0,1,7,1,0
4,1,0,0,1,0,1
5,1,0,0,8,0,1
6,1,0,1,7,0,1
7,1,0,1,3,1,0
8,1,0,1,7,0,1
9,1,0,1,8,1,0
10,1,1,0,5,0,1
11,1,1,0,8,0,0
12,1,0,1,8,1,1
13,1,1,0,8,0,1
14,0,,,7,0,1
15,1,0,1,8,0,1
16,1,0,1,8,0,0
17,1,1,0,8,0,1
18,1,0,0,7,0,0
19,1,1,0,8,1,0
20,1,0,1,6,0,1
21,1,0,1,7,1,1
22,1,1,0,7,0,0
23,1,0,1,5,1,0
24,1,0,1,8,0,1
25,1,0,1,8,0,1
; RUN;
title1 "Section 1.2 -- Fig1 Unadj rates quality care TM vs MA without ADRD";
title2 "Version &version.";
PROC MEANS data=have mean n lclm uclm stackods;
class total_qual_care_score;
var TM_non_ADRD_group MA_non_ADRD_group;
ods output summary=temp.TM_MA_groupMean;
WHERE cohort_flag = 1 AND (TM_non_ADRD_group = 1 OR MA_non_ADRD_group = 1);
RUN;
PROC SGPLOT data=temp.TM_MA_groupMean;
vbarparm category=/*variable*/ total_qual_care_score response=mean /
limitlower=lclm
limitupper=uclm;
label mean="Proportion satisfied";
RUN;
Hello,
Koen
stacked bar chart in SAS site:blogs.sas.com
stacked bar chart in SAS site:support.sas.com
Google Search.
Koen
PROC SGPLOT data=TM_MA_groupMean;
vbarparm category=total_qual_care_score response=mean / group=variable groupdisplay=cluster
limitlower=lclm limitupper=uclm;
yaxis label="Proportion satisfied";
xaxis label="Total Quality of Care Score";
RUN;
The general basic approach to differentiating bits of a graph based on the value of a variable is to use the Group=option. Which means this is where I would start:
PROC SGPLOT data=work.TM_MA_groupMean; vbarparm category=/*variable*/ total_qual_care_score response=mean / limitlower=lclm limitupper=uclm; label mean="Proportion satisfied" group= variable ; RUN;
However the size of your example data set is so small that the procedure complains that the data may not work for the VBARPARM.
LABEL your variable where you want the text instead of the name to appear.
If the question about your "binary" variable actually boils down to "How do I get a value of 1 to display "xxxx" and 0 to display "yyyy" text" then the question is usually a format.
You don't provide anywhere near enough detail to tell what sort of graph but here is a small example using the binary variable in two roles in a bar.
data example; set sashelp.class; /* create a binary 1/0 coded variable*/ bin= sex='F'; run; proc format library=work; value bin 0= 'The text for 0' 1= 'The text for 1' ; run; proc sgplot data=example; vbar age/ response=height group=bin; format bin bin. ; label bin='The text I want for a label'; run; proc sgplot data=example; vbar bin / response=height group=age; label bin='The text I want for a label'; format bin bin. ; run;
You need to change your data structure by PROC TRANSPOSE to get this kind of graph.
data have;
call streaminit(123);
do total_qual_care_score=0 to 8;
do id=1 to 100;
TM_non_ADRD_group=rand('bern',0.2);
MA_non_ADRD_group=rand('bern',0.8);
output;
end;
end;
run;
proc transpose data=have out=temp;
by total_qual_care_score id;
var TM_non_ADRD_group MA_non_ADRD_group;
run;
proc summary data=temp nway;
class total_qual_care_score _NAME_;
var col1;
output out=temp2 mean=mean lclm=lclm uclm=uclm;
run;
data want;
set temp2;
by total_qual_care_score ;
if first.total_qual_care_score then call missing(cum_mean,cum_lclm,cum_uclm);
cum_mean+mean;
_lclm=mean-lclm;cum_lclm=cum_mean-_lclm;
_uclm=uclm-mean;cum_uclm=cum_mean+_uclm;
run;
PROC SGPLOT data=want;
vbarparm category=total_qual_care_score response=mean/ group=_NAME_ name='x' ;
scatter x=total_qual_care_score y=cum_mean/markerattrs=(size=0) yerrorlower=cum_lclm yerrorupper=cum_uclm;
xaxis label='Total Quality of Care Score';
yaxis label='Proportion satisfied';
keylegend 'x'/title='';
RUN;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.