BookmarkSubscribeRSS Feed

How to Create an Animated BarChart using SGPlot Procedure

Started ‎03-22-2015 by
Modified ‎10-05-2015 by
Views 4,511

The ODS Graphics Procedures are a great way to create high quality graphics with easy to use syntax.  These procedures support a variety of plots, which can be combined to create your graph.


In this post, I will create an animated bar chart using the SGPLOT Procedure in SAS 9.40M2 to depict the population distribution by age group.




The first step is to get the summarized data set. Please note that the data used in this example is fictitious and is used to demonstrate the SGPlot example. For this plot, the data set contains the percent of US population and is divided into cohorts of different age groups. The group variable in this case is sex. In order to make the bars for the different group values appear on either sides, I have transformed the data for the population of 'MALES' to be negative so that both groups ('MALES' and 'FEMALES') can appear on either side of 0. A picture format is applied to display the transformed data as positive numbers in the graph. I have also created the necessary variables to get the gender information displayed as text on the bar chart.


Here is a snippet of the final input data set:




The next step is to create the plot using SGPLOT procedure. I have overlaid HBARPARM (horizontal bar chart) and SCATTER (scatterplot) statements to get the bar chart along with the labels for the gender. Scatter statements are used for displaying the gender information in the graph. The STYLEATTRS statement is used to control the visual attributes of the group variable. XAXIS|YAXIS statements are supplied to get the desired axes.


A handy feature of the SG procedures is that it supports BY-group processing and this is useful in generating multiple graph outputs that are used to play out the animation. Each of these outputs from the BY value acts as a key frame in the animation. I added a BY-group variable to generate the plots for every 5 years starting from 1950 to 2060. Hence, the procedure creates a bar chart for each value of the BY-group variable.


Finally, the series of images that are generated (based on the different values of the BY variable) can be played out using SAS options and ODS statements and voila, we have an animated chart!


Click Chart to view animation.



Here is the SGPlot program:


proc sgplot data=popsort  noautolegend noborder nowall;

  styleattrs datacolors=(VIG VIGB) datacontrastcolors=(VIG VIGB);

  hbarparm category=agegp response=poppct /group=sex nooutline name="bar"

     dataskin=none  fillattrs=(transparency=0.1) groupdisplay=stack;

  scatter y=agegp x=x/markerchar=val1 markercharattrs=(color=VIGB size=10pt


  scatter y=agegp x=x/markerchar=val2 markercharattrs=(color=VIG size=10pt


  yaxis reverse grid colorbands=even display=(noticks nolabel)


  xaxis values=(-6 to 6 by 1) display=(noticks nolabel) ;

  by year;



In the next post, I will discuss some improvements over the current version of this animation.



Great post! Thanks for this!

I think you have a typo somewhere for your female data in the 5-9 age range for 2055. It is a tad large, and doesn't reflect the number for the younger age group in 2050 or the older age in 2060.

It was about a percent higher. Thanks for pointing this out!

Version history
Last update:
‎10-05-2015 03:28 PM
Updated by:



Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Labels
Article Tags