BookmarkSubscribeRSS Feed
Reeza
Super User
Hi All,

I'm analyzing some survey data and want to present some data in a horizontal bar chart. The data is in terms of agreement, neutral or disagreement. I can get the data in the chart but not in the order I'd like.

I would like to have the Questions order by the Agree %, so the topic that most people agree with is at the top of the charts. Second I would like the 'bars' within the questions arranged from Agree to Neutral to Disagree. I could drop the Disagree without changing the appearance, but thought I'd leave it in for now.

I'm using a sample for Rob Allisons page.

Here's what I have so far.

Thanks!
[pre]

data have;
input question $ agreement $ percent;
cards;
Q1 Agree 0.25
Q1 Neutral 0.30
Q1 Disagree 0.45
Q2 Agree 0.55
Q2 Neutral 0.10
Q2 Disagree 0.35
Q3 Agree 0.45
Q3 Neutral 0.25
Q3 Disagree 0.3
;
run;


proc sort data=have; by agreement descending percent; run;


goptions gunit=pct htitle=4 ftitle="albany amt/bold" htext=2 ftext="albany amt";

axis1 label=none;
axis2 label=none order=(0 to 1 by .1) minor=(number=1) offset=(0,0) value=(h=3pct);



legend1 label=none position=(bottom)
shape=bar(3,3) across=3;

/* pattern v=solid color=red; */
pattern1 v=solid color=cxbd0026; /* kind of a seafoam green */
pattern2 v=solid color=cxc7e9b4; /* this is the hex rgb color for mild blue */
pattern3 v=solid color=cx43a2ca; /* reddish color */

title1 "Percent Agreement";
proc gchart data=have;
hbar question / discrete
type=sum sumvar=percent
subgroup=agreement/* this controls the coloring */
nostats
maxis=axis1 /* midpoint axis */
raxis=axis2 /* response/numeric axis */
autoref /* reflines at every major axis tickmark */
legend=legend1
coutline=same
;
run;

quit;
[/pre]
5 REPLIES 5
Ksharp
Super User
Hi.
How about this:
[pre]
data have;
input question $ agreement $ percent;
cards;
Q1 Agree 0.25
Q1 Neutral 0.30
Q1 Disagree 0.45
Q2 Agree 0.55
Q2 Neutral 0.10
Q2 Disagree 0.35
Q3 Agree 0.45
Q3 Neutral 0.25
Q3 Disagree 0.3
;
run;
[/pre]
data have;



 set have;



 select (question);



   when('Q2') question='  Q2' ;



   when( 'Q3') question= ' Q3';



   when( 'Q1') qwestion='Q1';



   otherwise;



 end;



 select (agreement);



   when('Agree') agreement='  Agree' ;



   when('Neutral') agreement= ' Neutral';



   when('Disagree') agreement='Disagree' ;



   otherwise;



 end;



 



run;



goptions gunit=pct htitle=4 ftitle="albany

amt/bold"
htext=2 ftext="albany
amt"
;



 



axis1 label=none; 



axis2 label=none order=(0 to 1 by .1) minor=(number=1) offset=(0,0) value=(h=3pct);



 



 



 



legend1 label=none position=(bottom) 



 shape=bar(3,3) across=3;



 



/* pattern
v=solid color=red; */



pattern1 v=solid
color=cxbd0026;  /* kind of a seafoam
green */



pattern2 v=solid

color=cxc7e9b4;  /* this is the hex rgb
color for mild blue  */



pattern3 v=solid
color=cx43a2ca;  /* reddish color */



 



title1 "Percent Agreement";



proc gchart data=have;



hbar question / discrete



 type=sum sumvar=percent



 subgroup=agreement/* this controls the coloring */



 nostats



 maxis=axis1 /* midpoint axis */



 raxis=axis2 /*
response/numeric axis */



 autoref /*
reflines at every major axis tickmark
*/



 legend=legend1



 coutline=same



    ; 



run;



 



quit;
[pre]





[/pre]



Ksharp

GraphGuy
Meteorite | Level 14
You can use numeric values for the subgroups, and assign the numbers in the order that produces the desired ordering of the stacked bar subgroups/segments.

Then use a user-defined format to cause the numbers to show up as the desired text in the legend.
GraphGuy
Meteorite | Level 14
Oh - and you can use the same sort of tricks to control the bar order also.
data_null__
Jade | Level 19
To make the question ordering data driven you can summarize, create a new variable and format as in this example. The order and format for response is also created.

[pre]
data have;
input question $ agreement $ percent;
cards;
Q1 Agree 0.25
Q1 Neutral 0.30
Q1 Disagree 0.45
Q2 Agree 0.55
Q2 Neutral 0.10
Q2 Disagree 0.35
Q3 Agree 0.45
Q3 Neutral 0.25
Q3 Disagree 0.3
Q4 Agree 0.25
Q4 Neutral 0.30
Q4 Disagree 0.45
;;;;
run;
proc summary data=have nway;
class percent / descending;
class question;
where agreement eq: 'A';
output out=Qorder(index=(question)) / levels;
run;
data control;
retain fmtname 'Qord' type 'N';
set Qorder;
start = _level_;
label = question;
run;
proc format cntlin=control fmtlib;
value Aord 1='Agree' 2='Neutral' 3='Disagree';
run;

data have;
set have;
set qorder(keep=question _level_) key=question/unique;
Aord = whichC(agreement,'Agree','Neutral','Disagree');
run;

goptions gunit=pct htitle=4 ftitle="albany amt/bold" htext=2 ftext="albany amt";
axis1 label=none;
axis2 label=none order=(0 to 1 by .1) minor=(number=1) offset=(0,0) value=(h=3pct);
legend1 label=none position=(bottom) shape=bar(3,3) across=3 value=(j=left);
pattern1 v=solid color=cxbd0026; /* kind of a seafoam green */
pattern2 v=solid color=cxc7e9b4; /* this is the hex rgb color for mild blue */
pattern3 v=solid color=cx43a2ca; /* reddish color */

title1 "Percent Agreement";
proc gchart data=have;
hbar /*question*/ _level_ / discrete
descending
type=sum sumvar=percent
subgroup=aord /*agreement*/ /* this controls the coloring */
nostats
maxis=axis1 /* midpoint axis */
raxis=axis2 /* response/numeric axis */
autoref /* reflines at every major axis tickmark */
legend=legend1
coutline=same
;
format _level_ qord. aord aord.;
run;
quit;
[/pre]
Reeza
Super User
Thanks Alll, got it working!

Add on to this question, is there an easy way (ie with the options in HBAR) to get the percentages added in to the chart or do I need to use an Annotate dataset?

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
  • 5 replies
  • 1133 views
  • 0 likes
  • 4 in conversation