<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Sortorder in Keylegend in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861711#M340367</link>
    <description>&lt;P&gt;I provided an example where I assigned a custom order variable to each record (based upon the variable ORIGIN in SASHELP.CARS) I don't see where you have attempted to order the categories of your variables in your data, as I have done in my code. Please give that a try.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also:&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/168930"&gt;@Anita_n&lt;/a&gt; you have been in this forum for a long time now. Please provide example data in your original question from now on. If you had, then you may have gotten a quicker and better answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You stated you were using PROC SGPLOT, but you are not. Please provide correct information.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 01 Mar 2023 11:25:06 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2023-03-01T11:25:06Z</dc:date>
    <item>
      <title>Sortorder in Keylegend</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861407#M340273</link>
      <description>&lt;P&gt;Is it possible to sort the entries in my key directory in a way I personally define, instead of using ascending, descending or autoreverse as stated in the documentation?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 14:45:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861407#M340273</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2023-02-28T14:45:55Z</dc:date>
    </item>
    <item>
      <title>Re: Sortorder in Keylegend</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861412#M340275</link>
      <description>&lt;P&gt;Are you talking about PROC SGPLOT, or something else?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 14:54:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861412#M340275</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-02-28T14:54:22Z</dc:date>
    </item>
    <item>
      <title>Re: Sortorder in Keylegend</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861417#M340279</link>
      <description>&lt;P&gt;Yes please&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 15:01:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861417#M340279</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2023-02-28T15:01:19Z</dc:date>
    </item>
    <item>
      <title>Re: Sortorder in Keylegend</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861455#M340294</link>
      <description>&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cars;
    set sashelp.cars;
    if origin='Asia' then order=2;
    else if origin='USA' then order=3;
    else if origin='Europe' then order=1;
run;
proc sort data=cars;
    by order;
run;
proc sgplot data=cars;
    scatter x=mpg_city y=mpg_highway/group=origin grouporder=data;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Feb 2023 15:52:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861455#M340294</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-02-28T15:52:23Z</dc:date>
    </item>
    <item>
      <title>Re: Sortorder in Keylegend</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861489#M340304</link>
      <description>&lt;P&gt;I will try that and let you know if it worked. Thanks a lot&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 16:22:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861489#M340304</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2023-02-28T16:22:58Z</dc:date>
    </item>
    <item>
      <title>Re: Sortorder in Keylegend</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861670#M340356</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp; sorry it's not working the way I want it to.&lt;/P&gt;
&lt;P&gt;using this sample data. I want the "other Piriton" to be the last element.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;

data have4;
set have3;
length N_percentage $15 ;
N_Percentage=catx(" ", SumDosis, "(", put(percentage, percent8.2), ")");
keep period drug visit SumDosis N_percentage;
run;
proc sort data have4;
by period visit drug;
run;

proc sgpanel data = have4 noautolegend;
styleattrs datacolors=(lightgreen lightred lightblue);
panelby period /columns=3 novarname noborder;
 vbarparm  category=visit response=SumDosis / group=drug grouporder=data  datalabel  groupdisplay=stack name='a';
 keylegend 'a'/ position=bottom across=6 title=" ";
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Mar 2023 10:38:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861670#M340356</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2023-03-01T10:38:23Z</dc:date>
    </item>
    <item>
      <title>Re: Sortorder in Keylegend</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861711#M340367</link>
      <description>&lt;P&gt;I provided an example where I assigned a custom order variable to each record (based upon the variable ORIGIN in SASHELP.CARS) I don't see where you have attempted to order the categories of your variables in your data, as I have done in my code. Please give that a try.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also:&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/168930"&gt;@Anita_n&lt;/a&gt; you have been in this forum for a long time now. Please provide example data in your original question from now on. If you had, then you may have gotten a quicker and better answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You stated you were using PROC SGPLOT, but you are not. Please provide correct information.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2023 11:25:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861711#M340367</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-03-01T11:25:06Z</dc:date>
    </item>
    <item>
      <title>Re: Sortorder in Keylegend</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861714#M340369</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;sorry, it was sgplot, I later changed it to sgpanel inorder to use the text statement.&amp;nbsp; I have updated the code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have4;
set have3;
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;

