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

Hello

I have a code that generates a bar chart, and I wish that the values of the variables show at the end top/bottom of the bar chart. But, since I am quite new to Sas, I do not know how to do so, and was hoping someone here would be willing to help.

My code is as follow:

OPTIONS ORIENTATION=LANDSCAPE NODATE nobyline; /*nobyline fjerner automatisk genereret by linie*/

ODS PDF File = "xx .pdf"  STYLE=sasWeb;

/* Set the graphics environment */

goptions reset=all border cback=white

           htitle=20pt;

Proc Sgplot Data = Return;

hbar NodeName /

  RESPONSE = ReturnATD_NodeName

  discreteoffset = -0.2

  Barwitdt=0.4

;

hbar NodeName /

  RESPONSE = AfkastATD_BM

  Discreteoffset = 0.2

  Barwidth = 0.4

  Transparency = 0.5

  ;

  label ReturnATD_NodeName = "Return"

  ReturnATD_BM = "BM"

  NodeName = "Asset";

  run;

  TITLE; FOOTNOTE;

GOPTIONS RESET=ALL;

ODS PDF CLOSE;

I have attached two files: The first one (Have) being that one my code generates, and the second one (Want) being the way I would like the first one to look (regarding the values at the end).

Thank you for your time and help Smiley Happy

1 ACCEPTED SOLUTION

Accepted Solutions
Andre
Obsidian | Level 7

Bruce,

Goptions has nothing to do with proc sgplot

thus suppress those two instructions

then try to use a datalabel  as option in your hbar instructions

Here to show you some code for another example

HTH

Andre

data a.fig1;
informat femmes hommes numx3.1;
input Age $5.    Femmes    Hommes ;
cards;
< 25   4,9 5,8
25-29   3,4 5,0
30-34   2,1 3,5
35-39   3,5 5,5
40-49   5,7 9,2
run;
proc sgplot data=a.fig1 ;*noautolegend;
vbar age / response=femmes /*stat=sum*/ nostatlabel datalabel
        discreteoffset=-0.2 barwidth=0.4 fillattrs=(color=thistle) name="f";
vbar age / response=hommes /*stat=sum*/ nostatlabel datalabel
           discreteoffset=+0.2 barwidth=0.4 fillattrs=(color=turquoise) name="h";
yaxis grid label='En %' values=(1 to 10 by 1);
keylegend "f" "h" /across=2  down=1 location=inside  position=topleft title="" ;
run;

View solution in original post

5 REPLIES 5
Andre
Obsidian | Level 7

Bruce,

Goptions has nothing to do with proc sgplot

thus suppress those two instructions

then try to use a datalabel  as option in your hbar instructions

Here to show you some code for another example

HTH

Andre

data a.fig1;
informat femmes hommes numx3.1;
input Age $5.    Femmes    Hommes ;
cards;
< 25   4,9 5,8
25-29   3,4 5,0
30-34   2,1 3,5
35-39   3,5 5,5
40-49   5,7 9,2
run;
proc sgplot data=a.fig1 ;*noautolegend;
vbar age / response=femmes /*stat=sum*/ nostatlabel datalabel
        discreteoffset=-0.2 barwidth=0.4 fillattrs=(color=thistle) name="f";
vbar age / response=hommes /*stat=sum*/ nostatlabel datalabel
           discreteoffset=+0.2 barwidth=0.4 fillattrs=(color=turquoise) name="h";
yaxis grid label='En %' values=(1 to 10 by 1);
keylegend "f" "h" /across=2  down=1 location=inside  position=topleft title="" ;
run;

Bruce123
Calcite | Level 5

Hey

Thank you for the informatoin and thank you for your help. It solved my problem Smiley Happy

Jay54
Meteorite | Level 14

If you have SAS 9.3 or later, you will get better results if you use a "Group" data structure, instead of "Multi-Variable".  Create a "Group" column that has "ReturnATD_NodeName" and "AfkastATD_BM" values with a "Value" column that has the corresponding value.  Now, you can use the GROUP= option on the HBAR that will give you better results.

Use DATALABEL option to see the bar values at the top of the bar.

Bruce123
Calcite | Level 5

Hello

Andre's solution solved my problem, but thank you for your reply Smiley Happy

Andre
Obsidian | Level 7

Bruce

Sanjay answer is also completely valid

to show you the difference see this code

BUT FIRST observe you  must have another type of data construction

data a.fig1A;

informat   pourc numx3.1;

input Age $11. +5 gr $1. +1 pourc ;

cards;

moins de 25     1 4,9

25-29 ans       1 3,4

30-34 ans       1 2,1

35-39 ans       1 3,5

40-49 ans       1 5,7

moins de 25     2 5,8

25-29 ans       2 5,0

30-34 ans       2 3,5

35-39 ans       2 5,5

40-49 ans       2 9,2

run;

proc format;

value $gr   1="Femmes" 2="Hommes" ;run;


proc sgplot data=a.fig1a ;*noautolegend;

title "Figure 1 blah blah selon l'âge (%) ";

vbar age / response=pourc group=gr stat=sum groupdisplay=cluster  datalabel;

yaxis grid label='En %' values=(1 to 10 by 1);

format gr $gr.;

run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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