BookmarkSubscribeRSS Feed
CColpaert
SAS Employee
I have an SGPlot statement which shows the sentiment (var: sentiment_category - values: Postive, Negative, Neutral) per category (var: category - values: service, price, product,..)

proc sgplot data = graph_final;
vbar category / freq=count group=sentiment_category NOSTATLABEL /*datalabel*/;
xaxis label=" " discreteorder=data;
yaxis label=" " GRID ;
run;

To align the colors with the sentiment (green=positive, yellow=neutral and red=negative) I’ve modified the style as following:

proc template;
Define style styles.mystyle;
Parent=styles.&styleclass.;
Style graphdata1 from graphdata1 / contrastcolor=grey color=CX84AF5B; /* green */
Style graphdata2 from graphdata2 / contrastcolor=grey color=CXF1DC63; /* yellow */
Style graphdata3 from graphdata3 / contrastcolor=grey color=CXC95A49; /* red */
end;
run;

This works if for the first category there are no zeroes for sentiment_category:

category;sentiment_category;COUNT;percent;graph_order
Car;Positive;3;10;1
Car;Neutral;1;3;2
Car;Negative;26;87;3
General;Positive;12;60;1
General;Neutral;0;0;2
General;Negative;8;40;3
Price;Positive;1;33;1
Price;Neutral;0;0;2
Price;Negative;2;67;3
Service;Positive;24;67;1
Service;Neutral;0;0;2
Service;Negative;12;33;3

In this case, everything is how I want it to be. But suppose that the “neutral” for Car is 0, then the colors are no longer aligned with the sentiment. I understand the colors are assigned based on the order of appearance and if the value for a sentiment_category is 0 then it is not taken into account.

How can I make sure that the colors are always aligned with the sentiment?

One suggestion I've had is to make the proc template definition variable, based on the actual order in the dataset (e.g. if the first non-blank value is Negative, than the first color is red) which does align the colors with the sentiment but.. I also want to make sure that within the stacked bar the positive sentiment is on the bottom, the neutral sentiment in the middle and the negative sentiment on top. Continuing with the same example (suppose that the "neutral" for Car is 0), the stacked bar for the category "car" consists of positive (green) and negative (red). For the second category (General) the order within the stack is positive, negative and neutral. The colors are aligned with the sentiment but the order within the stack needs to be positive, neutral and negative.

PS: if my explanation isn't clear enough or if I need to include a working example, please let me know.

Thanks,
Cedric
2 REPLIES 2
DanH_sas
SAS Super FREQ

In SAS 9.3, you will easily be able to associate visual attributes with data values in what's called an "attribute map". In the meantime, you can use an INDEX in GTL to create the linkage. The following program should get you what you want. Let me know if you have any questions.

Thanks!

Dan

proc template;

Define style styles.mystyle;

Parent=styles.listing;

Style graphdata1 from graphdata1 / contrastcolor=grey color=CX84AF5B; /* green */

Style graphdata2 from graphdata2 / contrastcolor=grey color=CXF1DC63; /* yellow */

Style graphdata3 from graphdata3 / contrastcolor=grey color=CXC95A49; /* red */

end;

run;

data sentiment;

input category $ sentiment_category $ COUNT percent graph_order;

cards;

Car Positive 3 10 1

Car Neutral 1 3 2

Car Negative 26 87 3

General Positive 12 60 1

General Neutral 0 0 2

General Negative 8 40 3

Price Positive 1 33 1

Price Neutral 0 0 2

Price Negative 2 67 3

Service Positive 24 67 1

Service Neutral 0 0 2

Service Negative 12 33 3

;

run;

data indexed;

set sentiment;

if (sentiment_category eq "Positive") then index=1;

if (sentiment_category eq "Neutral") then index=2;

if (sentiment_category eq "Negative") then index=3;

run;

proc template;

define statgraph barchart;

begingraph;

   layout overlay / xaxisopts=( Label=" " ) yaxisopts=( griddisplay=on Label=" " linearopts=(Integer=true));

      BarChart X=category Y=Count / Group=sentiment_category index=index NAME="VBAR";

      DiscreteLegend "VBAR"/ title="sentiment_category";

   endlayout;

endgraph;

end;

run;

ods listing style=mystyle;

proc sgrender data=indexed template=barchart; run;

CColpaert
SAS Employee

Works great! Thanks a lot Dan

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 2 replies
  • 2201 views
  • 0 likes
  • 2 in conversation