I have a feeling that this code could be greatly streamlined, but I think this works, following the ideas in the papers I mentioned.
data have;
infile datalines;
input period $10. drug $15. visit dosis 2.;
datalines;
2000-2005 Cefalexin 1 12
2000-2005 Calcipotriol 1 16
2000-2005 Folic acid 1 26
2000-2005 Piriton 1 3
2000-2005 Fentanyl 1 36
2000-2005 Folic acid 1 26
2000-2005 Co-beneldopa 1 35
2000-2005 Folic acid 1 26
2000-2005 Cefalexin 1 11
2000-2005 Fentanyl 1 35
2006-2011 Allopurinol 1 33
2006-2011 Folic acid 1 27
2006-2011 Cefalexin 1 11
2006-2011 Allopurinol 1 33
2006-2011 Cefalexin 1 11
2006-2011 Folic acid 1 26
2006-2011 Piriton 1 3
2006-2011 Folic acid 1 27
2006-2011 Allopurinol 1 33
2006-2011 Piriton 1 3
2006-2011 Cefalexin 1 11
2011-2022 Piriton 1 3
2011-2022 Cefalexin 1 11
2011-2022 Cefalexin 1 12
2011-2022 Cefalexin 1 11
2011-2022 Cefalexin 1 11
2011-2022 Baclofen 1 35
2011-2022 Diclofenac 1 22
2011-2022 Diclofenac 1 22
2011-2022 Co-beneldopa 1 35
2011-2022 Co-beneldopa 1 3
2011-2022 Other Piriton 1 22
2000-2005 Co-beneldopa 2 3
2000-2005 Folic acid 2 15
2000-2005 Cefalexin 2 14
2000-2005 Fentanyl 2 11
2000-2005 Allopurinol 2 3
2000-2005 Folic acid 2 3
2000-2005 Cefalexin 2 5
2000-2005 Allopurinol 2 15
2000-2005 Cefalexin 2 35
2006-2011 Folic acid 2 16
2006-2011 Piriton 2 22
2006-2011 Folic acid 2 38
2006-2011 Allopurinol 2 11
2006-2011 Piriton 2 7
2006-2011 Cefalexin 2 11
2006-2011 Piriton 2 11
2006-2011 Cefalexin 2 15
2006-2011 Cefalexin 2 17
2006-2011 Cefalexin 2 11
2011-2022 Cefalexin 2 33
2011-2022 Baclofen 2 77
2011-2022 Diclofenac 2 25
2011-2022 Acrivastine 2 12
2011-2022 Acrivastine 2 8
2011-2022 Acrivastine 2 15
2011-2022 Acrivastine 2 20
2011-2022 Acrivastine 2 6
2011-2022 Acrivastine 2 20
2011-2022 Acrivastine 2 33
2011-2022 Other Piriton 2 14
2000-2005 Cefalexin 3 14
2000-2005 Calcipotriol 3 18
2000-2005 Folic acid 3 16
2000-2005 Piriton 3 15
2000-2005 Fentanyl 3 19
2000-2005 Folic acid 3 20
2000-2005 Co-beneldopa 3 25
2000-2005 Folic acid 3 52
2000-2005 Cefalexin 3 25
2000-2005 Fentanyl 3 16
2006-2011 Allopurinol 3 8
2006-2011 Folic acid 3 2
2006-2011 Cefalexin 3 55
2006-2011 Allopurinol 3 70
2006-2011 Cefalexin 3 18
2006-2011 Folic acid 3 1
2006-2011 Piriton 3 20
2006-2011 Folic acid 3 11
2006-2011 Allopurinol 3 14
2006-2011 Piriton 3 7
2006-2011 Cefalexin 3 33
2011-2022 Piriton 3 20
2011-2022 Cefalexin 3 15
2011-2022 Cefalexin 3 12
2011-2022 Cefalexin 3 77
2011-2022 Cefalexin 3 6
2011-2022 Acrivastine 3 44
2011-2022 Acrivastine 3 7
2011-2022 Acrivastine 3 25
2011-2022 Acrivastine 3 11
2011-2022 Acrivastine 3 9
2011-2022 Other Piriton 3 8
;
run;
proc sql;
create table have2 as select period, drug, visit, sum(dosis) as SumDosis from have group by period, drug, visit;
create table sumtotal as select period,visit, sum(SumDosis) as totals from have2 group by period, visit;
create table have3 as select a.*, b.totals, SumDosis/totals as percentage format percent8.2 from have2 as
a, sumtotal as b where a.visit=b.visit and a.period=b.period;
quit;
proc sql;
create table distinct_drugs as select distinct drug from have3;
create table distinct_visit as select distinct visit from have3;
create table distinct_period as select distinct period from have3;
create table all_distinct as select * from distinct_drugs,distinct_visit,distinct_period;
quit;
data have4;
set all_distinct have3;
run;
data have5;
set have4;
length N_percentage $15 ;
N_Percentage=catx(" ", SumDosis, "(", put(percentage, percent8.2), ")");
if drug ="Acrivastine" then order=1; if drug ="Allopurinol" then order=2; if drug ="Baclofen" then order=3;
if drug ="Calcipotriol" then order=4; if drug ="Cefalexin" then order=5; if drug ="Co-beneldopa" then order=6;
if drug ="Diclofenac" then order=7; if drug ="Fentanyl" then order=8; if drug ="Folic acid" then order=9;
if drug ="Piriton" then order=10; if drug ="Other Piriton" then order=11;
if sumdosis=. then sumdosis=0;
keep period drug visit SumDosis N_percentage order;
run;
proc summary data=have5 nway;
class period visit drug;
var sumdosis;
id order;
output out=have6 sum=;
run;
proc sort data=have6;
by period visit order;
run;
proc sgpanel data = have6 noautolegend;
styleattrs datacolors=(lightgreen lightred lightblue);
panelby period /columns=3 novarname noborder;
vbarparm category=visit response=SumDosis / group=drug grouporder=data seglabel groupdisplay=stack ;
rowaxis values=(0 to 300 by 50);
keylegend / position=bottom across=6 title=" ";
;run;
The method described in the blog post Feb 02, 2016 allows you to define group values that may not be in the data, but need to be included in the legend. Note, this same method also controls the order in which the values are displayed in the legend as described below:
https://blogs.sas.com/content/graphicallyspeaking/2017/11/01/legend-order-redux/
I hope you can use this method to get the legend entries in the custom order you need.
@Jay54 Very easy and efficient method, thankyou
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.