Hi all,
This is my first time posting to the forum but I have found it incredibly helpful in my journey of learning SAS so I thought I would try posting a question I have. I found one similar question in the forum from a few years ago but there was no solution in the thread. I am using SAS 9.4 and am trying to create a clustered bar chart (using sgplot) with a table of values directly below. Using xaxistable, I am able to get the desired result with the exception of one small formatting issue: gridlines. I am not looking to add gridlines to the graph, but to the table below the graph. I inserted a sample dataset and code for what I am doing as well as pictures of the resulting graph and desired formatting changes.
Thanks!
data sampledata;
input percent 4.1 gender $ group $;
datalines;
15.8 M 1
27.4 M 2
19.6 M 3
25.2 F 2
28.1 F 1
29.6 F 3
;
run;
proc sgplot data=sampledata;
vbar gender / group=group groupdisplay=cluster datalabel datalabelfitpolicy=none response=percent;
xaxis values=("M" "F") display=(nolabel)
valuesdisplay=("Male" "Female") fitpolicy=splitalways;
styleattrs datacolors=(CXFBFFB3 CX68CAB5 CX4376B3);
styleattrs datacontrastcolors=(Black);
xaxistable percent/ pad=15;
run;
You could use ODS layout and proc report to add the table.
Here is a quick and dirty solultion.
ods layout gridded;
ods region style={bordercolor=black borderstyle=solid borderwidth=.25pt};
ods graphics / noborder;
proc sgplot data=sampledata;
vbar gender / group=group groupdisplay=cluster datalabel datalabelfitpolicy=none response=percent;
xaxis values=("M" "F") display=(nolabel)
valuesdisplay=("Male" "Female") fitpolicy=splitalways;
styleattrs datacolors=(CXFBFFB3 CX68CAB5 CX4376B3);
styleattrs datacontrastcolors=(Black);
/* xaxistable percent/ pad=15; */
run;
proc report data=sampledata nowd;
columns group percent,gender;
define group / group style=[cellwidth=2cm just=c];
define gender / across '' order=data style=[cellwidth=7cm just=c] ;
define percent / analysis '' style=[cellwidth=7cm just=c];
run;
ods layout end;
I don't think there is an option to add a grid to an XAXISTABLE. I looked in the documentation for the XAXISTABLE statement and even looked at the AXISTABLE statement in the GTL. I do not see any options to draw a grid automatically.
Thank you for looking into this, Rick. It seems that adding gridlines to the XAXISTABLE statement may not be a feature that is currently available then.
Yes. That is my conclusion.
You could use ODS layout and proc report to add the table.
Here is a quick and dirty solultion.
ods layout gridded;
ods region style={bordercolor=black borderstyle=solid borderwidth=.25pt};
ods graphics / noborder;
proc sgplot data=sampledata;
vbar gender / group=group groupdisplay=cluster datalabel datalabelfitpolicy=none response=percent;
xaxis values=("M" "F") display=(nolabel)
valuesdisplay=("Male" "Female") fitpolicy=splitalways;
styleattrs datacolors=(CXFBFFB3 CX68CAB5 CX4376B3);
styleattrs datacontrastcolors=(Black);
/* xaxistable percent/ pad=15; */
run;
proc report data=sampledata nowd;
columns group percent,gender;
define group / group style=[cellwidth=2cm just=c];
define gender / across '' order=data style=[cellwidth=7cm just=c] ;
define percent / analysis '' style=[cellwidth=7cm just=c];
run;
ods layout end;
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.