BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ktitler
Calcite | Level 5

I am trying to animate a stacked barchart over time.  As time passes I want the graph to populate on the screen. I want the gif to start with the empty graph and then as time passes fill in the chart horizontally.  

 

 

 

Below is the code to create the stacked barcharts.  

 

 

 

proc import datafile = "C:\Users\...\Desktop\model.xlsx"
dbms = xlsx
out = model
REPLACE;
run;

 

 

data am;
set model;
if time > "12:00"t then delete;
format time timeampm9.;
run;

data pm;
set model;
if time <= "12:00"t then delete;
format time timeampm9.;
run;


data myattrmap;
length linecolor $ 9 fillcolor $ 9;
input ID $ value $ linecolor $ fillcolor $;
datalines;
myid OX yellow yellow
myid SS orange orange
myid WS purple purple
myid ES green green
myid HS lightblue lightblue
myid IS blue blue
myid JH red red
;
run;

 

proc sgplot data = am dattrmap = myattrmap;
title 'Deployment Model AM';
vbar time /response = buses stat = sum group = Group datalabel seglabel attrid=myid;
xaxis display =(nolabel) valuesrotate=vertical values = ("6:15"t to "09:00"t by "0:05"t);
yaxis grid values = (0 to 40 by 5);
refline "7:30 AM" /
axis = x lineattrs=(pattern=2 thickness = 2 color = green) label = "7:30 AM";
refline "7:50 AM" /
axis = x lineattrs=(pattern=2 thickness = 2 color = red) label = "7:50 AM";
refline "8:20 AM" /
axis = x lineattrs=(pattern=2 thickness = 2 color = lightblue) label = "8:20 AM";
refline "8:50 AM" /
axis = x lineattrs=(pattern=2 thickness = 2 color = blue) label = "8:50 AM";
run;


proc sgplot data = pm dattrmap = myattrmap;
title 'Deployment Model PM';
vbar time /response = buses stat = sum group = Group datalabel seglabel attrid=myid;
xaxis display =(nolabel) valuesrotate=vertical values = ("14:00"t to "16:50:00"t by "0:05"t);
yaxis grid values = (0 to 40 by 5);
refline "2:20 PM" /
axis = x lineattrs=(pattern=2 thickness = 2 color = green) label = "2:20 PM";
refline "2:50 PM" /
axis = x lineattrs=(pattern=2 thickness = 2 color = red) label = "2:50 PM";
refline "3:20 PM" /
axis = x lineattrs=(pattern=2 thickness = 2 color = lightblue) label = "3:20 PM";
refline "3:50 PM" /
axis = x lineattrs=(pattern=2 thickness = 2 color = blue) label = "3:50 PM";
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You can do this by creating a short macro and using a WHERE statement to control the time shown on the graph. Here's a fully worked example that you can use to see how this is done and then you can modify your code accordingly. Note that you need SAS 9.4 + to have this work. HTH.

 

/*--This program requires SAS 9.4--*/

%macro lineAnnually(dsn=, start=, end=);
   %let start=%sysfunc(inputn(&start,anydtdte9.));
   %let end=%sysfunc(inputn(&end,anydtdte9.));
   %let dif=%sysfunc(intck(month,&start,&end));
     %do i=0 %to &dif;
      %let date=%sysfunc(intnx(month,&start,&i,b),date9.);

         proc sgplot data=sashelp.stocks; 
             
            *used to control the time period shown on the graph;
             where date > &start and date < "&date"d and stock='IBM'; 
            
             series x=date y=open; 
             xaxis interval=month min='01Jan1990'd max='01Jan2007'd; 
             yaxis values=(0 to 200 by 50); 
             footnote j=l 'Created using the SGPLOT Procedure, using SAS UE'; 
         run; 

        quit;
    %end;
%mend lineAnnually;



/*--Create animation--*/
options papersize=('11 in', '7 in') 
printerpath=gif 
animation=start 
animduration=0.1 
animloop=yes 
noanimoverlay
nodate;
ods printer file='/folders/myfolders/LineGraph.gif';

ods graphics / width=10in height=6in imagefmt=GIF;

%lineAnnually(dsn=sashelp.stocks , start=01Jan1990, end=01Jan2007);

options printerpath=gif animation=stop;
ods printer close;

View solution in original post

1 REPLY 1
Reeza
Super User

You can do this by creating a short macro and using a WHERE statement to control the time shown on the graph. Here's a fully worked example that you can use to see how this is done and then you can modify your code accordingly. Note that you need SAS 9.4 + to have this work. HTH.

 

/*--This program requires SAS 9.4--*/

%macro lineAnnually(dsn=, start=, end=);
   %let start=%sysfunc(inputn(&start,anydtdte9.));
   %let end=%sysfunc(inputn(&end,anydtdte9.));
   %let dif=%sysfunc(intck(month,&start,&end));
     %do i=0 %to &dif;
      %let date=%sysfunc(intnx(month,&start,&i,b),date9.);

         proc sgplot data=sashelp.stocks; 
             
            *used to control the time period shown on the graph;
             where date > &start and date < "&date"d and stock='IBM'; 
            
             series x=date y=open; 
             xaxis interval=month min='01Jan1990'd max='01Jan2007'd; 
             yaxis values=(0 to 200 by 50); 
             footnote j=l 'Created using the SGPLOT Procedure, using SAS UE'; 
         run; 

        quit;
    %end;
%mend lineAnnually;



/*--Create animation--*/
options papersize=('11 in', '7 in') 
printerpath=gif 
animation=start 
animduration=0.1 
animloop=yes 
noanimoverlay
nodate;
ods printer file='/folders/myfolders/LineGraph.gif';

ods graphics / width=10in height=6in imagefmt=GIF;

%lineAnnually(dsn=sashelp.stocks , start=01Jan1990, end=01Jan2007);

options printerpath=gif animation=stop;
ods printer close;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1 reply
  • 561 views
  • 0 likes
  • 2 in conversation