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.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 1234 views
  • 0 likes
  • 2 in conversation