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

I am creating a bar plot but would like to have the data labels inside the bars at the bottom. Here is my code so far. Thanks!

PS. Also - when I insert the sas code here, how do I make it look like the original sas code (colors)? 🙂

data test;
input id group var;
cards;

1 1 2.236
2 1 4.569
3 2 3.694
4 2 1.256
;
proc sgplot data=test;
vbar group / response=var stat=mean datalabel group=group fillattrs=(color=royalblue) DATALABELATTRS=(size=14);
xaxis display=(noline noticks) labelattrs=(size=16) valueattrs=(size=16) ;
yaxis display=(noline) labelattrs=(size=16) valueattrs=(size=16);
keylegend / noborder location=outside position=bottom valueattrs=(size=12);
format var 6.2;
title height=16pt 'var by group';
run;
quit;
1 ACCEPTED SOLUTION

Accepted Solutions
dpalmer1
Fluorite | Level 6

Add a data step after LSMeans are output from PROC GLM:

 

data LS;
	set LS;
	yposition = 0;
run;

Use the modified PROC SGPLOT code with the VBARPARM and TEXT statements:

 

proc sgplot data=LS;
vbarparm category = group response=LSMean / group=group fillattrs=(color=royalblue);
xaxis display=(noline noticks) labelattrs=(size=16) valueattrs=(size=16) ;
yaxis display=(noline) labelattrs=(size=16) valueattrs=(size=16);
keylegend / noborder location=outside position=bottom valueattrs=(size=12);
format LSMean 6.2;
title height=16pt 'var by group';
text x = group y = yposition text = LSMean / position = top contributeoffsets = none textattrs = (size = 14);
run;
quit;

 

View solution in original post

4 REPLIES 4
dpalmer1
Fluorite | Level 6

I would create a dataset with the means first and use the VBARPARM and TEXT statements with that data.  I added the variable yposition to your data step to place the labels at the bottom.

 

data test;
input id group var;
yposition = 0;
cards;

1 1 2.236
2 1 4.569
3 2 3.694
4 2 1.256
;

proc means data = test noprint;
	var var;
	by group;
	output out = have mean = / autoname;
	id yposition;
run;

proc sgplot data=have;
vbarparm category = group response = var_Mean / fillattrs=(color=royalblue);
xaxis display=(noline noticks) labelattrs=(size=16) valueattrs=(size=16) ;
yaxis display=(noline) labelattrs=(size=16) valueattrs=(size=16);
keylegend / noborder location=outside position=bottom valueattrs=(size=12);
format var_Mean 6.2;
title height=16pt 'var by group';
text x = group y = yposition text = var_Mean / position = top contributeoffsets = none textattrs = (size = 14);
run;
quit;

 

Source: https://blogs.sas.com/content/graphicallyspeaking/2022/04/23/optimizing-bar-label-placement/ 

 

To get the SAS code with the colors, it may work to try pasting the code from your SAS editor into MS Word.

mateescu
Fluorite | Level 6

This is great, thanks. However, I made up that example. I am actually running some regressions, putting out the LSMeans and then try to plot those. I expended the code on this example so you can see the set up. How do I get the data labels inside the bars in this case? Thanks,

 

data test;
input id group var;
cards;

1 1 2.236
2 1 4.569
3 2 3.694
4 2 1.256
;

proc glm data=test; 
class group ;
model  Var	=    group  / Solution ss3;
output out=GLMout Predicted=Pred lclm=lclm uclm=uclm;
lsmeans group/ cl;
ods output LSMeanCL=LS;
run;

proc sgplot data=LS;
vbar group / response=LSMean stat=mean datalabel group=group fillattrs=(color=royalblue) DATALABELATTRS=(size=14) datalabelpos=data;
xaxis display=(noline noticks) labelattrs=(size=16) valueattrs=(size=16) ;
yaxis display=(noline) labelattrs=(size=16) valueattrs=(size=16);
keylegend / noborder location=outside position=bottom valueattrs=(size=12);
format LSMean 6.2;
title height=16pt 'var by group';
run;
quit;

 

dpalmer1
Fluorite | Level 6

Add a data step after LSMeans are output from PROC GLM:

 

data LS;
	set LS;
	yposition = 0;
run;

Use the modified PROC SGPLOT code with the VBARPARM and TEXT statements:

 

proc sgplot data=LS;
vbarparm category = group response=LSMean / group=group fillattrs=(color=royalblue);
xaxis display=(noline noticks) labelattrs=(size=16) valueattrs=(size=16) ;
yaxis display=(noline) labelattrs=(size=16) valueattrs=(size=16);
keylegend / noborder location=outside position=bottom valueattrs=(size=12);
format LSMean 6.2;
title height=16pt 'var by group';
text x = group y = yposition text = LSMean / position = top contributeoffsets = none textattrs = (size = 14);
run;
quit;

 

mateescu
Fluorite | Level 6

Perfect. Thanks!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 567 views
  • 0 likes
  • 2 in conversation