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

Hi,

I want to change the names in a SGPLOT legend. I am using a grouping variable in character format and I would like to change the names.

 

I'm using SAS 9.4

 

My code looks like this:

 

DATA cars1;
 INPUT treatment $ ID score ;
CARDS;
base  1 2930 
advanced   1 3350 
base   2 2640 
advanced  2 6150 
base  3 4080 
advanced  3 5250
;
RUN; 

proc sgplot data=cars1;
hbox score / group=treatment;
keylegend;
run;

And my result is:

 

Screenshot 2021-03-02 at 10.17.33.png

How can I replace "base" and "advanced" with "base dose" and "advanced dose" ?

 

Thank you in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

I think that nearly 99% of the time when the question is "display this value instead of what is actually in the data" the response is a custom format.

 

Try this:

proc format;
value $dosegroup
'base'     = 'Base Dose'
'advanced' = 'Advanced Dose'
;
run;

proc sgplot data=cars1;
hbox score / group=treatment;
keylegend;
format treatment $dosegroup.;
run;

View solution in original post

10 REPLIES 10
PeterClemmensen
Tourmaline | Level 20

Simply change them in your data?

marta25
Obsidian | Level 7

No I can not change it in the dataset, I was looking for a quicker way. 

What do you mean by "create a label"?

PeterClemmensen
Tourmaline | Level 20

Sorry, that was a mistake. What version of SAS do you use?

marta25
Obsidian | Level 7

I have SAS University Edition and it should be release 9.4.

I tried LEGENDITEM but it does not get highlighted, so I guess I do not have the version that supports it.

ballardw
Super User

I think that nearly 99% of the time when the question is "display this value instead of what is actually in the data" the response is a custom format.

 

Try this:

proc format;
value $dosegroup
'base'     = 'Base Dose'
'advanced' = 'Advanced Dose'
;
run;

proc sgplot data=cars1;
hbox score / group=treatment;
keylegend;
format treatment $dosegroup.;
run;
marta25
Obsidian | Level 7

Thanks, this works! 

However, it generates another problem.

I was using an attribute map to set the colors in the box plot:

data myattrmap;
	length linecolor $ 9 fillcolor $ 9 value $ 12 ID $ 12;
	input ID $ value $ linecolor $ fillcolor $;
	datalines;
treatment  base black grey  
treatment  advanced green green 
;
run;

Now if I try: 

proc sgplot data=data dattrmap=myattrmap;
hbox... / ...
attrid=treatment;
format treatment=$dosegroup; run;

The attribute map is not applied and I have the same plot as before with default colors.

Is there a way to keep using such attribute map?

Thank you!

ballardw
Super User

@marta25 wrote:

Thanks, this works! 

However, it generates another problem.

I was using an attribute map to set the colors in the box plot:

data myattrmap;
	length linecolor $ 9 fillcolor $ 9 value $ 12 ID $ 12;
	input ID $ value $ linecolor $ fillcolor $;
	datalines;
treatment  base black grey  
treatment  advanced green green 
;
run;

Now if I try: 

proc sgplot data=data dattrmap=myattrmap;
hbox... / ...
attrid=treatment;
format treatment=$dosegroup; run;

The attribute map is not applied and I have the same plot as before with default colors.

Is there a way to keep using such attribute map?

Thank you!


The documentation for DATTRMAP sets pretty clearly states that the value has to be the Formatted value of a variable.

So modify the value of the attribute data set.

marta25
Obsidian | Level 7

Thanks for your reply. Do you mean I should change it in the dataset? Or how?

ballardw
Super User

@marta25 wrote:

Thanks for your reply. Do you mean I should change it in the dataset? Or how?


Yes in the dattrmap set. Something like this is one way. The DSD option will allow a space in the value of a read variable when the value is enclosed in quotes. However, make sure there is only one space between values as consecutive blanks would mean a "missing" value. Or provide a different delimiter and the quotes wouldn't be needed. Note that value was made longer to fit Advanced dose.

data myattrmap;
	length linecolor $ 9 fillcolor $ 9 value $ 15 ID $ 12;
   infile datalines dlm=' ' dsd;
	input ID $ value $ linecolor $ fillcolor $;
	datalines;
treatment "Base Dose" black grey  
treatment "Advanced Dose" green green 
;

The value will be case sensitive.

marta25
Obsidian | Level 7
Perfect, thank you so much!!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 10 replies
  • 2884 views
  • 4 likes
  • 3 in conversation