Hi folks:
I have a couple of questions on the same plot if you don't mind.
1. How to assign colours to the paired bars by group variable (localized, regional and distant) but not within (blue vs red) as of now. It's confusing that two bars both belong to 'localized' takes two different colours: blue vs red. Instead, I'd like to, for example, assign green to both "localized" pair-bars but two varying in tone. Assign blue but two different blues for the 'regional' so forth so on.
2. How to widen the space between the pair-bars. I'd like to have more space between clusters while pair-bars stick together with no space between as it is now.
Thanks for your time indeed.
DATA PLOT1;
INPUT METHOD AGEGRP STAGE1 SURV;
DATALINES;
1 1 2 99
1 1 3 100
1 1 4 89
1 2 2 96
1 2 3 96
1 2 4 81
1 3 2 94
1 3 3 93
1 3 4 78
1 4 2 89
1 4 3 89
1 4 4 77
2 1 2 95
2 1 3 92
2 1 4 65
2 2 2 87
2 2 3 85
2 2 4 60
2 3 2 80
2 3 3 79
2 3 4 54
2 4 2 63
2 4 3 67
2 4 4 41
;
PROC FORMAT;
VALUE STAGE
2='Localized'
3='Regional'
4='Distant'
;
VALUE AGEGRP
1='00-44yr old'
2='45-59yr old'
3='60-74yr old'
4='75+yr old'
;
RUN;
ods graphics ON;
ods graphics/width=6in height=4in;
proc sgpanel data=plot1;
panelby agegrp /novarname colheaderpos=bottom COLUMNS=4 onepanel;
styleattrs DATACONTRASTCOLORS=(BLUE green red);
vbar stage1 / response=surv stat=sum group=method nostatlabel
groupdisplay=cluster;
keylegend "Empirical" "Midpoint" / across=1 position=Top;
format agegrp agegrp. stage1 stage.;
run;
1. One work-around I could think of is using the COLORRESPONSE option to color the different combinations of STAGE1 and METHOD.
data plot2;
set plot1;
colorvar=5*stage1+2*method;
run;
ods graphics ON;
ods graphics/width=6in height=4in;
proc sgpanel data=plot2;
panelby agegrp / novarname colheaderpos=bottom COLUMNS=4 onepanel;
vbar stage1 / response=surv stat=sum nostatlabel group=method
groupdisplay=cluster barwidth=0.7
colorresponse=colorvar colormodel=(lightcoral lightgreen darkblue);
keylegend "Empirical" "Midpoint" / across=1 position=Top;
format agegrp agegrp. stage1 stage.;
run;
What's tricky is picking colors and a combination function (5*STAGE1+2*METHOD) that allow for distinction in the colors.
2. The option for spacing between clusters is called BARWIDTH. Here, BARWIDTH=0.7.
Hope this helps!
1. One work-around I could think of is using the COLORRESPONSE option to color the different combinations of STAGE1 and METHOD.
data plot2;
set plot1;
colorvar=5*stage1+2*method;
run;
ods graphics ON;
ods graphics/width=6in height=4in;
proc sgpanel data=plot2;
panelby agegrp / novarname colheaderpos=bottom COLUMNS=4 onepanel;
vbar stage1 / response=surv stat=sum nostatlabel group=method
groupdisplay=cluster barwidth=0.7
colorresponse=colorvar colormodel=(lightcoral lightgreen darkblue);
keylegend "Empirical" "Midpoint" / across=1 position=Top;
format agegrp agegrp. stage1 stage.;
run;
What's tricky is picking colors and a combination function (5*STAGE1+2*METHOD) that allow for distinction in the colors.
2. The option for spacing between clusters is called BARWIDTH. Here, BARWIDTH=0.7.
Hope this helps!
You need to change data sometime .
DATA PLOT1;
INPUT METHOD AGEGRP STAGE1 SURV;
DATALINES;
1 1 2 99
1 1 3 100
1 1 4 89
1 2 2 96
1 2 3 96
1 2 4 81
1 3 2 94
1 3 3 93
1 3 4 78
1 4 2 89
1 4 3 89
1 4 4 77
2 1 2 95
2 1 3 92
2 1 4 65
2 2 2 87
2 2 3 85
2 2 4 60
2 3 2 80
2 3 3 79
2 3 4 54
2 4 2 63
2 4 3 67
2 4 4 41
;
proc summary data=plot1 nway;
class agegrp stage1 method;
var surv;
output out=have(drop=_:) sum=;
run;
proc transpose data=have out=want prefix=_;
by agegrp stage1;
id method;
var surv;
run;
PROC FORMAT;
VALUE STAGE
2='Localized'
3='Regional'
4='Distant';
VALUE AGEGRP
1='00-44yr old'
2='45-59yr old'
3='60-74yr old'
4='75+yr old';
RUN;
ods graphics ON;
ods graphics/width=6in height=4in;
proc sgpanel data=want;
styleattrs datacolors=(blue green red) datacontrastcolors=(blue green red) ;
panelby agegrp /novarname colheaderpos=bottom COLUMNS=4 onepanel;
vbar stage1 /group=stage1 response=_1 nostatlabel discreteoffset=-0.1 barwidth=0.2;
vbar stage1 /group=stage1 response=_2 nostatlabel discreteoffset=0.1 barwidth=0.2;
keylegend "Empirical" "Midpoint" / across=1 position=Top;
format agegrp agegrp. stage1 stage.;
run;
Thanks, both.
I used Unison's approach by modifying my data a tiny bit. Combination of barwidth=1 CLUSTERWIDTH=0.7
did the trick. I ran out of time to figure out the legends for the bars. Then I ended up cheating the texts on the bar using the Microsoft Paint program. Please let me know if anyone knows the proper way of doing it using SAS.
Thanks again!
Final code is below in case if it helps anyone in the future.
DATA PLOT1;
INPUT METHOD AGEGRP STAGE1 SURV;
DATALINES;
1 1 2 95
1 1 3 92
1 1 4 65
1 2 2 87
1 2 3 85
1 2 4 60
1 3 2 80
1 3 3 79
1 3 4 54
1 4 2 63
1 4 3 67
1 4 4 41
2 1 2 99
2 1 3 100
2 1 4 89
2 2 2 96
2 2 3 96
2 2 4 81
2 3 2 94
2 3 3 93
2 3 4 78
2 4 2 89
2 4 3 89
2 4 4 77
;
PROC FORMAT;
VALUE STAGE
2='Localized'
3='Regional'
4='Distant';
VALUE AGEGRP
1='00-44yr old'
2='45-59yr old'
3='60-74yr old'
4='75+yr old';
RUN;
data plot2; set plot1;
colorvar=5*stage1+2*method;
run;
ods graphics ON;
ods graphics/width=12in height=4in;
proc sgpanel data=plot2 noautolegend;
panelby agegrp / novarname colheaderpos=bottom COLUMNS=4 onepanel;
hbar stage1 / response=surv stat=sum nostatlabel group=method
groupdisplay=cluster barwidth=1 CLUSTERWIDTH=0.7 datalabel dataskin=matte
colorresponse=colorvar colormodel=(lightcoral lightgreen blue);
rowaxis label=' ';
colaxis label='Agreement rate within 1 month (%)' grid;
format agegrp agegrp. stage1 stage.;
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.