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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 505 views
  • 0 likes
  • 3 in conversation