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

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

mounikag_0-1638955476162.png

 

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;

1 ACCEPTED SOLUTION

Accepted Solutions
GraphGuy
Meteorite | Level 14

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;

group_bar.png

View solution in original post

11 REPLIES 11
GraphGuy
Meteorite | Level 14

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;

group_bar.png

mounikag
Obsidian | Level 7

Thanks for your response

 

Ksharp
Super User
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;

Ksharp_0-1639050940616.png

 

GraphGuy
Meteorite | Level 14

LOL - We both 'cheated' in that neither of us used Proc SGplot! 😉

 

Ksharp
Super User
Agree ! 🙂
Anybody want try by PROC SGPLOT ?
Jay54
Meteorite | Level 14

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

mounikag
Obsidian | Level 7
Hi Ksharp,

It worked . Thanks alot for your help and response . I just want need one addition to the figure .. I want to display number of subjects on each bar at particular time point . I know how to display in sgplot by using datalabels but how do i need to use in sgpanel to genearte number of subjects on each bar ..
DanH_sas
SAS Super FREQ

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:

  1. Use the SEGLABEL option to display the value in each bar segment
  2. Pre-summarize the data and use VBARPARM instead of VBAR. Then, you can overlay a TEXT plot to put the values on the ends of the bars

If you need more information about the latter, let me know.

mounikag
Obsidian | Level 7
Thanks danh_s for quick turn around. Iam trying to use as suggested but is not displaying. here is my code .please let me know if iam doing wrong


proc sgpanel data=final dattrmap=attrmap ;
by rowlbl;
panelby tptn / layout=columnlattice
onepanel noborder
colheaderpos=bottom spacing=10 ;
format tptn tptf.;
vbarparm category=trtn response=percent/ group=grade displaybaseline=auto datalabel=nsubjs dataskin=pressed;;
/* vbar tptn / datalabel=nsubjs nofill;*/
format trtn trtf.;
colaxis display=(nolabel);
rowaxis values=(0 to 100 by 10) label='Percentage of Subjects with Reactions' ;
keylegend /border;
run;
DanH_sas
SAS Super FREQ

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;

mounikag
Obsidian | Level 7

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;

 

SG PAnel.PNG

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 1747 views
  • 6 likes
  • 5 in conversation