BookmarkSubscribeRSS Feed
ccc143
Calcite | Level 5
When I run the following, I get 37.96% for dog under year 1 and 34.57% for dog under year 2.

With the data given, it should be 287.1/(469.9+287.1) = 37.926% for dog under year 1 and 261.6/(494.3+287.1) = 34.61% for dog under year 2.

Am I missing something in the code? Why aren't the percentages coming out right? Please see attached.

Thanks!

data test1;
input year cat $3. num;
datalines;
1 cat 469.9
1 dog 287.1
2 cat 494.3
2 dog 261.6
;
run;

goptions reset=all;

proc gchart data=test1;
vbar year / discrete subgroup=cat
group=year g100 nozero
freq=num type=percent
inside=percent width=20;
run;
quit;
4 REPLIES 4
ballardw
Super User
I think you'll find that the FREQ option truncates fractional values. Re-run your example without the decimal parts of NUM and you'll get the same result.

This calculates the correct percentages but you'll probably not be happy with the vertical axis.


I find most of the times when I want to use weighted statistics that I have to pre-summarize the data prior to display.

proc gchart data=test1;
vbar year / discrete subgroup=cat
sumvar=num
raxis=none
inside=subpct width=20;
run;
quit;

Message was edited by: ballardw Message was edited by: ballardw
ccc143
Calcite | Level 5
Thanks ballardw! Is there a way to maintain the y-axis in percentages though?
ballardw
Super User
The approach I would to get data and axis to agree is to pre-process the data as such.

proc freq data=test1 noprint;
table year*cat/ outpct out=test2;
weight num;
run;

The order of the table variable determines if you want to display row or column percent. The default labels will be ugly as well so provide your own.

proc gchart data=test2;
vbar year / discrete subgroup=cat
sumvar=pct_row
inside=subpct width=20;
label pct_row ='%';
run;
quit;
ccc143
Calcite | Level 5
This is perfect!! Thanks again!!!!

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1102 views
  • 0 likes
  • 2 in conversation