Could someone please help me figure out why SAS is not assigning colors to my bar chart in the correct order? It is driving me crazy! I want colors to be assigned to each frequency level as I've written it out:
proc format;
value imp_screening
1="Very unimportant"
2="A litte unimportant"
3="Neutral"
4="A litte important"
5="Very important"
6="I do not have a sense of this";
run;
proc sgplot data=IMP_COMB NOCYCLEATTRS noautolegend;
vbar cat /group = howoft GROUPDISPLAY = CLUSTER fillattrs=howoft;
title 'Responses to: "Indicate the importance of screening for each of the following in primary care" (n=52)';
XAXIS DISPLAY=(NOLABEL);
keylegend / title=" ";
styleattrs datacolors=(CX8B0021 /*dark red*/ CXAB0532 /*light red*/ CX558DBD /*grey*/ CX002872 /*light blue*/ CXE2E9EB /*dark blue*/ CXF4EDE5 /*beige*/);
format cat screener. howoft imp_screening.;
run;
I want neutral to be grey and a little unimportant to be light red, but it keeps switching them:
No matter what I do, as the picture shows, neutral is red and a little unimportant is grey. Please help, it is driving me crazy! I would also appreciate some help getting the key legend in the right order (very unimportant --> very important). Thanks so much!
I think the attribute map should do the job:
ods graphics /
height=5.5in
width=5.5in;
data imp_comb;
call streaminit(42);
do howoft = 1,6,3,4,5,2;
do cat = "A","B","C";
do i = 1 to rand("integer",0,3);
output;
end;
end;
end;
run;
proc format;
value imp_screening
1="Very unimportant"
2="A litte unimportant"
3="Neutral"
4="A litte important"
5="Very important"
6="I do not have a sense of this";
run;
data myattrmap;
length fillcolor $ 20;
input id $ v fillcolor $;
linecolor = fillcolor;
value = put(v,imp_screening.);
datalines;
myid 1 CX8B0021
myid 2 CXAB0532
myid 3 CX558DBD
myid 4 CX002872
myid 5 CXE2E9EB
myid 6 CXF4EDE5
;
run;
proc print;
run;
proc sgplot data=IMP_COMB NOCYCLEATTRS noautolegend dattrmap=myattrmap;
vbar cat / group = howoft GROUPDISPLAY = CLUSTER /*fillattrs=howoft*/ attrid=myid;
title 'Responses to: "Indicate the importance of screening for each of the following in primary care" (n=52)';
XAXIS DISPLAY=(NOLABEL);
keylegend / title=" ";
format /*cat screener.*/ howoft imp_screening.;
run;
Search for "Discrete Attribute Map Data Sets" in the documentation.
Bart
Try sorting your data by the group variable.
The colors tend to be assigned "first value encountered gets first data color".
I think the attribute map should do the job:
ods graphics /
height=5.5in
width=5.5in;
data imp_comb;
call streaminit(42);
do howoft = 1,6,3,4,5,2;
do cat = "A","B","C";
do i = 1 to rand("integer",0,3);
output;
end;
end;
end;
run;
proc format;
value imp_screening
1="Very unimportant"
2="A litte unimportant"
3="Neutral"
4="A litte important"
5="Very important"
6="I do not have a sense of this";
run;
data myattrmap;
length fillcolor $ 20;
input id $ v fillcolor $;
linecolor = fillcolor;
value = put(v,imp_screening.);
datalines;
myid 1 CX8B0021
myid 2 CXAB0532
myid 3 CX558DBD
myid 4 CX002872
myid 5 CXE2E9EB
myid 6 CXF4EDE5
;
run;
proc print;
run;
proc sgplot data=IMP_COMB NOCYCLEATTRS noautolegend dattrmap=myattrmap;
vbar cat / group = howoft GROUPDISPLAY = CLUSTER /*fillattrs=howoft*/ attrid=myid;
title 'Responses to: "Indicate the importance of screening for each of the following in primary care" (n=52)';
XAXIS DISPLAY=(NOLABEL);
keylegend / title=" ";
format /*cat screener.*/ howoft imp_screening.;
run;
Search for "Discrete Attribute Map Data Sets" in the documentation.
Bart
Amazing, thanks so much for writing out all the code! Still haven't resolved the legend issue, but this fixed the color issue.
Try this and let me know if this is what you need:
proc sgplot data=IMP_COMB NOCYCLEATTRS noautolegend dattrmap=myattrmap;
vbar cat / group = howoft GROUPDISPLAY = CLUSTER /*fillattrs=howoft*/ attrid=myid
GROUPORDER=ASCENDING /* <- NEW */
;
title 'Responses to: "Indicate the importance of screening for each of the following in primary care" (n=52)';
XAXIS DISPLAY=(NOLABEL);
keylegend / title=" ";
format /*cat screener.*/ howoft imp_screening.;
run;
Bart
/*
Change your format as the following.
a.k.a padding white blanks befor label
*/
proc format;
value imp_screening
1=" Very unimportant"
2=" A litte unimportant"
3=" Neutral"
4=" A litte important"
5=" Very important"
6="I do not have a sense of this";
run;
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.