BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ran_
Fluorite | Level 6

Screenshot 2024-10-22 104908.png

I want to generate a plot with SAS, like the upper picture. Any ideas or help are appreciated.

  • Panel by aeterm to show separate panels for each adverse event.
  • Cluster the bars by treatment.
  • Use aesev to differentiate colors within each cluster.

data mydata;
input aeterm $ treatment $  aesev $ percentage;
datalines;
ABDOMINAL TRT01  MILD 3
ABDOMINAL TRT01 MODERATE 1
ABDOMINAL TRT02  SEVERE 8
BRONCHITIS TRT01  SEVERE 22
BRONCHITIS TRT02  MILD 11
BRONCHITIS TRT01  MILD 25

COUGH TRT01 MILD 9

COUGH TRT02 MILD 23
;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

It looks like not really easy to get this kind of graph.

 

 

data mydata;
input aeterm $ treatment $  aesev $ percentage;
datalines;
ABDOMINAL TRT01  MILD 0
ABDOMINAL TRT01 MODERATE 18
ABDOMINAL TRT01 SEVERE 60
ABDOMINAL TRT02  MILD 30
ABDOMINAL TRT02 MODERATE 60
ABDOMINAL TRT02  SEVERE 28
BRONCHITIS TRT01  MILD 41
BRONCHITIS TRT01  MODERATE 10
BRONCHITIS TRT01  SEVERE 0
BRONCHITIS TRT02  MILD 4
BRONCHITIS TRT02  MODERATE 30
BRONCHITIS TRT02  SEVERE 30
COUGH TRT01  MILD 40
COUGH TRT01  MODERATE 10
COUGH TRT01  SEVERE 30
COUGH TRT02  MILD 2
COUGH TRT02  MODERATE 4
COUGH TRT02  SEVERE 3
;
run;

proc sort data=mydata;by aeterm aesev;run;
data have;
merge mydata(where=(treatment='TRT01') rename=(percentage=pct_TRT01))
      mydata(where=(treatment='TRT02') rename=(percentage=pct_TRT02));
by aeterm aesev;
drop treatment;
run;



proc sgplot data=have noborder ;
styleattrs datacolors=(CXFEE0D2 CXFC9272 CXDE2D26 CXDEEBF7 CX9ECAE1 CX3182BD)
           datacontrastcolors=(CXFEE0D2 CXFC9272 CXDE2D26 CXDEEBF7 CX9ECAE1 CX3182BD);
vbarparm category=aeterm response=pct_TRT01/group=aesev seglabel seglabelfitpolicy=noclip barwidth=0.4 discreteoffset=-0.22 name='a' ;
vbarparm category=aeterm response=pct_TRT02/group=aesev seglabel seglabelfitpolicy=noclip barwidth=0.4 discreteoffset= 0.22 name='b' ;
xaxis tickstyle=inbetween display=(nolabel);
yaxis label='Percent';
keylegend 'a' /title='treatment1' noborder position=ne  ;
keylegend 'b' /title='treatment2' noborder position=ne ;

run;

Ksharp_0-1729651016227.png

 

 

 

View solution in original post

5 REPLIES 5
Ran_
Fluorite | Level 6

Screenshot 2024-10-22 104908.png

I want to generate a plot with SAS, like the upper picture. Any ideas or help are appreciated.

  • Panel by aeterm to show separate panels for each adverse event.
  • Cluster the bars by treatment.
  • Use aesev to differentiate colors within each cluster.

data mydata;
input aeterm $ treatment $  aesev $ percentage;
datalines;
ABDOMINAL TRT01  MILD 3
ABDOMINAL TRT01 MODERATE 1
ABDOMINAL TRT02  SEVERE 8
BRONCHITIS TRT01  SEVERE 22
BRONCHITIS TRT02  MILD 11
BRONCHITIS TRT01  MILD 25

COUGH TRT01 MILD 9

COUGH TRT02 MILD 23
;
run;

DanH_sas
SAS Super FREQ

Something like the following should work for you. By splitting the treatment into two bar charts, it gives you the opportunity to assign two different sets of colors to the severity variable:

Screenshot 2024-10-22 at 10.05.15 PM.png

