Animation using SGPLOT

11

Often we want to visualize the relationship between variables over time.  The understanding of such data can be improved by viewing the animated graph over time.  With SAS 9.4, you can create animated graphs using the new animation options on the OPTIONS statement and the PRINTER destination.

A popular example an animated graph is the GapMinder demo presented by Hans Rosling.  Using the data from the web site, Pratik has created a SAS data set for life expectancy, income and population by country and year.  Here is a bubble plot animation created using the SGPLOT procedure, stepping through the data for each year from 1960 to 2007.

Here is the graph:

The key options used are the following:

options papersize=('5 in', '3 in') printerpath=gif animation=start 
        animduration=0.1 animloop=yes noanimoverlay;
ods printer file='GapMinder.gif';
 
ods graphics / width=5in height=3in imagefmt=GIF;
  Create multiple graphs using SGPLOT procedure;
 
options printerpath=gif animation=stop;
ods printer close;

In this example, I wrote a macro that loops through all the values for "Year", from 1960 to 2006.   To maintain the axis ranges, it is important to set these so they will be consistent over all the frames.  The resulting GIF file can quickly get too large to include in a blog article.  So to keep the size within the limits allowed for a blog article, I used a step of 2 years, and a smaller image size.  The bubbles are colored by region of the world, but I dropped the legend to save space.

The full code is attached below.  Along with animations, bubble skins are supported in SAS 9.4 SGPLOT procedure.

SAS 9.4 Code: GapMinder

The animation can likely be improved by generating intermediate frames by interpolating the data from one year to the next.  That will smooth out the "jerkiness" of the animation where there are large changes from year to year.  Here is an example of data interpolation with the SASHELP.CLASS data set.

SAS 9.4 code: ClassBubble

Share

About Author

Sanjay Matange

Director, R&D

Sanjay Matange is R&D Director in the Data Visualization Division responsible for the development and support of the ODS Graphics system, including the Graph Template Language (GTL), Statistical Graphics (SG) procedures, ODS Graphics Designer and related software. Sanjay has co-authored a book on SG Procedures with SAS/PRESS.

Related Posts

11 Comments

  1. Rick Wicklin

    This is very cool. Has there been a discussion of adding a BY statement to the SGPLOT procedure? (Or adapting the PANELBY statement in SGPANEL?) That would eliminate the need for the macro that calls SGPLOT with a WHERE clause.

  2. Peter Lancashire on

    Animations look cool but I prefer to be able to control the graphic myself so I can understand at my own pace and look at details. A slider / stepper / button control would be the solution for this.

  3. Pingback: How to create a bubble plot in SAS University Edition | SAS Training

  4. Pingback: Create spaghetti plots in SAS - The DO Loop

  5. Pingback: Compute highest density regions in SAS - The DO Loop

  6. Pingback: Create an animation with the BY statement in PROC SGPLOT - The DO Loop

    • Sanjay Matange
      Sanjay Matange on

      Try GROUPORDER=descending. Or, make sure your data has dummy observations with group values in the order you want at the start of the data. The response values of these dummy observations can be made missing.

Leave A Reply

Back to Top