Hi All,
Here is my data I need to represent the below data into the figure:
Data:X
Row trt01 timepoint grade percent
Pain a day1,30 min mild 33.3
Pain a day1 moderate 40.2
Pain b day1,30 min moderate 24.0
Pain b day1 mild 35.0
Pain c day1,30 min moderate 44.0
Pain c day1 mild 35.0
On the x-axis I need to display three treatment groups(trt01a) with severity grade which need to be a clustered or adjacent group bar on each timepoint. Each treatment should display there severity grade. On the x-axis I want to display(key legend) grade as below figure along with I want to display treatment group with varies color option
Here is the figure which I need : Iam using SG plot procedure but somehow I was not able to generate grouped bars with treatment groups at each time point. Do I need to use any other proc procedure. Could you please help me in providing the code with an example
on X-axis key legend should be :
Pain: Grade Mild moderate Severe
Trt : a b c
I am using below SG plot Procedure to output the figure and here is the code:
proc sgplot data=x dattrmap=attrmap;
by trtn rowlbl;
vbar tptn / response=percent group=grade name="Param" barwidth=0.5 attrid=rowlbl;
vbar tptn / response=percent group=trt groupdisplay=cluster name="Param" barwidth=0.5 attrid=rowlbl;
xaxis label='Timepoints' valueattrs=(size=8) values=(0 to valuesdisplay=('Day 1 30 Min' 'Day 1 PM' 'Day 2' 'Day 3' 'Day 4' 'Day 5' 'Day 6' 'Day 7' ); *valuesformat=atpt.;* fitpolicy=rotatethin;
yaxis values=(0 to 100 by 10) label='Percentage of Subjects with Reactions' ;
keylegend "Param"/ title=&title. location=outside position=bottom across=9 sortorder=ascending;
run;
Here's one way to do it with SAS/Graph Proc Gchart, with a partial dataset I created (I'll leave it to the ODS Graphics experts to show how to do it with Proc SGplot) 🙂
data x;
infile datalines pad truncover dlm=':';
length trt01 $3 timepoint $15 grade $10;
input trt01 timepoint grade percent;
datalines;
a:day 1, 30 min:mild:34
a:day 1, 30 min:moderate:2
a:day 1, 30 min:severe:0
b:day 1, 30 min:mild:64
b:day 1, 30 min:moderate:0
b:day 1, 30 min:severe:0
c:day 1, 30 min:mild:15.0
c:day 1, 30 min:moderate:0
c:day 1, 30 min:severe:0
a:day 1, PM:mild:62
a:day 1, PM:moderate:14
a:day 1, PM:severe:0
b:day 1, PM:mild:45
b:day 1, PM:moderate:37
b:day 1, PM:severe:0
c:day 1, PM:mild:15.0
c:day 1, PM:moderate:5
c:day 1, PM:severe:0
a:day 2:mild:78
a:day 2:moderate:11
a:day 2:severe:4
b:day 2:mild:27
b:day 2:moderate:30
b:day 2:severe:0
c:day 2:mild:15.0
c:day 2:moderate:5
c:day 2:severe:0
run;
axis1 minor=none label=(angle=90 'Percentage of Subjects with Reactions');
axis2 label=none value=none;
axis3 label=none;
legend1 label=("GRADE") shape=bar(.15in,.15in) frame;
proc gchart data=x;
vbar trt01 / sumvar=percent subgroup=grade space=0
group=timepoint maxis=axis2 gaxis=axis3 legend=legend1
raxis=axis1;
run;
Here's one way to do it with SAS/Graph Proc Gchart, with a partial dataset I created (I'll leave it to the ODS Graphics experts to show how to do it with Proc SGplot) 🙂
data x;
infile datalines pad truncover dlm=':';
length trt01 $3 timepoint $15 grade $10;
input trt01 timepoint grade percent;
datalines;
a:day 1, 30 min:mild:34
a:day 1, 30 min:moderate:2
a:day 1, 30 min:severe:0
b:day 1, 30 min:mild:64
b:day 1, 30 min:moderate:0
b:day 1, 30 min:severe:0
c:day 1, 30 min:mild:15.0
c:day 1, 30 min:moderate:0
c:day 1, 30 min:severe:0
a:day 1, PM:mild:62
a:day 1, PM:moderate:14
a:day 1, PM:severe:0
b:day 1, PM:mild:45
b:day 1, PM:moderate:37
b:day 1, PM:severe:0
c:day 1, PM:mild:15.0
c:day 1, PM:moderate:5
c:day 1, PM:severe:0
a:day 2:mild:78
a:day 2:moderate:11
a:day 2:severe:4
b:day 2:mild:27
b:day 2:moderate:30
b:day 2:severe:0
c:day 2:mild:15.0
c:day 2:moderate:5
c:day 2:severe:0
run;
axis1 minor=none label=(angle=90 'Percentage of Subjects with Reactions');
axis2 label=none value=none;
axis3 label=none;
legend1 label=("GRADE") shape=bar(.15in,.15in) frame;
proc gchart data=x;
vbar trt01 / sumvar=percent subgroup=grade space=0
group=timepoint maxis=axis2 gaxis=axis3 legend=legend1
raxis=axis1;
run;
Thanks for your response
data x;
infile datalines pad truncover dlm=':';
length trt01 $3 timepoint $15 grade $10;
input trt01 timepoint grade percent;
datalines;
a:day 1, 30 min:mild:34
a:day 1, 30 min:moderate:2
a:day 1, 30 min:severe:0
b:day 1, 30 min:mild:64
b:day 1, 30 min:moderate:0
b:day 1, 30 min:severe:0
c:day 1, 30 min:mild:15.0
c:day 1, 30 min:moderate:0
c:day 1, 30 min:severe:0
a:day 1, PM:mild:62
a:day 1, PM:moderate:14
a:day 1, PM:severe:0
b:day 1, PM:mild:45
b:day 1, PM:moderate:37
b:day 1, PM:severe:0
c:day 1, PM:mild:15.0
c:day 1, PM:moderate:5
c:day 1, PM:severe:0
a:day 2:mild:78
a:day 2:moderate:11
a:day 2:severe:4
b:day 2:mild:27
b:day 2:moderate:30
b:day 2:severe:0
c:day 2:mild:15.0
c:day 2:moderate:5
c:day 2:severe:0
;
proc sgpanel data=x ;
panelby timepoint/onepanel layout=COLUMNLATTICE novarname
NOBORDER COLHEADERPOS=bottom HEADERBACKCOLOR=white NOHEADERBORDER;
vbar trt01/response=percent group=grade barwidth=1 nostatlabel;
colaxis display=none;
rowaxis values=(0 to 100 by 10) label='Percentage of Subjects with Reactions';
run;
LOL - We both 'cheated' in that neither of us used Proc SGplot! 😉
SGPANEL is the right way to create this graph with cluster groups and stacked groups among the ODS graphics tools. SGPLOT does not handle this case easily. One could try to force using SGPLOT using multiple VBAR statements with DISCRETEOFFSETs, but it easier with SGPANEL (or GCHART).
SGPANEL supports the DATALABEL option like SGPLOT; but, in this case, that option will not work, as the grouped bars are "stacked" (as opposed to "clustered". You have a couple of options:
If you need more information about the latter, let me know.
DATALABEL will still not work due to the stacked bars. That is why you will need to add the TEXT plot. You code will look something like the following:
vbarparm category=trtn response=percent/ group=grade displaybaseline=auto dataskin=pressed;
text x=trtn y=percent text=nsubjs / position=top pad=(bottom=5px) contributeoffsets=none;
Thank U so much it worked . But i need some cosmetic change where I need a space between each treatment within a panel. Here is my code iam using:
proc sgpanel data=final dattrmap=attrmap ;
by rowlbl;
panelby tptn / layout=columnlattice
onepanel noborder novarname
colheaderpos=bottom spacing=10 proportional;
format tptn tptf.;
vbarparm category=trtn response=percent/ group=grade datalabel=nsubjs groupdisplay=cluster
barwidth=1.0 DATALABELFITPOLICY=NONE DATALABELATTRS=(size=9);
format trtn trtf.;
colaxis display=(nolabel);;
rowaxis values=(0 to 100 by 10) label='Percentage of Subjects with Reactions' ;
keylegend /exclude = ("PERCENT") border;
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.