BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Junyong
Pyrite | Level 9

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.

1.png

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.

2.png

How can I suppress the "groupname" here while avoiding this untying behavior? Thanks for your help.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

What version of SAS do you use?

Junyong
Pyrite | Level 9
It's 9.4 TS1M5. I found the same at https://odamid.oda.sas.com/ too.
ballardw
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 796 views
  • 0 likes
  • 3 in conversation