Have you ever tried to enhance a report in SAS Visual Analytics by adding an animation? But when you hit play, the graph doesn't animate like you expected? This has been an issue I've run into from time to time when attempting to add animations to a time-series bar chart.
For example, let's imagine that we work for a retail store and we had a store-wide sale last 4th of July that lasted from June 25th though July 13th. During the sale, we collected sale data. Here are a few lines of it:
Looks like we had a very impressive sale! So we add the data to SAS Visual Analytics and make the following time-series bar chart:
By looking at the data, we can see that we have a sas-date variable called "Date". So we know that we should be able to create an animation from this. Therefore, we add "Date" to the "Animation" Role for the bar chart. But when we play the animation, it doesn't look quite right:
The bars in the bar chart appear and then suddenly disappear as the plot animates through the date values. What we WANT to happen is something like this:
To investigate, let's go back and review our source data:
The reason the bars are disappearing is because there's only one "Day of Sale" and "Total Sales" value for each of the dates. So when the animation slider is on July 15th, it displays the bar chart value for "Day 01 - Total Sales: $50". And when the animation slider advances to July 16th, the "Day 02 - Total Sales: $54" value is displayed, but the "Day 01 - Total Sales: $50" value is no longer present. So the Day 01, bar disappears as soon as the Day 02 bar appears.
How can we get the bar chart to behave like we want? We want past date bar values to remain on the plot as the chart animates through the dates. Well, as it turns out it's actually quite simple! All we need to do is dust off our SAS programming skills!
To make the bar chart's values 'stick' as the animation slider advances, we'll need a data structure that looks like this:
Even though this data structure might look a little strange, it's what is needed to prevent the bars from disappearing. Notice how there are repeated records for each "Day of Sale." Those repeated records contain all the dates the sale was taking place in the new variable "Animation_Date." This new variable, "Animation_Date," is what can be used in the bar chart's animate role. In the example above, when the play button is pressed and the animation slider advances though the variable Animation_Date (from the July 25th through July 27th, in this case), the value of "Day 01 - Total Sales: $50" is retained though the entire animation!
How do we get SAS to create this special data structure? It's actually quite simple. It just requires a do-loop and where-clause step.
do i='25Jun2017'd to '13jul2017'd;
format i date9.;
2. Keep only the values where "Animation_Date" is less than or equal to the original "Date" value:
where date le Animation_Date;
Now that our new data structure has been built by the do-loop and where clause, we can feed our new data set into VA and assign the following roles:
Category: Day of Sale
Measure: Total Sales
Group: Sales Performance
We now have a time-series bar chart that animates as expected! The animation slider advances and the past sales values are retained:
This example was created in SAS Visual Analytics 8.2. Attached to this post is
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.
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.