'I need to create a simple pie chart in SAS from a Proc SQL Table
I need to create a pie chart showing gender distribution for a state pulled through a macro variable. The code for the sql table looks like this:
proc sql;
create table pie_data as
select distinct mean(Men2017/(Men2017 + Women2017)) format=comma10.2 as Men2017, mean(Women2017/(Men2017 + Women2017)) format=comma10.2 as Women2017
from project.county_data
where State= &StateValue;
quit;
The output of the table looks like this:
Men2017 Women2017
0.49 0.51
I don't know how to do it through proc gchart since Men2017 and Women2017 are technically separate variables, and I can't manually input the data since it needs to be dynamic. Any suggestions to how I should approach this? I'm new to SAS.
Solution 1:[1]
Use PROC TRANSPOSE to transpose the data
Rough idea, may need to re-name variables:
data pie_data;
input Men2017 Women2017;
cards;
0.49 0.51
;;;;
proc transpose data=pie_data out=pie_data_long;
var MEN2017 WOMEN2017;
run;
proc sgpie data=pie_data_long;
pie col1;
run;
Solution 2:[2]
PROC TRANSPOSE can re-arrange your data so that the values appear in 1 variable. Then you can easily do the pie chart. Add the following after your PROC SQL step.
proc transpose data= pie_data out=pie_transpose name=gender;
var men2017 women2017;
run;
title 'Men and women, 2017';
proc gchart data=pie_transpose;
pie gender / sumvar=col1 noheading;
run;
title;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Reeza |
| Solution 2 | shaun_m |
