Hi
I have been attempting to scour the different forums to find a code for how to add data labels to the top of my bars when plotting in PROC FREQ.
I was hoping to do it in one statement; not having to create a new output dataset.
My syntax so far is:
Proc freq data = thesis order=freq;
Tables species /
plots=freqplot(orient=vertical scale=percent);
datalabel;
BY site;
Run;
The proc works fine until i insert the datalabel code. I found the datalabel command in a code for sgplot, which I'm sure is why it doesn't work.
The log gives me this when including the datalabel command to my code:
I knew it wouldn't work, as it turns up red when writing it in SAS. I also experimented with BARCHARTPARM but this does not work either, as I think this command requires a new dataset output.
When just doing this:
Proc freq data = thesis order=freq;
Tables species /
plots=freqplot(orient=vertical scale=percent);
BY site;
Run;
...I get these two charts:
...which is exactly what I want, but i also want the %-values added on top of the bar, like this:
(...that chart is from an sgplot)
How do I code the same type of barlabel into my proc freq?
Best regards
Maja
Hi @Lysegroentblad,
I think it's easier (than modifying an ODS template) and also the more flexible approach to apply PROC SGPLOT to an output dataset from PROC FREQ.
Example:
/* Create test data for demonstration */
data have;
retain site 'M ';
input species $ _n_ @@;
if _n_>lag(_n_)>. then site='NI';
do _n_=1 to _n_;
output;
end;
cards;
PA 167 PS 34 PC 19 LE 12 PM 8 PA 882 PS 19 LE 16 LM 8 PS3 6 FS 4 AA 2 PS2 1
;
/* Count species by site */
proc freq data=have order=freq;
by site;
tables species / out=want;
run;
/* Create bar charts from percentages */
title 'Distribution of species';
proc sgplot data=want;
by site;
format percent 6.2;
yaxis label='Percent' grid;
vbar species / response=percent categoryorder=respdesc datalabel;
run;
title;
Hi @Lysegroentblad,
I think it's easier (than modifying an ODS template) and also the more flexible approach to apply PROC SGPLOT to an output dataset from PROC FREQ.
Example:
/* Create test data for demonstration */
data have;
retain site 'M ';
input species $ _n_ @@;
if _n_>lag(_n_)>. then site='NI';
do _n_=1 to _n_;
output;
end;
cards;
PA 167 PS 34 PC 19 LE 12 PM 8 PA 882 PS 19 LE 16 LM 8 PS3 6 FS 4 AA 2 PS2 1
;
/* Count species by site */
proc freq data=have order=freq;
by site;
tables species / out=want;
run;
/* Create bar charts from percentages */
title 'Distribution of species';
proc sgplot data=want;
by site;
format percent 6.2;
yaxis label='Percent' grid;
vbar species / response=percent categoryorder=respdesc datalabel;
run;
title;
I very much agree with @FreelanceReinh
If you keep in mind that the plots generated by summary or analysis procedures, like Freq, Univariate or any of the regressions, are intended as aids to data exploration and learning about your results and not intended to be "camera ready for publication" you can lower your stress level about trying to force tools to do something they were not intended to to.
Consider from a software design idea. SAS has provided a number of very powerful flexible graphing products in SGplot, SGpanel, SGrender plus Graphics Template Language. Why should they provide all of the tools and associated code into every single other procedure that generates graphs? That would multiply significantly the size of the software and the complexity of maintaining every single procedure that produces graphs. Basic appearance for basic graphs tells you want the data is doing. Use the proper tool to make reporting graphs.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.