I have the following data.
data have;
do groupname="Group 1","Group 2","Group 3";
do i=1 to 100;
x=2*substr(groupname,6)+rannor(1);
output;
end;
end;
run;
As I want to draw a histogram of three groups, I did the following.
proc sgplot;
histogram x/group=groupname transparency=0.3333;
density x/group=groupname type=kernel;
run;
Here's the outcome.
I tried to erase the "groupname" inside the bottom legend box, so used KEYLEGEND as follows.
proc sgplot;
histogram x/group=groupname transparency=0.3333;
density x/group=groupname type=kernel;
keylegend/title="";
run;
And it seems KEYLEGEND unties the items and the outcome repeats these groups twice.
How can I suppress the "groupname" here while avoiding this untying behavior? Thanks for your help.
You are getting two sets in the legend because the default is to provide a legend for each plot.
You can request which items to include in the legend by listing the names in the Keylegend statement. Which means that you use the options on the histogram and density statements to name their legend entries.
Example below should only show the legend for the histogram. Change 'Hist' to 'Dens' to show the density legend.
proc sgplot; histogram x/group=groupname transparency=0.3333 name='Hist'; density x/group=groupname type=kernel name='Dens'; keylegend 'Hist' / title="" ; run;
What version of SAS do you use?
You are getting two sets in the legend because the default is to provide a legend for each plot.
You can request which items to include in the legend by listing the names in the Keylegend statement. Which means that you use the options on the histogram and density statements to name their legend entries.
Example below should only show the legend for the histogram. Change 'Hist' to 'Dens' to show the density legend.
proc sgplot; histogram x/group=groupname transparency=0.3333 name='Hist'; density x/group=groupname type=kernel name='Dens'; keylegend 'Hist' / title="" ; run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.