data mydata;
length aeterm $ 10;
input aeterm $ treatment $  aesev $ percentage;
treatment1 = ifc(treatment ="TRT01", "TRT01", "     ");
treatment2 = ifc(treatment ="TRT02", "TRT02", "     ");
aesev2 = aesev;
datalines;
ABDOMINAL TRT01  MILD 3
ABDOMINAL TRT01 MODERATE 1
ABDOMINAL TRT02  SEVERE 8
BRONCHITIS TRT01  SEVERE 22
BRONCHITIS TRT02  MILD 11
BRONCHITIS TRT01  MILD 25
COUGH TRT01 MILD 9
COUGH TRT02 MILD 23
;
run;

data attrmap;
    length Value $ 8;
    input ID $ Value $ FillColor $ LineColor $;
    cards;
    t1 MILD     cxf8d4c1 cxe97132
    t1 MODERATE cxf2aa84 cxe97132
    t1 SEVERE   cxe97132 cxe97132
    t2 MILD     cxb5c5d9 cx143f82
    t2 MODERATE cx6c8cb4 cx143f82
    t2 SEVERE   cx143f82 cx143f82
    ;
run;

proc sgpanel data=mydata dattrmap=attrmap;
    panelby aeterm / layout=columnlattice noheaderborder noborder colheaderpos=bottom novarname;
    vbarparm category=treatment1 response=percentage / group=aesev seglabel seglabelattrs=(color=white sizze=8) attrid=t1 name="t1";
    vbarparm category=treatment2 response=percentage / group=aesev2 seglabel seglabelattrs=(color=white size=8) attrid=t2 name="t2";
    colaxis display=none;
    keylegend "t1" / title="Treatment 1";
    keylegend "t2" / title="Treatment 2";
run;
Ran_
Fluorite | Level 6
I appreciate for your help.
Ksharp
Super User

It looks like not really easy to get this kind of graph.

 

 

data mydata;
input aeterm $ treatment $  aesev $ percentage;
datalines;
ABDOMINAL TRT01  MILD 0
ABDOMINAL TRT01 MODERATE 18
ABDOMINAL TRT01 SEVERE 60
ABDOMINAL TRT02  MILD 30
ABDOMINAL TRT02 MODERATE 60
ABDOMINAL TRT02  SEVERE 28
BRONCHITIS TRT01  MILD 41
BRONCHITIS TRT01  MODERATE 10
BRONCHITIS TRT01  SEVERE 0
BRONCHITIS TRT02  MILD 4
BRONCHITIS TRT02  MODERATE 30
BRONCHITIS TRT02  SEVERE 30
COUGH TRT01  MILD 40
COUGH TRT01  MODERATE 10
COUGH TRT01  SEVERE 30
COUGH TRT02  MILD 2
COUGH TRT02  MODERATE 4
COUGH TRT02  SEVERE 3
;
run;

proc sort data=mydata;by aeterm aesev;run;
data have;
merge mydata(where=(treatment='TRT01') rename=(percentage=pct_TRT01))
      mydata(where=(treatment='TRT02') rename=(percentage=pct_TRT02));
by aeterm aesev;
drop treatment;
run;



proc sgplot data=have noborder ;
styleattrs datacolors=(CXFEE0D2 CXFC9272 CXDE2D26 CXDEEBF7 CX9ECAE1 CX3182BD)
           datacontrastcolors=(CXFEE0D2 CXFC9272 CXDE2D26 CXDEEBF7 CX9ECAE1 CX3182BD);
vbarparm category=aeterm response=pct_TRT01/group=aesev seglabel seglabelfitpolicy=noclip barwidth=0.4 discreteoffset=-0.22 name='a' ;
vbarparm category=aeterm response=pct_TRT02/group=aesev seglabel seglabelfitpolicy=noclip barwidth=0.4 discreteoffset= 0.22 name='b' ;
xaxis tickstyle=inbetween display=(nolabel);
yaxis label='Percent';
keylegend 'a' /title='treatment1' noborder position=ne  ;
keylegend 'b' /title='treatment2' noborder position=ne ;

run;

Ksharp_0-1729651016227.png

 

 

 

Ran_
Fluorite | Level 6
I really appreciate your help. I have been struggling with this for two days

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1177 views
  • 6 likes
  • 3 in conversation