Hi,
recently I've found a post with a way to create a customized bar chart: https://communities.sas.com/t5/SAS-Programming/Creating-a-customized-waterfall-graph/m-p/326830#M728...
@Rick_SAS mentioned, that you can add labels to bars using TEXT statement, this is my modified code and output:
data Test;
length Name $13. Type $1.;
input Name $13. Amount Type $1.;
if missing(type) then type=".";
datalines;
CY15 97.9 A
CY15 35.1 B
CY15 38.9 C
CY16 102.0 A
CY16 43.2 B
CY16 44.6 C
CY17 106.8 A
CY17 49.9 B
CY17 46.7 C
Co. A:Row 203.4 .
Co. A:Row 6.0 A
Co. A:US 209.6 .
Co. A:US 5.5 A
Co. A:Dom 215.1 .
Co. A:Dom 3.0 A
Co. B:Dom+Int 218.1 .
Co. B:Dom+Int 9.6 B
Co. C:Dom+Int 227.7 .
Co. C:Dom+Int 4.1 C
CY18 121.3 A
CY18 59.5 B
CY18 50.8 C
;
proc sort data=test;
by name;
run;
data test2;
set test;
retain label_position;
by name;
if first.name then do;
label_position=amount;
end;
else do;
label_position+amount;
end;
run;
proc sgplot data=Test2;
styleattrs datacolors=(orange green red white);
vbarparm category=Name response=Amount / group=Type nooutline;
*seglabel seglabelfitpolicy=none seglabelattrs=(weight=bold);
text x=name y=label_position text=Amount;
xaxis display=(nolabel);
yaxis labelpos=top;
run;
My question is: Is it possible to display only the labels of the colored (non-white) bars?
Ideally I'd like to display just the labels of the colored bars and place them either in the middle of the bar (if it fits inside) or outside of it (if it doesn't). But I'd be more than happy if I could just hide the labels of the white bars.
Thanks for any help!
Don't use Amount as the label. Create a label variable that is either a formatted value of Amount or is blank.:
data test2;
set test;
retain label_position;
by name;
if first.name then do;
label_position=amount;
end;
else do;
label_position+amount;
end;
if Type="." then MyLabel=" ";
else MyLabel=put(Amount, Best5.);
run;
proc sgplot data=Test2;
styleattrs datacolors=(orange green red white);
vbarparm category=Name response=Amount / group=Type nooutline;
*seglabel seglabelfitpolicy=none seglabelattrs=(weight=bold);
text x=name y=label_position text=MyLabel;
xaxis display=(nolabel);
yaxis labelpos=top;
run;
Don't use Amount as the label. Create a label variable that is either a formatted value of Amount or is blank.:
data test2;
set test;
retain label_position;
by name;
if first.name then do;
label_position=amount;
end;
else do;
label_position+amount;
end;
if Type="." then MyLabel=" ";
else MyLabel=put(Amount, Best5.);
run;
proc sgplot data=Test2;
styleattrs datacolors=(orange green red white);
vbarparm category=Name response=Amount / group=Type nooutline;
*seglabel seglabelfitpolicy=none seglabelattrs=(weight=bold);
text x=name y=label_position text=MyLabel;
xaxis display=(nolabel);
yaxis labelpos=top;
run;
That is so simple, I can't believe I didn't think of it 😄
Thank you so much!
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.