I would like to add a legend to the bottom of my bar chart. In the legend I would like the bar colors to correspond with the legend. For example, in the legend the little box besides participants should be blue and cumulative should be orange. The code is below and the data is attached. Thank you.
proc import datafile = "/home/luvscandy270/CHARTONE.xlsx"
DBMS = xlsx out = graph replace ;
run;
proc sort data=graph ;
by year descending regstatusnew;
run;
/*Set the graph colors*/
data graphcolor;
set graph;
if regstatusnew='Participant' then FLAG=1;
else FLAG = 0;
run;
title 'Test Double Bar Graph';
/*ods graphics / border=off*/ ;
proc sgplot data=graphcolor noautolegend noborder nowall;
vbar year / response=totals colorresponse=flag colormodel=(orange bigb) stat=sum group=regstatusnew nostatlabel
groupdisplay=cluster grouporder=data ;
xaxis display=(nolabel);
yaxis grid;
run;
No need to create new data. Simply loose the NOAUTOLEGEND Option and do this
data graph;
input YEAR REGSTATUSNEW :$11. TOTALS;
datalines;
2014 Participant 28.475
2015 Participant 28.132
2016 Participant 40.526
2017 Participant 28.179
2018 Participant 39.876
2019 Participant 27.195
2020 Participant 31.497
2014 Cumulative 28.475
2015 Cumulative 56.607
2016 Cumulative 97.133
2017 Cumulative 125.312
2018 Cumulative 165.188
2019 Cumulative 192.383
2020 Cumulative 223.880
;
proc sort data=graph ;
by year descending regstatusnew;
run;
title 'Test Double Bar Graph';
proc sgplot data=graphcolor noborder nowall;
styleattrs datacolors=(bigb orange);
vbar year / response=totals stat=sum group=regstatusnew nostatlabel
groupdisplay=cluster grouporder=data ;
xaxis display=(nolabel);
yaxis grid;
run;
Result:
No need to create new data. Simply loose the NOAUTOLEGEND Option and do this
data graph;
input YEAR REGSTATUSNEW :$11. TOTALS;
datalines;
2014 Participant 28.475
2015 Participant 28.132
2016 Participant 40.526
2017 Participant 28.179
2018 Participant 39.876
2019 Participant 27.195
2020 Participant 31.497
2014 Cumulative 28.475
2015 Cumulative 56.607
2016 Cumulative 97.133
2017 Cumulative 125.312
2018 Cumulative 165.188
2019 Cumulative 192.383
2020 Cumulative 223.880
;
proc sort data=graph ;
by year descending regstatusnew;
run;
title 'Test Double Bar Graph';
proc sgplot data=graphcolor noborder nowall;
styleattrs datacolors=(bigb orange);
vbar year / response=totals stat=sum group=regstatusnew nostatlabel
groupdisplay=cluster grouporder=data ;
xaxis display=(nolabel);
yaxis grid;
run;
Result:
Thanks again! This works great!
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.