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:
How can I replace "base" and "advanced" with "base dose" and "advanced dose" ?
Thank you in advance!
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;
Simply change them in your data?
No I can not change it in the dataset, I was looking for a quicker way.
What do you mean by "create a label"?
Sorry, that was a mistake. What version of SAS do you use?
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.
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;
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!
@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.
Thanks for your reply. Do you mean I should change it in the dataset? Or how?
@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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.