Data visualization with SAS programming

Producing stacked bar chart in SAS EG

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Producing stacked bar chart in SAS EG

Hello,

 

I'm trying to produce the stacked bar chart using the below data but not able to achieve the desired results. I'm getting the desired graph in the excel without any issue. Please help.

Tenure 1 Yr    2 yr    3 Yr   4 Yr   5 Yr   6 to 10 yr   over 10yrs

prct      9.00     8.00    7.00   6.00  6.00    24.00        41.00

 

I have Attached the graph produced through excel. I'm trying to achieve same using the SAS. Tried proc gchart but not working.

 

Thanks.


Accepted Solutions
Solution
‎03-09-2018 12:13 PM
SAS Super FREQ
Posts: 1,263

Re: Producing stacked bar chart in SAS EG

SAS does not work like Excel.  You will need to transform your data structure from multi-column to classified.  You need data as follows:

 

Category   Tenure  Value

1                1 Yr      9.0

1                2 Yr      8.0

 

and so on.  Then, you can use the following statement:

 

proc sgplot data= <your dataset>;

  vbar  Category  / Response=value Group=Tenure;

run;

 

You can have additional categories to get multiple stacked bars.

 

See:  https://blogs.sas.com/content/graphicallyspeaking/2013/09/07/bar-charts-with-stacked-and-cluster-gro...

View solution in original post


All Replies
Solution
‎03-09-2018 12:13 PM
SAS Super FREQ
Posts: 1,263

Re: Producing stacked bar chart in SAS EG

SAS does not work like Excel.  You will need to transform your data structure from multi-column to classified.  You need data as follows:

 

Category   Tenure  Value

1                1 Yr      9.0

1                2 Yr      8.0

 

and so on.  Then, you can use the following statement:

 

proc sgplot data= <your dataset>;

  vbar  Category  / Response=value Group=Tenure;

run;

 

You can have additional categories to get multiple stacked bars.

 

See:  https://blogs.sas.com/content/graphicallyspeaking/2013/09/07/bar-charts-with-stacked-and-cluster-gro...

New Contributor
Posts: 4

Re: Producing stacked bar chart in SAS EG

[ Edited ]
Posted in reply to Sanjay_SAS

Thanks Sanjay. I was trying it in different way like below. I was able to stack the bars but I'm seeing gap between the bars. Not sure why.

 

proc sgplot data=totacct_tenure2;
title 'Distribution of Accounts by Tenure';
series x= Tenure y='1 yr'n /lineattrs=(color= Blue thickness=30 ) ;
series x= Tenure y='2 yr'n / lineattrs=(color= '#13478C' thickness=15 );
series x= Tenure y='3 yr'n / lineattrs=(color='#90EE90' thickness=15);
series x= Tenure y='4 yr'n / lineattrs=(color= yellow thickness=15);
series x= Tenure y='5 yr'n / lineattrs=(color= black thickness=15);
series x= Tenure y='6 to 10 yrs'n / lineattrs=(color=brown thickness=15);
series x= Tenure y='over 10 yrs'n / lineattrs=(color= pink thickness=15);
keylegend / location=Outside Position=right down=7 ;
yaxis label= "Percentage" grid values=( 0 to 100 by 10 ) ;
Xaxis label="% of Accounts";
run;

SAS Super FREQ
Posts: 1,263

Re: Producing stacked bar chart in SAS EG

I can't guess why you are using a SERIES statement to create stacked bars.  See "Getting Started" section in Graphically Speaking for many examples of how SAS procedures work.  It may not help to try to use Excel methodology with SAS.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 93 views
  • 0 likes
  • 2 in conversation