New SAS User

Completely new to SAS or trying something new with SAS? Post here for help getting started.
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 1477 views
  • 3 likes
  • 3 in conversation