Data visualization with SAS programming

Inaccuracy of vbar g100 percentage calculations

Reply
Occasional Contributor
Posts: 14

Inaccuracy of vbar g100 percentage calculations

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;
Grand Advisor
Posts: 9,699

Re: Inaccuracy of vbar g100 percentage calculations

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
Occasional Contributor
Posts: 14

Re: Inaccuracy of vbar g100 percentage calculations

Thanks ballardw! Is there a way to maintain the y-axis in percentages though?
Grand Advisor
Posts: 9,699

Re: Inaccuracy of vbar g100 percentage calculations

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;
Occasional Contributor
Posts: 14

Re: Inaccuracy of vbar g100 percentage calculations

This is perfect!! Thanks again!!!!
Post a Question
Discussion Stats
  • 4 replies
  • 258 views
  • 0 likes
  • 2 in conversation