keep period drug visit SumDosis N_percentage order;
run;&lt;BR /&gt;&lt;BR /&gt;proc sort data= have4;&lt;BR /&gt;by period visit order;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sgpanel data = have4 noautolegend;&lt;BR /&gt;styleattrs datacolors=(lightgreen lightred lightblue);&lt;BR /&gt;panelby period /columns=3 novarname noborder;&lt;BR /&gt;vbarparm category=visit response=SumDosis / group=drug grouporder=data seglabel groupdisplay=stack ;&lt;BR /&gt;rowaxis values=(0 to 300 by 50);&lt;BR /&gt;keylegend / position=bottom across=6 title=" ";&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Mar 2023 12:09:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861714#M340369</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2023-03-01T12:09:22Z</dc:date>
    </item>
    <item>
      <title>Re: Sortorder in Keylegend</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861717#M340371</link>
      <description>&lt;P&gt;Change your PROC SORT&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data= have4;
    by period order;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to sort by Period because of the PANELBY PERIOD command and you need to sort by order to get the drugs in the desired order.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2023 12:14:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861717#M340371</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-03-01T12:14:54Z</dc:date>
    </item>
    <item>
      <title>Re: Sortorder in Keylegend</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861720#M340373</link>
      <description>&lt;P&gt;I already did that (see above): I need to sort these three variables&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data= have4;
by period visit order;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Mar 2023 12:20:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861720#M340373</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2023-03-01T12:20:50Z</dc:date>
    </item>
    <item>
      <title>Re: Sortorder in Keylegend</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861723#M340375</link>
      <description>&lt;P&gt;That's not the code I used. I don't see why you need to include VISIT in the sort.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2023 12:26:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861723#M340375</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-03-01T12:26:10Z</dc:date>
    </item>
    <item>
      <title>Re: Sortorder in Keylegend</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861770#M340387</link>
      <description>&lt;P&gt;I need to sort that because the sortorder changes when I plot the graph&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2023 16:14:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861770#M340387</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2023-03-01T16:14:26Z</dc:date>
    </item>
    <item>
      <title>Re: Sortorder in Keylegend</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861772#M340388</link>
      <description>&lt;P&gt;Did you try it without VISIT in PROC SORT, as in my code? If so, what is wrong with the plot that results?&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2023 16:19:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861772#M340388</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-03-01T16:19:39Z</dc:date>
    </item>
    <item>
      <title>Re: Sortorder in Keylegend</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861775#M340391</link>
      <description>&lt;P&gt;This is what happens if I don't sort the visits. It starts with the first the the fifth&amp;nbsp; and then the fourth for example. In the real data I have visits 1 to 12. I did not list all here because this will be a lot of work. But it's okay, I will try other means. Thankyou&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2023 16:32:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861775#M340391</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2023-03-01T16:32:11Z</dc:date>
    </item>
    <item>
      <title>Re: Sortorder in Keylegend</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861788#M340399</link>
      <description>&lt;P&gt;Okay, we have gone down the path that produces correct plots in PROC SGPLOT, but apparently not in PROC SGPANEL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you read these posts, the method ought to work in PROC SGPANEL&lt;/P&gt;
&lt;P&gt;&lt;A href="http://saslist.com/blog/2016/02/17/a-simple-trick-to-include-and-order-all-categories-in-sgplot-legends/\" target="_blank"&gt;http://saslist.com/blog/2016/02/17/a-simple-trick-to-include-and-order-all-categories-in-sgplot-legends/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/iml/2016/02/17/include-and-order-categories-in-legends.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2016/02/17/include-and-order-categories-in-legends.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;According to these posts, you prepend all missing categories, so you have all possible categories for each visit and period. Then sort by the desired order, and then you should get what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I call on&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13856"&gt;@Jay54&lt;/a&gt;&amp;nbsp;to advise if this method will work for PROC SGPANEL with the VBARPARM statement.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2023 17:17:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861788#M340399</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-03-01T17:17:33Z</dc:date>
    </item>
    <item>
      <title>Re: Sortorder in Keylegend</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861792#M340400</link>
      <description>&lt;P&gt;I have a feeling that this code could be greatly streamlined, but I think this works, following the ideas in the papers I mentioned.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Mar 2023 17:41:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861792#M340400</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-03-01T17:41:11Z</dc:date>
    </item>
    <item>
      <title>Re: Sortorder in Keylegend</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861837#M340404</link>
      <description>&lt;P&gt;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.&amp;nbsp; Note, this same method also controls the order in which the values are displayed in the legend as described below:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/graphicallyspeaking/2017/11/01/legend-order-redux/" target="_blank"&gt;https://blogs.sas.com/content/graphicallyspeaking/2017/11/01/legend-order-redux/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope you can use this method to get the legend entries in the custom order you need.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2023 19:58:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861837#M340404</guid>
      <dc:creator>Jay54</dc:creator>
      <dc:date>2023-03-01T19:58:32Z</dc:date>
    </item>
    <item>
      <title>Re: Sortorder in Keylegend</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861905#M340426</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13856"&gt;@Jay54&lt;/a&gt;&amp;nbsp; Very easy and efficient method, thankyou&lt;/P&gt;</description>
      <pubDate>Thu, 02 Mar 2023 07:17:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sortorder-in-Keylegend/m-p/861905#M340426</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2023-03-02T07:17:57Z</dc:date>
    </item>
  </channel>
</rss>

