- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I am comparing two sources of information, and I want to create a bar chart to demonstrate these differences.
My data looks like this:
ID | Source_A | Source_B |
1 | 1 | 1 |
2 | 0 | 0 |
3 | 1 | 1 |
4 | 1 | 1 |
5 | 0 | 1 |
6 | 1 | 0 |
7 | 0 | 1 |
8 | 0 | 1 |
9 | 0 | 0 |
10 | 1 | 1 |
I want to show a barchart where one bar is for Source_A and it shows the percentage/frequency of the value 1 (50%), and another bar in the same figure for Source_B value 1 percentage/frequency (70%).
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Part of the issue here is that SAS expects the data in a particular layout or arrangement in order for its PROCs to work. That layout is called the "long" data set, where each observation is its own row. Values of Source are in a variable called Source and not in the variable name. This is true for almost all data analysis PROCs, not just plotting.
So first, you must re-arrange your data into the long arrangement. Then plotting is simple.
data re_arrange;
set have;
source='A';
value=source_a;
output;
source="B";
value=source_b;
output;
drop source_a source_b;
run;
proc sgplot data=re_arrange;
vbar source/response=value stat=mean;
yaxis label='Percent' min=0 max=1 valuesformat=percent8.0;
run;
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Part of the issue here is that SAS expects the data in a particular layout or arrangement in order for its PROCs to work. That layout is called the "long" data set, where each observation is its own row. Values of Source are in a variable called Source and not in the variable name. This is true for almost all data analysis PROCs, not just plotting.
So first, you must re-arrange your data into the long arrangement. Then plotting is simple.
data re_arrange;
set have;
source='A';
value=source_a;
output;
source="B";
value=source_b;
output;
drop source_a source_b;
run;
proc sgplot data=re_arrange;
vbar source/response=value stat=mean;
yaxis label='Percent' min=0 max=1 valuesformat=percent8.0;
run;
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
SAS does have Proc Transpose that often is helpful in rearranging data:
data have; input ID Source_A Source_B; datalines; 1 1 1 2 0 0 3 1 1 4 1 1 5 0 1 6 1 0 7 0 1 8 0 1 9 0 0 10 1 1 ; proc transpose data=have out=trans; by id; run; proc sgplot data=trans; vbar _name_ / response=col1 state=mean; run;
And different types of values may call for slightly different plots:
proc sgplot data=trans; vbar _name_ / group=col1 ; label _name_ ='Source' col1='Value' ; run;
or
proc sgplot data=trans; vbar _name_ / group=col1 groupdisplay=cluster; label _name_ ='Source' col1='Value' ; run;