UCLA Academic Technology Services HomeServicesClassesContactJobs
Help the Stat Consulting Group by giving a gift             
Loading

SAS Code Fragment: Making an Animated Spin Plot in SAS

 

We used SAS to make this 3d animated plot with a SAS feature called the "GIFANIM" driver.  

* make data with predicted values ;
data test;
  * range age from -21 to 21 (centered) ;
  do age = -21 to 21 ;
    * range tenure from -18 to 25 (centered) ;
    do ten = -18 to 25 ;
	  * here is the regression equation ;
      ypred = 47 + 0.0705*age + 0.1820*ten - 0.00779*age*ten ;
      output ;
    end;
  end;
run;

* this is required ;
%macro ENDGIF;
   goptions gepilog='3B'x;
%mend;


%macro IMGSIZE(w=1280, h=1024, dpi=100, rows=43, cols=83);

   %if &dpi<=0 %then
      %put DPI must be greater than zero.;
   %else %do;
      goptions hsize=%sysevalf(&w/&dpi)in vsize=%sysevalf(&h/&dpi)in
               hpos=&cols                 vpos=&rows;
   %end;

%mend IMGSIZE;

%macro ANIMATE;
  %let first=1;
  * shift 6 degrees each frame ;
  %do angle=1 %to 360 %by 6;
      * on first frame, set defaults etc... ;
      %if &first = 1 %then %do;
         goptions reset=all;
		 * name of output gif file;
         filename out "e:\temp\spin.gif";


		 * initiialize size of .gif file ;
         %IMGSIZE(w=570, h=480);

         * set options for gif, like color font ;
         * wait time between frames ;
         goptions dev=gifanim gsfname=out
                  gsfmode=replace ftext=swiss
                  htext=1 gcopies=0 gwait=50  /* changed 25 to 5 */
                  cback=black
                  colors=(WHITE YELLOW GOLD BLUE MAGENTA DAGRAY BROWN);
         * give info for footnote ;
         footnote h=4 j=right "Graph of Age and Tenure on Predicted Y";
         %let first=0;
         %end;
      %else 
      %do;
         goptions gsfmode=append;
      %end;

	  * after last frame, put out final part via "endgif" macro ;
      %if &angle = 360 %then 
      %do;
         %ENDGIF;
      %end;

	  * this makes each frame ;
      proc g3d;
         plot age*ten=ypred/rotate=&ANGLE ctop=yellow cbottom=gold;
         label age    = 'Age of Person'
               ten    = 'Tenure of Person'
               ypred  = 'Predicted Y' ;
       run;
   %end;

%mend;

* this calls the animate macro to make the animated gif ;
%ANIMATE;

How to cite this page

Report an error on this page or leave a comment

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California