Hi all,
Is it possible to specify a response and a different datalabel in sgplot. I mean I wish to display the relative (percent) values on the yaxis but the absolute on that bars. I used the following code but the pecentages are display in both cases. Is it possible to do that anyway?
proc sgplot data =mergedat dattrmap=myattrimap pad=(bottom=5%) noautolegend noborder; xaxis grid type=discrete discreteorder=data; vbar year response=percentvar group=grup attrid=grup datalabel = absolutevar datalabelattrs=(weight=bold) seglabel seglabelattrs=(size=0.25) seglabelfitpolicy=none barwidth=0.9 missing; xaxis display= (nolabel) valueattrs=(family="verdana" size=8pt) fitpolicy=rotate ValuesRotate=Vertical; yaxis values=(0 to 60 by 10) display= (nolabel) valueattrs=(family="verdana" size=8pt); run;
proc sql;
create table want as
select date,var1,sum(var2) as sum,calculated sum/(select sum(var2) from x.test4 where date=a.date) as per format=percent8.2
from x.test4 as a
group by date,var1
order by date,var1;
quit;
data want;
set want;
by date;
if first.date then lag=0;
mean=lag+per/2;
lag+per;
run;
proc sgplot data=want;
vbarparm category=date response=per/group=var1 GROUPDISPLAY=stack;
scatter x=date y=mean / markerchar=sum;
run;
You code looks good . What you want to try ?
proc sql; create table want as select age,sex,sum(weight) as sum,calculated sum/(select sum(weight) from sashelp.class where sex=a.sex) as per format=percent8.2 from sashelp.class as a group by age,sex; quit; proc sgplot data=want; vbarparm category=age response=per/group=sex datalabel=sum; run;
@Ksharp thanks for the Code but the bars aren't labelled
In @Ksharp code, add option GROUPDISPLAY=cluster to VBARPARM stmt see the data labels.
See doc for DATALABEL that says this is displayed only when groups are clustered, not stacked.
If you want label for each bar segment, use SEGLABEL option.
https://blogs.sas.com/content/graphicallyspeaking/2013/09/20/stacked-bar-chart-with-segment-labels/
@Jay54 that displays the bars side by side. I wouldn't like to display them as clusters
In ODS Graphics parlance, side-by-side is called "Clustered". Default group display is stacked. For labels on all stacked segments, use SEGLABEL option.
Example:
title 'Product Sales by Country';
proc sgplot data=sashelp.prdsale noborder;
format actual dollar8.0;
vbar country / response=actual group=product displaybaseline=auto barwidth=0.6
seglabel datalabel dataskin=pressed;
xaxis display=(noline noticks nolabel);
yaxis display=(noline noticks nolabel) grid;
keylegend / location=outside fillheight=10 fillaspect=2 ;
run;
Show the code from the log.
Your code as posted will throw at least one error because of a missing / option indicator.
Data is nice.
@ballardw I have no error in the log. The problem is that it uses the percent instead of the absolute values although I indicated datalabel= absolute value
Can you post your data, so we can experiment with it?
proc sql;
create table want as
select age,sex,sum(weight) as sum,calculated sum/(select sum(weight) from sashelp.class where sex=a.sex) as per format=percent8.2
from sashelp.class as a
group by age,sex
order by age,sex;
quit;
data want;
set want;
by age;
retain lag;
if first.age then lag=0;
mean=lag+per/2;
lag=per;
run;
proc sgplot data=want;
vbarparm category=age response=per/group=sex GROUPDISPLAY=stack;
scatter x=age y=mean / markerchar=sum;
run;
You want this one ?
Hi @all : Thankyou all for your suggestions.
@Ksharp Since I already used vbar, I taught I could continue with it.
@Jay54 I tried that code but it still writes the percentage values in the segment, I wanted instead the absolute values
@GraphGuy I have attached a sample data and my code
proc sgplot data =t.test4 pad=(bottom=5%) noautolegend noborder; xaxis grid type=discrete discreteorder=data; vbar date /response=freq group=var1 attrid=var1 seglabel seglabelattrs=(size=0.25) seglabelfitpolicy=none barwidth=0.6 missing; xaxis display= (nolabel) valueattrs=(family="verdana" size=8pt) fitpolicy=rotate ValuesRotate=Vertical; yaxis display= (nolabel) valueattrs=(family="verdana" size=8pt); run;
proc sql;
create table want as
select date,var1,sum(var2) as sum,calculated sum/(select sum(var2) from x.test4 where date=a.date) as per format=percent8.2
from x.test4 as a
group by date,var1
order by date,var1;
quit;
data want;
set want;
by date;
if first.date then lag=0;
mean=lag+per/2;
lag+per;
run;
proc sgplot data=want;
vbarparm category=date response=per/group=var1 GROUPDISPLAY=stack;
scatter x=date y=mean / markerchar=sum;
run;
okay, is this not possible with vbar
data want;
set x.test4;
by date;
if first.date then lag=0;
mean=lag+freq/2;
lag+freq;
run;
proc sgplot data=want;
vbarbasic date /response=freq group=var1 GROUPDISPLAY=stack;
scatter x=date y=mean / markerchar=var2;
run;
If you really want VBAR ,try this one .
Or could be simpler. I see your data has been orgonized very well.
data want; set x.test4; by date; if first.date then lag=0; mean=lag+freq/2; lag+freq; run; proc sgplot data=want; vbarparm category=date response=freq/group=var1 GROUPDISPLAY=stack; scatter x=date y=mean / markerchar=var2; run;
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.