Solved
Contributor
Posts: 27

# plotting two variables in one bar

Hi,

In proc sgplot with either hbar or vbar statement, how to bring information from two variables to one bar.  For eg. var1=5 and var2=10.  Other obs could be var1=2 and var2=5.  The plot should show one bar for each obs and each representaive bar should show informaion from both var1 (red) and var2 (blue).  The group won't work here.

Accepted Solutions
Solution
‎04-09-2017 10:56 PM
Super User
Posts: 22,874

## Re: plotting two variables in one bar

Posted in reply to Cynthia_sas

If you want the bars stacked change GROUPDISPLAY from CLUSTER to STACK.

All Replies
Contributor
Posts: 27

## Re: plotting two variables in one bar

Basically need to show two 'response' values into one bar with different color.
Super User
Posts: 22,874

## Re: plotting two variables in one bar

You'll need to restructure you data into how SAS expects it to be for a stacked bar chart.

This means combining the variables and included a new variable to denote the source/group.

Contributor
Posts: 27

## Re: plotting two variables in one bar

You mean create a new variable by adding var1+var2.  But in the vbar statement, what will be go in response=?? and group= combined?? groupdisplay=stack?  Thanks.

Super User
Posts: 22,874

## Re: plotting two variables in one bar

If you want sample code post sample data.

Contributor
Posts: 27

## Re: plotting two variables in one bar

Here is a sample data. Need to show var1 and var2 of each customer in the same bar.

data fitness;
input cus_id country \$ var1 var2;
datalines;
102 Can 3.73 11.2
105 USA 20.3 9.1
123 USA 4.6 16.4
156 Can 8.1 0.3
177 USA 0.5 13.4
;
run;
SAS Super FREQ
Posts: 9,261

## Re: plotting two variables in one bar

Hi:

Restructuring the data will get you want you want. You just need to make 2 observations for every observation in the input data.

cynthia

Solution
‎04-09-2017 10:56 PM
Super User
Posts: 22,874

## Re: plotting two variables in one bar

Posted in reply to Cynthia_sas

If you want the bars stacked change GROUPDISPLAY from CLUSTER to STACK.

Contributor
Posts: 27

## Re: plotting two variables in one bar

Thanks Reeza and Cynthia..!  Did not get the restructuing part, it is clear now.

Super User
Posts: 22,874

## Re: plotting two variables in one bar

The correct answer should be Cynthia's post.

☑ This topic is solved.

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

Discussion stats
• 9 replies
• 266 views
• 0 likes
• 3 in conversation