This article is by guest contributor Lelia McConnell, SAS Tech Support.
Several users have called recently to ask the question, “Can I reorder the legend entries on the bar chart that I created with PROC SPLOT?”
Although there is no option that does this directly in PROC SGPLOT, the answer to this question is “YES, you can define the order of your legend entries.”
In this post, I present an example that illustrates the syntax that you would use to define the order of your legend entries. For this example, we begin by sub setting the data set SASHELP.CARS to include only observations in which TYPE is not equal to HYBRID. To do this, we use the WHERE option in the SET statement, thus creating the data set CARS.
By bringing the CARS data set into PROC SGPLOT, I can create a vertical bar chart of the values of ORIGIN, where the height of the bars is based on the mean values of the variable MPG_CITY and the group variable is TYPE.
data cars; set sashelp.cars(where=(type ne 'Hybrid')); run; proc sgplot data=cars; vbar origin / response=mpg_city group=type groupdisplay=cluster stat=mean ; xaxis display=(nolabel); title 'Mileage by Origin and Type'; run;
Instead of the default order of SUV, Sedan, Sports, Truck, and Wagon, I want the order of the legend entries to be Wagon, Sports, SUV, Truck, and Sedan. To make this change, I create a numeric variable that contains the values 1-5, based on the order in which I want my vehicle types to be displayed.
I need to create a format in order to display the values of TYPE in the legend instead of 1-5. The most efficient way to do this is to create a control data set that I can use with PROC FORMAT. Since I need only one observation for each value of MYTYPE in this data set, I will sort the data by MYTYPE so that I can use the FIRST logic in the DATA step that follows. In the DATA step, I need to create the columns FMTNAME, START, and LABEL. These are used to define the format name, original value, and format values, respectively. When you create a format, the automatic variable TYPE defines the variable as numeric or character, so we need to include the statement DROP TYPE to remove the variable TYPE from the control data set.
Now I can resubmit my original PROC SGPLOT code along with the FORMAT statement to create the legend in the correct order and the KEYLEGEND statement with the TITLE option in order to keep the original title in my legend.
data newcars; set cars; if type='Wagon' then mytype=1; else if type='Sports' then mytype=2; else if type='SUV' then mytype=3; else if type='Truck' then mytype=4; else if type='Sedan' then mytype=5; run; proc sort data=newcars out=sortcars; by mytype; run; data myfmt; set sortcars; by mytype; if first.mytype then do; fmtname='typefmt'; start=mytype; label=type; drop type; output; end; run; proc format cntlin=myfmt; run; proc sgplot data=newcars; vbar origin / response=mpg_city group=mytype groupdisplay=cluster stat=mean; xaxis display=(nolabel); keylegend /title='Type'; format mytype typefmt.; title 'Mileage by Origin and Type'; run;
Full SAS 9.3 SGPLOT code: Legend_93