Solved
Contributor
Posts: 48

# How to plot 3 variables on X-axis.

[ Edited ]

Hello SAS Community,

Thank you in advance for responding.

I was wondering if anyone could help me with plotting 3 variables namely Top Medium and Lowon X axis and Counts of those variables on Y-axis.

Each variable( Top, Medium and Low) are expected to have 0 1 2 3 observations and I want to plot all 3 variables on X axis and the frequency of 0 1 2 3 on Y axis.

``````Data dsn1;
input
UID \$2. Top Medium Low
;
cards;
WW	1	1	1
O	1	1	1
FF	2	1	0
D	2	1	0
Y	1	2	0
BB	0	1	2
R	1	1	1
E	1	2	0
AA	1	1	1
OO	1	1	1
T 	0	0	3
CC	1	0	2
;
run;

proc sgplot data = dsn1;
vbar Top/ stat = freq ;
run;

proc sgplot data = dsn1;
vbar Medium/ stat = freq ;
run;

proc sgplot data = dsn1;
vbar Low/ stat = freq ;
run;``````

Thanks.

Accepted Solutions
Highlighted
Solution
‎10-06-2017 06:47 PM
Posts: 5,133

## Re: How to plot 3 variables on X-axis.

Maybe this (third option)

``````data dsn2;
set dsn1;
length grp \$10;
grp = "Low";
value = low;
output;
grp = "Medium";
value = medium;
output;
grp = "Top";
value = top;;
output;
run;

proc sgplot data = dsn2;
vbar value/ stat = freq group=grp groupdisplay=stack grouporder=data ;
run;

/* Or */
proc sgplot data = dsn2;
vbar value/ stat = freq group=grp groupdisplay=cluster grouporder=data ;
run;

/* Or */
proc sgpanel data=dsn2;
panelby grp / rows=1;
vbar value / stat=freq;
run;``````
PG

All Replies
Super User
Posts: 12,148

## Re: How to plot 3 variables on X-axis.

Perhaps something like this:

```Data dsn1;
input
UID \$2. Top Medium Low
;
cards;
WW	1	1	1
O	1	1	1
FF	2	1	0
D	2	1	0
Y	1	2	0
BB	0	1	2
R	1	1	1
E	1	2	0
AA	1	1	1
OO	1	1	1
T 	0	0	3
CC	1	0	2
;
run;
data dsntrans;
set dsn1;
array a top medium low;
do i = 1 to dim(a);
Grp = vname(a[i]);
value = a[i];
output;
end;
keep uid grp value;
run;

proc sgplot data = dsntrans;
vbar value/ stat = freq group=grp
datalabel
groupdisplay=cluster;
run;```
Contributor
Posts: 48

## Re: How to plot 3 variables on X-axis.

Hi Ballardw,

Thank you for the quick reply and thank you for the solution.
I am sorry if was not clear with my question. Your solution give me an output of 0 1 2 3 on X axis. I was expecting Top medium and Low on X axis and they are further sub grouped into 0 1 2 3.
I am sorry for not being clear.
Posts: 5,133

## Re: How to plot 3 variables on X-axis.

Try these:

``````
data dsn2;
set dsn1;
length grp \$10;
grp = "Low";
value = low;
output;
grp = "Medium";
value = medium;
output;
grp = "Top";
value = top;;
output;
run;

proc sgplot data = dsn2;
vbar value/ stat = freq group=grp groupdisplay=stack grouporder=data ;
run;

/* Or */
proc sgplot data = dsn2;
vbar value/ stat = freq group=grp groupdisplay=cluster grouporder=data ;
run;``````
PG
Contributor
Posts: 48

## Re: How to plot 3 variables on X-axis.

Hi PGStats,

I am sorry if was not clear with my question.
I was expecting Top Medium and Low in X axis which are futher each sub classified into 0 1 2 3. Just like the merging the outputs of my 3 Sgplots.
Highlighted
Solution
‎10-06-2017 06:47 PM
Posts: 5,133

## Re: How to plot 3 variables on X-axis.

Maybe this (third option)

``````data dsn2;
set dsn1;
length grp \$10;
grp = "Low";
value = low;
output;
grp = "Medium";
value = medium;
output;
grp = "Top";
value = top;;
output;
run;

proc sgplot data = dsn2;
vbar value/ stat = freq group=grp groupdisplay=stack grouporder=data ;
run;

/* Or */
proc sgplot data = dsn2;
vbar value/ stat = freq group=grp groupdisplay=cluster grouporder=data ;
run;

/* Or */
proc sgpanel data=dsn2;
panelby grp / rows=1;
vbar value / stat=freq;
run;``````
PG
☑ This topic is solved.