BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
A_Kh
Lapis Lazuli | Level 10

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= labelsWith N= labels

 

Thank you!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
whymath
Lapis Lazuli | Level 10

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:

1.png

View solution in original post

4 REPLIES 4
whymath
Lapis Lazuli | Level 10

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:

1.png

A_Kh
Lapis Lazuli | Level 10

@whymath , 

 

That's awesome!

Thank you for your help! I like the way totals and percent to be created in data step. 

DanH_sas
SAS Super FREQ

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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1086 views
  • 6 likes
  • 3 in conversation