DATA Step, Macro, Functions and more

Simple bar chart (multiple variables)

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Simple bar chart (multiple variables)

[ Edited ]

I am able to create many types of bar charts in SAS but the simplest is escaping my grasp.

 

A subset of my data looks somewhat like this:

 

Year   Var1  Var2  Var3

2004   0.50  0.38  0.93

2005   0.40  0.22  0.88

 

I want to create (for each year) a simple bar chart that has 3 bars, one for each variable and along the same axis for its respective value, but it seems like I am able to plot everything except what I'm trying to accomplish. This is easily done in Excel but I am automating the process since I need to make many charts (I need no assistance with automation). I have tried using Year as one of the variables (after limiting my plotting to certain years using a where statement) but still to no avail. Any advice?

 


Example.png

Accepted Solutions
Solution
‎06-28-2017 06:40 PM
Super User
Posts: 19,877

Re: Simple bar chart (multiple variables)

Why not transpose the data to a long format, via PROC TRANSPOSE. Probably the easiest method. 

 

If you're using SAS Graph see the examples here

http://robslink.com/SAS/democd6/aaaindex.htm

 

Otherwise, see the SGPLOT examples in the graph gallery

https://support.sas.com/sassamples/graphgallery/PROC_SGPLOT.html

 

If you navigate up from either of those links you'll see more examples. 

 

View solution in original post


All Replies
Solution
‎06-28-2017 06:40 PM
Super User
Posts: 19,877

Re: Simple bar chart (multiple variables)

Why not transpose the data to a long format, via PROC TRANSPOSE. Probably the easiest method. 

 

If you're using SAS Graph see the examples here

http://robslink.com/SAS/democd6/aaaindex.htm

 

Otherwise, see the SGPLOT examples in the graph gallery

https://support.sas.com/sassamples/graphgallery/PROC_SGPLOT.html

 

If you navigate up from either of those links you'll see more examples. 

 

New Contributor
Posts: 2

Re: Simple bar chart (multiple variables)

Thank you for the tranpose idea! This is what ultimately worked:

 

proc transpose data=chart out=dataset;
id year;
run;

 

%macro year (inp = );

 

%do i=2001 %to 2012;

title "&inp. &i.";

proc gchart data=dataset;
where _name_ in ("&inp.var1","&inp.var2","&inp.var3");
vbar _name_ / sumvar = _&i.;
run;

%end;

 

%mend year;

Super User
Posts: 19,877

Re: Simple bar chart (multiple variables)

Hmm...Why didn't you just do a BY statement for each year instead?

PROC Star
Posts: 1,760

Re: Simple bar chart (multiple variables)

> A subset of my data looks somewhat like this

 

Excel "data" is not data. It is a actually a report.

 

You need to make it into usable data by creatting proper variables that you can then plot.

See what the data looks like in the links given by @Reeza

 

 

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 663 views
  • 0 likes
  • 3 in conversation