BookmarkSubscribeRSS Feed
kpro1234
Fluorite | Level 6

I am using this code to get percent of bars within group (data_column_C), but what I am getting is percent within data_column_A category.

 

Basically I want the percents to sum up to 100% for data_column_C and have individual bar by data_column_A.

 

proc sgplot data=data_table pctlevel=group;
vline data_column_A /response=data_column_B stat=mean limitstat=clm markers group=data_column_C lineattrs=(thickness=2);
vbar data_column_A / response=data_column_B y2axis transparency=0.75 group=data_column_C groupdisplay=cluster stat=percent;
run;
10 REPLIES 10
DanH_sas
SAS Super FREQ

If I'm interpreting your request correctly, you really want to set PCTLEVEL=GRAPH (which is the default). Give that a try and see if the numbers are what you expect.


Thanks!

Dan

kpro1234
Fluorite | Level 6
I had tried that, when I do PCTLEVEL=GRAPH I get percents for the entire data.

If I do PCTLEVEL=GRAPH and then add a BY DATA_COLUMN_C statement after VBAR, then I get what I need but in two different graphs. But I am trying to have them both in one GRAPH.

Trying to get distribution of data in PERCENT but the percents should add up to 100 by the GROUP variable.
DanH_sas
SAS Super FREQ

Can you post a picture of what you're getting without the BY statement?

kpro1234
Fluorite | Level 6

 What I want is that the blue bars sum up to 100% and the pink bars also sum up to 100%. The variable name differentiating them is DATA_COLUMN_C in my code above. 0-14 are DATA_COLUMN_A values and DATA_COLUMN_B is the response variable.

 

 Capture.PNG

DanH_sas
SAS Super FREQ

The way you can do this is to transpose the data such that the values in DATA_COLUMN_B are put into DATA_COLUMN_B1 and DATA_COLUMN_B2, depending on the value in DATA_COLUMN_C. Then, your plot becomes 2 vbar sttements with stat=mean and 2 vline statements with stat=percent. The PCTLEVEL option will not be required.

 

Hope this helps!

Dan

kpro1234
Fluorite | Level 6
In this case I have 2 distinct values in DATA_COLUMN_C, but I more in other scenarios. So transforming and repeating should work as you suggested, but I wish there was an option that would make it happen without transforming data. From the documentation PCTLEVEL seemed like was the solution but it is giving percent by DATA_COLUMN_A grouping. Thanks for your responses, will have to do that work around if no other solution.
yin1
Calcite | Level 5

i think this is a much needed function in sgplot.

 

my work-around is to use proc freq to export the percentages and then use sgplot. like

 

proc freq data=xxx noprint;table a*b/out=XXXX outpct;run;

proc sgplot data=XXXX;vbar b /response=pct_row group=a groupdisplay=cluster;run;

rwat1
Fluorite | Level 6
This exact code worked for me. Thank you.
katslau
Fluorite | Level 6

These work around codes work for me. Now, I'm having the percentage display on the bars (datalabel). Is it possible to limit the decimal places to one only. It's displaying 3 and that's too much.

 

Thanks.

 

kz_
Quartz | Level 8 kz_
Quartz | Level 8
This is exactly what I'm trying to do! But it looks like proc sgplot doesn't offer any options.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 6017 views
  • 6 likes
  • 6 in conversation