BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Jason2020
Obsidian | Level 7

I would like to have 2 keylegends in a 2-panel graph in Proc SGPANEL (see code below). As you may note in the figure below, the keylegends is not sorted by the demographics1 or demographics2. Sorting the data by these 2 variables did not make a difference. Is there any way to make 2 separate legends by the variable demographics1: one for the gender, and the other for race?

Thank you.

 

proc sgpanel;

styleattrs datacolors=(blue lightred grey brown white)

   datalinepatterns=(solid);

panelby parameter Demographics1 /novarname noheaderborder

   headerattrs=(size=18pt weight=bold)

   layout=lattice colheaderpos=bottom;

vbarparm category=threshold response=prevalence /

  group=Demographics2 groupdisplay=cluster

   dataskin=crisp grouporder=ascending;

colaxis discreteorder=data

  display=(noticks nolabel)

  valueattrs=(size=16pt weight=bold);

rowaxis label='Percent'

  labelattrs=(size=24pt weight=bold)

  valueattrs=(size=16pt weight=bold)

  values=(0 to 80 by 20);

keylegend /position=top autoitemsize down=3

  title="" sortorder=ascending

  titleattrs=(Family=Arial Size=12 Weight=Bold)

  valueattrs=(Family=Arial Size=18 Weight=Bold);

run;

 

Jason2020_0-1675793504014.png

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
DanH_sas
SAS Super FREQ

Try this EXCLUDE trick in the KEYLEGEND statement and see if it works for you:

 

proc sgpanel;
styleattrs datacolors=(blue lightred grey brown white)
   datalinepatterns=(solid);
panelby parameter Demographics1 /novarname noheaderborder
   headerattrs=(size=18pt weight=bold)
   layout=lattice colheaderpos=bottom;
vbarparm category=threshold response=prevalence /
  group=Demographics2 groupdisplay=cluster name="bar"
   dataskin=crisp grouporder=ascending;
colaxis discreteorder=data
  display=(noticks nolabel)
  valueattrs=(size=16pt weight=bold);
rowaxis label='Percent'
  labelattrs=(size=24pt weight=bold)
  valueattrs=(size=16pt weight=bold)
  values=(0 to 80 by 20);
keylegend "bar" / position=topleft autoitemsize across=1
  title="" sortorder=ascending
  exclude=("Female" "Male")
  titleattrs=(Family=Arial Size=12 Weight=Bold)
  valueattrs=(Family=Arial Size=18 Weight=Bold);
keylegend "bar" / position=topright autoitemsize across=1
  title="" sortorder=ascending
  exclude=("Black" "Mexican" "White")
  titleattrs=(Family=Arial Size=12 Weight=Bold)
  valueattrs=(Family=Arial Size=18 Weight=Bold);
run;

View solution in original post

3 REPLIES 3
DanH_sas
SAS Super FREQ

Try this EXCLUDE trick in the KEYLEGEND statement and see if it works for you:

 

proc sgpanel;
styleattrs datacolors=(blue lightred grey brown white)
   datalinepatterns=(solid);
panelby parameter Demographics1 /novarname noheaderborder
   headerattrs=(size=18pt weight=bold)
   layout=lattice colheaderpos=bottom;
vbarparm category=threshold response=prevalence /
  group=Demographics2 groupdisplay=cluster name="bar"
   dataskin=crisp grouporder=ascending;
colaxis discreteorder=data
  display=(noticks nolabel)
  valueattrs=(size=16pt weight=bold);
rowaxis label='Percent'
  labelattrs=(size=24pt weight=bold)
  valueattrs=(size=16pt weight=bold)
  values=(0 to 80 by 20);
keylegend "bar" / position=topleft autoitemsize across=1
  title="" sortorder=ascending
  exclude=("Female" "Male")
  titleattrs=(Family=Arial Size=12 Weight=Bold)
  valueattrs=(Family=Arial Size=18 Weight=Bold);
keylegend "bar" / position=topright autoitemsize across=1
  title="" sortorder=ascending
  exclude=("Black" "Mexican" "White")
  titleattrs=(Family=Arial Size=12 Weight=Bold)
  valueattrs=(Family=Arial Size=18 Weight=Bold);
run;
Jason2020
Obsidian | Level 7

Hi Dan,

It actually worked. I did however get an error message, that "topright" and "topleft" were not acceptable, so I used "top" and "bottom" instead. Here is how the figure looks now.

Thank you very much for your help.

 

Jason2020_0-1675807528989.png

 

 

DanH_sas
SAS Super FREQ

Ah, yes...sorry about that. Those values are supported in SGPLOT, but not SGPANEL. The way you have them arranged looks better anyway :-).

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
  • 763 views
  • 2 likes
  • 2 in conversation