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

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:

SGPlot17.png

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! 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Amethyst | Level 16

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;

yabwon_0-1679482623141.png

 

Search for "Discrete Attribute Map Data Sets" in the documentation.

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

5 REPLIES 5
ballardw
Super User

Try sorting your data by the group variable.

The colors tend to be assigned "first value encountered gets first data color".

yabwon
Amethyst | Level 16

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;

yabwon_0-1679482623141.png

 

Search for "Discrete Attribute Map Data Sets" in the documentation.

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



keherder
Obsidian | Level 7

Amazing, thanks so much for writing out all the code! Still haven't resolved the legend issue, but this fixed the color issue.

yabwon
Amethyst | Level 16

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Ksharp
Super User
/*
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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1584 views
  • 0 likes
  • 4 in conversation