Data visualization with SAS programming

Sortoder keylegend sgplot

Reply
Contributor
Posts: 57

Sortoder keylegend sgplot

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?

PROC Star
Posts: 1,190

Re: Sortoder keylegend sgplot

[ Edited ]

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

 

Or perhaps this article can help you:

 

Legend Order in SGPLOT Procedure

Contributor
Posts: 57

Re: Sortoder keylegend sgplot

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

PROC Star
Posts: 1,190

Re: Sortoder keylegend sgplot

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

Super User
Super User
Posts: 9,203

Re: Sortoder keylegend sgplot

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.

Contributor
Posts: 57

Re: Sortoder keylegend sgplot

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

Super User
Super User
Posts: 9,203

Re: Sortoder keylegend sgplot

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.  

Contributor
Posts: 57

Re: Sortoder keylegend sgplot


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

SAS Super FREQ
Posts: 1,044

Re: Sortoder keylegend sgplot

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;

Ask a Question
Discussion stats
  • 8 replies
  • 176 views
  • 4 likes
  • 4 in conversation