BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
HijB
Fluorite | Level 6

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_ASource_B
111
200
311
411
501
610
701
801
900
1011

 

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%). 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

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
ballardw
Super User

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;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 395 views
  • 3 likes
  • 3 in conversation