BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Lysegroentblad
Obsidian | Level 7

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:

Lysegroentblad_3-1649065893884.png

 

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:

Lysegroentblad_0-1649065444680.png

Lysegroentblad_1-1649065473514.png

...which is exactly what I want, but i also want the %-values added on top of the bar, like this: 

Lysegroentblad_2-1649065608539.png

(...that chart is from an sgplot)
How do I code the same type of barlabel into my proc freq?

 

Best regards

Maja

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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;

View solution in original post

3 REPLIES 3
FreelanceReinh
Jade | Level 19

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;
ballardw
Super User

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.

Lysegroentblad
Obsidian | Level 7
Hi @FreelanceReinhard and @ballardw
Thank you for your answers. I will have to accept that I shouldn't try to do it all with the PROC FREQ, but only use it for what it was made for.

Best regards

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 1444 views
  • 5 likes
  • 3 in conversation