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

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; 
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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;

x.png

View solution in original post

18 REPLIES 18
Ksharp
Super User

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;
Anita_n
Pyrite | Level 9

@Ksharp thanks for the Code but the bars aren't labelled

Jay54
Meteorite | Level 14

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.

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/grstatproc/n0mc5dtithid5mn13c54dlgaceq3.htm#p...

 

If you want label for each bar segment, use SEGLABEL option.

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/grstatproc/n0mc5dtithid5mn13c54dlgaceq3.htm#n...

https://blogs.sas.com/content/graphicallyspeaking/2013/09/20/stacked-bar-chart-with-segment-labels/

 

Anita_n
Pyrite | Level 9

@Jay54 that displays the bars side by side. I wouldn't like to display them as clusters 

Jay54
Meteorite | Level 14

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

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.

 

Anita_n
Pyrite | Level 9

@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

GraphGuy
Meteorite | Level 14

Can you post your data, so we can experiment with it?

 

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

x.pngYou want this one ?

Anita_n
Pyrite | Level 9

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

x.png

Anita_n
Pyrite | Level 9

okay, is this not possible with vbar

Ksharp
Super User

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 .

Ksharp
Super User

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;
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
  • 18 replies
  • 4165 views
  • 5 likes
  • 6 in conversation