220 likes | 323 Vues
Learn how to place multiple graphs on a single page in SAS using graphics catalogs and macros for easy annotation and labeling. Simplify graph creation and maximize space utilization.
E N D
Tips & Tricks MASUG 02/18/2005
Multiple Graphs on One Page • Often it is useful to put several or even many graphs on one page • Graphs are created first and stored in a SAS graphics catalog • Using proc greplay a template can be defined and graphic elements put into the various panels
Defining a template can be tedious • Here is the template definition code for a sample 3 panel template: tdef mytemp 1 / llx=0 ulx=0 lrx=100 urx=100 lly=0 lry=0 uly=100 ury=100 2 / llx=0 ulx=0 lrx=100 urx=100 lly=65 lry=65 uly=95 ury=95 3 / llx=0 ulx=0 lrx=100 urx=100 lly=35 lry=35 uly=65 ury=65 ; • Rather than define the two opposite corners of a rectangle (to define a panel), SAS wants the user to define all four corners of each rectangle (each panel). • Each (x,y) value is repeated twice, opening the door to human error. (Yes, I’ve seen templates with “crooked” panels.)
PanelDef /********************************************************************* * Name: PanelDef * * Desc: Returns the text for a greplay panel definition. * * Type: Graphics Utility * * History: * * 7/1/04 WJS * *********************************************************************/ %macro paneldef( pnlnbr , leftx , rightx , lowery , uppery , color ); %local return; %let return = &pnlnbr /; %let return = &return llx=&leftx ulx=&leftx lrx=&rightx urx=&rightx; %let return = &return lly=&lowery lry=&lowery uly=&uppery ury=&uppery; %if ( %length( &color ) > 0 ) %then %let return = &return color=&color; &return %mend;
Sample Code goptions nodisplay; proc gplot data=rpt gout=&mygout; plot &var * &timevar = 9 / grid frame autovref vaxis = axis1 haxis = axis2 name = "Top“ ; plot &var * fmonth = fyear / grid frame autovref vaxis = axis1 haxis = axis3 legend = legend1 name = "RRTrks“ ; run; [snip] goptions display; proc greplay nofs tc=mytcat igout=&mygout; tdef mytemp %panelDef( 1 ,0,100,0,100) %panelDef( 2 ,0,100,&lowery1,&uppery1) %panelDef( 3 ,0,100,&lowery2,&uppery2) ; template mytemp; treplay 1:titles 2:Top 3:RRTrks ; run;
Three Macros to Simplify Annotations • The SAS annotate facility give great flexibility in writing or drawing things on graphs – even producing specialized graphs • Typically, simple text is “dropped” onto a graph • Learning annotate is not difficult but takes time
Three simple macros can help • Macro avars creates the basic variable needed in a dataset to annotate a graph • Macro xyhsys specifies a “coordinate system” • Macro alabel defines a text label, placed anywhere you need it
First an example • The following code defines a “graph” that has the titles for a Multi-graph page: data annotitles; %avars %xyhsys( 1,1,1 ) %alabel( 50,100, "&title1",,,, 3.5, centb,E ) %alabel( 50,95, "&title2",,,, 2.5, centb,E ) run; proc ganno annotate=annotitles gout=&mygout name='titles'; run;
avars /*********************************************************** * Name: avars * * Desc: Annotate macro to declare and size standard * * annotate dataset variables * * Type: Graphics Utility - Annotate Macro * * Walt Smith May 10, 2001 * ***********************************************************/ %macro avars; length color function style $8; length text $200; length x y 8; %mend;
xyhsys /**************************************************************** * Name: xyhsys * * Desc: Annotate macro to set the value of three annotate * * variables: xsys, ysys, and hsys. * * Type: Graphics Utility - Annotate Macro * *---------------------------------------------------------------* * ============================================= * * | Table of Coordinate System Codes | * * | (See SAS/Graph Software pg 476) | * * +-----------+---------+----------+----------+ * * | Area | Units | Absolute | Relative | * * +-----------+---------+----------+----------+ * * | Data | % | 1 | 7 | * * | Area | Values | 2 | 8 | * * +-----------+---------+----------+----------+ * * | Graphics | % | 3 | 9 | * * | Output | Cells | 4 | A | * * | Area | | | | * * +-----------+---------+----------+----------+ * * | Procedure | % | 5 | B | * * | Output | Cells | 6 | C | * * | Area | | | | * * ============================================= * ****************************************************************/ %macro xyhsys( xsys , ysys , hsys ); xsys = "&xsys"; ysys = "&ysys"; hsys = "&hsys"; %mend;
alabel /*********************************************************** * Name: alabel * * Desc: Annotate macro to place text on a graph * * Type: Graphics Utility - Annotate Macro * * * * Walt Smith November 30, 1999 * * 5/8/02 WJS Modify logic for pos parm * ***********************************************************/ %macro alabel (x,y,txt,coltxt,ang,rot,size,font,pos); %if ( %length( &x ) >0 ) %then x = &x %str(;); %if ( %length( &y ) >0 ) %then y = &y %str(;); %if ( %length( &ang ) >0 ) %then angle = &ang %str(;); %if ( %length( &rot ) >0 ) %then rotate = &rot %str(;); %if ( %length( &size ) >0 ) %then size = &size%str(;); %if ( %length( &txt ) >0 ) %then text = &txt %str(;); %if ( %length( &font ) >0 ) %then style = "&font"%str(;); %if ( %length( &pos ) =1 ) %then position = "&pos"%str(;); %else %if ( %length( &pos ) > 1 ) %then position = &pos %str(;); %if %length(&coltxt)>0 %then color = "&coltxt"%str(;); function='label'; output; %mend;
PROC SQL vs. PROC MEANS by John AngSenior Merchandising AnalystAutoZone
Summary Functions in PROC SQL • The following summary functions are available when using PROC SQL: • AVG • COUNT • MIN • MAX • STD • SUM • VAR • The GROUP BY statement is used in conjunction with these summary functions.
PROC SQL Query procsql; create table boo as select hoo, sum(foo) as goo from doo group by hoo; quit;
Output Statistics in PROC MEANS • The following output statistics are available when using PROC SQL: • MEAN • N • MIN • MAX • STD • SUM • VAR • The OUTPUT statement is used in conjunction with these output statistics.
PROC MEANS Query procmeans data=doo sum mean noprint; class hoo; var foo; output out=boo sum=goo; run;
Comparison of SAS Log 135 proc sql; 136 create table boo as 137 select hoo, sum(foo) as goo 138 from doo 139 group by hoo; NOTE: Table WORK.BOO created, with 18607 rows and 2 columns. 140 quit; NOTE: PROCEDURE SQL used: real time 1:15.31 cpu time 46.82 seconds 141 proc means data=doo sum mean noprint; 142 class hoo; 143 var foo; 144 output out=boo2 sum=goo; 145 run; NOTE: There were 20532029 observations read from the data set DAD5DATA.SSPOLK. NOTE: The data set WORK.BOO2 has 18608 observations and 2 variables. NOTE: PROCEDURE MEANS used: real time 39.10 seconds cpu time 34.89 seconds
Another Example proc means data=limahl.omd sum noprint; class time; var edge; output out=sting1 sum=toto; run; proc sql; create table sting2 as select time, sum(edge) as toto from limahl.omd group by time; quit;
Another SAS Log 3 proc means data=limahl.omd sum noprint; 4 class time; 5 var edge; 6 output out=sting1 sum=toto; 7 run; NOTE: There were 444186953 observations read from the data set SREGDATA.VITEMADJ_DOWNLOAD. NOTE: The data set WORK.STING1 has 158234 observations and 4 variables. NOTE: PROCEDURE MEANS used: real time 23:16.58 cpu time 12:52.62 8 9 proc sql; 10 create table sting2 as 11 select time, sum(edge) as toto 12 from limahl.omd 13 group by time; NOTE: Table WORK.STING2 created, with 158233 rows and 2 columns. 14 quit; NOTE: PROCEDURE SQL used: real time 50:50.62 cpu time 23:33.05
PROC SQL or PROC MEANS? • PROC SQL code can be more readable and easily understood by other SAS users. • Generally, PROC MEANS will produce the same summarized result in less CPU time than PROC SQL will. • More descriptive statistics (e.g. quantiles) are available with PROC MEANS than with PROC SQL.