I'm reading "Statistical graphics procedures by example" by Matange and Heath. I'm trying to modify the colors in the sgplot in the example below, but so far I have been not sucessful. Please see below for an example. I'm trying to change the color of "actual" by individual products.
data attrs;
input id $ value $ fillcolor $;
datalines;
actual A Blue
actual B Black
actual B Green
actual D Yellow
;
run;
proc sgplot data = sgbook.product_sales dattrmap=attrs;
hbar product /response = actual barwidth=0.5 attrid=id;
hbar product /response=predict barwidth=0.2;
run;
The above code does not work, how would I change the color for individual products.
The problem is that the ATTRID is incorrect. The ATTRID should be the *value* in the ID column -- not the ID column name itself. The ID column is a reserved column name in an ATTMAP, so it does not need to be referenced. In your case, set attrid=actual, and you should get the correct result.
@DanH, I changed the code following your suggestion, I still do not get the color change, Can you please help
data attrs;
input id $ value $ fillcolor $;
datalines;
ACTUAL A Blue
ACTUAL B Black
ACTUAL C Green
ACTUAL D Yellow
;
run;
proc sgplot data = sgbook.product_sales dattrmap=attrs;
hbar product /response = actual barwidth=0.5 attrid=actual;
hbar product /response=predict barwidth=0.2;
run;
The other issue is case-sensitivity. Sorry I missed that earlier. The ID value on the ATTRID option must match the case of the ID value in the ATTRMAP data set. Incidentally, We have added a couple of new columns to the ATTRMAP data set for SAS 9.4m3. One of those columns gives you the ability to turn off case-sensitivity.
@DanH for some reason, I'm still not getting the right colors. I have changed the codes for easy replicability.
I'm running this code on SAS 9.3 (TS1M2)
data sales;
input product $ actual predict;
datalines;
A 10 5
B 12 15
C 15 18
D 13 20
;
run;
data attrs;
input id $ value $ fillcolor $;
datalines;
actual A blue
actual B black
actual C green
actual D yellow
;
run;
proc sgplot data = sales dattrmap=attrs;
hbar product /response = actual barwidth=0.5 attrid=actual;
hbar product /response=predict barwidth=0.2 ;
run;
Sorry, I overlooked that you are not using a GROUP variable. Attrmaps only work when there is a GROUP active. For your example, you just need to use FILLATTRS=(color=<some color>) to control the color of the bars.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.