- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I have stacked plots that need total N to be displayed in Vbar label (on top or the bottom of the bar). I've tried xaxistable cat/ stat=freq label="N"; statement in proc sgplot , but it is not giving Total N, instead the Group N stacked in the bar chart. Does anyone share the techniques that would do the task?
data cars;
set sashelp.cars;
if type eq 'Hybrid' then cat=1;
else if type eq 'Sedan' then cat=2;
else if type eq 'Sports' then cat=3;
else if type eq 'SUV' then cat=4;
else if type eq 'Truck' then cat=5;
if cat ne .;
run;
proc sgplot data = cars pctlevel=group;
where origin eq 'Asia' and make in ('Toyota', 'Hyundai', 'Honda');
vbar make / stat=percent group= type seglabel dataskin=pressed;
xaxis discreteorder=data;
yaxis label='Percent';
run;
Expected output is below,With N= labels
Thank you!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Use vbarparm statement to instead vbar, and use series statement to add data label.
proc freq data=cars noprint; where origin eq 'Asia' and make in ('Toyota', 'Hyundai', 'Honda'); tables make*type/out=cars_computed outpct; run; data cars_computed; set cars_computed; by make; pct_row=pct_row/100; dummy=1; if last.make then do; deno=round(count/pct_row); dlab=cats('N=',deno); end; run; proc sgplot data = cars_computed; vbarparm category=Make response=pct_row/group=type seglabel dataskin=pressed; series x=Make y=dummy/datalabel=dlab datalabelpos=top lineattrs=(thickness=0) markerattrs=(size=0); xaxis discreteorder=data; yaxis label='Percent' offsetmax=0.05; format pct_row percent12.1; run;
The result:
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Use vbarparm statement to instead vbar, and use series statement to add data label.
proc freq data=cars noprint; where origin eq 'Asia' and make in ('Toyota', 'Hyundai', 'Honda'); tables make*type/out=cars_computed outpct; run; data cars_computed; set cars_computed; by make; pct_row=pct_row/100; dummy=1; if last.make then do; deno=round(count/pct_row); dlab=cats('N=',deno); end; run; proc sgplot data = cars_computed; vbarparm category=Make response=pct_row/group=type seglabel dataskin=pressed; series x=Make y=dummy/datalabel=dlab datalabelpos=top lineattrs=(thickness=0) markerattrs=(size=0); xaxis discreteorder=data; yaxis label='Percent' offsetmax=0.05; format pct_row percent12.1; run;
The result:
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@whymath ,
That's awesome!
Thank you for your help! I like the way totals and percent to be created in data step.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I would suggest one modification to this solution: try using the TEXT plot instead of the SERIES plot. That way, you don't have to turn off the plot features just to get the labels.
Put this in place of the SERIES plot statement:
text x=make y=dummy text=dlab / position=top;
Hope this helps!
Dan
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content