BookmarkSubscribeRSS Feed
Anita_n
Pyrite | Level 9

I have a question. I need to sort the labels displayed in my legend in a particular order. Not descending, ascending nor reversed.

Is there any way to do that?

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

What plot are you talking about? Show us some of your code 🙂

 

Or perhaps this article can help you:

 

Legend Order in SGPLOT Procedure

Anita_n
Pyrite | Level 9

Thanks draycut for your reply am still trying to implement that but it hasn't worked yet. I hope am not making any mistake. I will let you know if it worked

PeterClemmensen
Tourmaline | Level 20

Anytime.. Post your code if you want a more usable answer 🙂

RW9
Diamond | Level 26 RW9
Diamond | Level 26

The simplest way is to set your labels to be numbers in the order you want to see them, and then apply a format to the labels so they show the text, e.g.:

proc format;
  value temp
    1="Value 2"
     2="Value 3"
     3="Value 1";
run;

data have;
  set have;
  if lab="Value 2" then labn=1;
  ...;
  format labn temp.;
run;

Then use labn as your labels, this will sort by the number, and display the test per the format.

Anita_n
Pyrite | Level 9

Thanks RW9 for your reply am still trying to implement that in sgplot but it hasn't worked yet. I hope it works.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

No probs.  You might want to favourite this site:

http://blogs.sas.com/content/graphicallyspeaking/

 

Its has thousands of examples of all types of graphs using sgplot/gtl.  Very useful.  

Anita_n
Pyrite | Level 9

Hello draycut, hello RW9,

thanks for the help, It unfortunately didn't work with your suggestions, I probably might have made a mistake some where in the code but I finally found a very easy way to solve the problem.

 

the issue is that I wanted to sort the listed items in my keylegend. Not using ascending or descending order as used in Sortorder (the Keylegend Statement).

 

I solved the problem this way: since I have set the order already in my attrmap while assigning colors to the bars. I just added an extra column to my attrmap which is Show = "Attrmap"

this displays the legend items in the order which is defined in the Attrmap for a example if you assign colors this way using attrmap

 

data attrmap;

input id $ value $10. @19 fillcolor $8. show $10.;
datalines;

flavor Vanilla beige     attrmap
flavor Chocolate cx663D29  attrmap
flavor Strawberry pink attrmap

;

run;

 

this will display the items in your legend in this order vanilla, Chocolate, Strawberry

DanH_sas
SAS Super FREQ

Great solution, Anita!

 

A related tip:

When using the SHOW and NOCASE columns, I usually RETAIN the values for the attrmap, as you can have only one value for these columns per map. Using RETAIN helps simplify the data step a bit:

 

data attrmap;

retain show "attrmap";

input id $ value $10. @19 fillcolor $8.;
datalines;

flavor Vanilla beige     
flavor Chocolate cx663D29  
flavor Strawberry pink 

;

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 8 replies
  • 1824 views
  • 4 likes
  • 4 in conversation