tips & tricks masug02/18/2005. multiple graphs on one page
TRANSCRIPT
Tips & TricksTips & Tricks
MASUGMASUG
02/18/200502/18/2005
Multiple Graphs on Multiple Graphs on One PageOne Page
Multiple Graphs on One PageMultiple Graphs on One Page
Often it is useful to put several or Often it is useful to put several or even many graphs on one pageeven many graphs on one page
Graphs are created first and stored in Graphs are created first and stored in a SAS graphics cataloga SAS graphics catalog
Using proc greplay a template can be Using proc greplay a template can be defined and graphic elements put defined and graphic elements put into the various panelsinto the various panels
Defining a template can be tediousDefining a template can be tedious
Here is the template definition code for a sample Here is the template definition code for a sample 3 panel template:3 panel template:
tdef mytemptdef mytemp 1 / llx=0 ulx=0 lrx=100 urx=100 lly=0 lry=0 uly=100 ury=1001 / 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=952 / 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 ;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 Rather than define the two opposite corners of a rectangle (to define a panel), SAS wants the user rectangle (to define a panel), SAS wants the user to define all four corners of each rectangle (each to define all four corners of each rectangle (each panel).panel).
Each (x,y) value is repeated twice, opening the Each (x,y) value is repeated twice, opening the door to human error. (Yes, I’ve seen templates door to human error. (Yes, I’ve seen templates with “crooked” panels.)with “crooked” panels.)
PanelDefPanelDef
/*********************************************************************/********************************************************************** Name: PanelDef ** Name: PanelDef ** Desc: Returns the text for a greplay panel definition. ** Desc: Returns the text for a greplay panel definition. ** Type: Graphics Utility ** Type: Graphics Utility ** History: ** History: ** 7/1/04 WJS ** 7/1/04 WJS **********************************************************************/*********************************************************************/%macro paneldef( pnlnbr , leftx , rightx , lowery , uppery , color );%macro paneldef( pnlnbr , leftx , rightx , lowery , uppery , color ); %local return;%local return; %let return = &pnlnbr /;%let return = &pnlnbr /; %let return = &return llx=&leftx ulx=&leftx lrx=&rightx %let return = &return llx=&leftx ulx=&leftx lrx=&rightx
urx=&rightx;urx=&rightx; %let return = &return lly=&lowery lry=&lowery uly=&uppery %let return = &return lly=&lowery lry=&lowery uly=&uppery
ury=&uppery;ury=&uppery; %if ( %length( &color ) > 0 ) %then%if ( %length( &color ) > 0 ) %then %let return = &return color=&color;%let return = &return color=&color;&return&return%mend;%mend;
Sample CodeSample Code goptions nodisplay;goptions nodisplay; proc gplot data=rpt gout=&mygout;proc gplot data=rpt gout=&mygout; plot &var * &timevar = 9 /plot &var * &timevar = 9 / grid frame autovrefgrid frame autovref vaxis = axis1vaxis = axis1 haxis = axis2haxis = axis2 name = "Top“ ;name = "Top“ ; plot &var * fmonth = fyear /plot &var * fmonth = fyear / grid frame autovrefgrid frame autovref vaxis = axis1vaxis = axis1 haxis = axis3haxis = axis3 legend = legend1legend = legend1 name = "RRTrks“ ;name = "RRTrks“ ; run;run;[snip][snip] goptions display;goptions display; proc greplay nofs tc=mytcat igout=&mygout;proc greplay nofs tc=mytcat igout=&mygout; tdef mytemptdef mytemp %panelDef( 1 ,0,100,0,100)%panelDef( 1 ,0,100,0,100) %panelDef( 2 ,0,100,&lowery1,&uppery1)%panelDef( 2 ,0,100,&lowery1,&uppery1) %panelDef( 3 ,0,100,&lowery2,&uppery2) ;%panelDef( 3 ,0,100,&lowery2,&uppery2) ; template mytemp;template mytemp; treplaytreplay 1:titles1:titles 2:Top2:Top 3:RRTrks ;3:RRTrks ; run;run;
Three Macros to Three Macros to Simplify AnnotationsSimplify Annotations
Three Macros to Simplify Three Macros to Simplify AnnotationsAnnotations
The SAS annotate facility give great The SAS annotate facility give great flexibility in writing or drawing things flexibility in writing or drawing things on graphs – even producing on graphs – even producing specialized graphsspecialized graphs
Typically, simple text is “dropped” Typically, simple text is “dropped” onto a graphonto a graph
Learning annotate is not difficult but Learning annotate is not difficult but takes timetakes time
Three simple macros can helpThree simple macros can help
Macro Macro avarsavars creates the basic creates the basic variable needed in a dataset to variable needed in a dataset to annotate a graphannotate a graph
Macro Macro xyhsysxyhsys specifies a specifies a “coordinate system”“coordinate system”
Macro Macro alabelalabel defines a text label, defines a text label, placed anywhere you need itplaced anywhere you need it
First an exampleFirst an example
The following code defines a “graph” The following code defines a “graph” that has the titles for a Multi-graph that has the titles for a Multi-graph page:page:
data annotitles;data annotitles;
%avars%avars
%xyhsys( 1,1,1 )%xyhsys( 1,1,1 )
%alabel( 50,100, "&title1",,,, 3.5, centb,E )%alabel( 50,100, "&title1",,,, 3.5, centb,E )
%alabel( 50,95, "&title2",,,, 2.5, centb,E )%alabel( 50,95, "&title2",,,, 2.5, centb,E )
run;run;
proc ganno annotate=annotitles gout=&mygout proc ganno annotate=annotitles gout=&mygout name='titles';name='titles';
run;run;
avarsavars/***********************************************************/************************************************************ Name: avars ** Name: avars ** Desc: Annotate macro to declare and size standard ** Desc: Annotate macro to declare and size standard ** annotate dataset variables ** annotate dataset variables ** Type: Graphics Utility - Annotate Macro ** Type: Graphics Utility - Annotate Macro ** Walt Smith May 10, 2001 ** Walt Smith May 10, 2001 ************************************************************/***********************************************************/%macro avars;%macro avars; length color function style $8;length color function style $8; length text $200;length text $200; length x y 8;length x y 8;%mend;%mend;
xyhsysxyhsys/****************************************************************/***************************************************************** Name: xyhsys ** Name: xyhsys ** Desc: Annotate macro to set the value of three annotate ** Desc: Annotate macro to set the value of three annotate ** variables: xsys, ysys, and hsys. ** variables: xsys, ysys, and hsys. ** Type: Graphics Utility - Annotate Macro ** Type: Graphics Utility - Annotate Macro **---------------------------------------------------------------**---------------------------------------------------------------** ============================================= ** ============================================= ** | Table of Coordinate System Codes | ** | Table of Coordinate System Codes | ** | (See SAS/Graph Software pg 476) | ** | (See SAS/Graph Software pg 476) | ** +-----------+---------+----------+----------+ ** +-----------+---------+----------+----------+ ** | Area | Units | Absolute | Relative | ** | Area | Units | Absolute | Relative | ** +-----------+---------+----------+----------+ ** +-----------+---------+----------+----------+ ** | Data | % | 1 | 7 | ** | Data | % | 1 | 7 | ** | Area | Values | 2 | 8 | ** | Area | Values | 2 | 8 | ** +-----------+---------+----------+----------+ ** +-----------+---------+----------+----------+ ** | Graphics | % | 3 | 9 | ** | Graphics | % | 3 | 9 | ** | Output | Cells | 4 | A | ** | Output | Cells | 4 | A | ** | Area | | | | ** | Area | | | | ** +-----------+---------+----------+----------+ ** +-----------+---------+----------+----------+ ** | Procedure | % | 5 | B | ** | Procedure | % | 5 | B | ** | Output | Cells | 6 | C | ** | Output | Cells | 6 | C | ** | Area | | | | ** | Area | | | | ** ============================================= ** ============================================= *****************************************************************/****************************************************************/%macro xyhsys( xsys , ysys , hsys );%macro xyhsys( xsys , ysys , hsys ); xsys = "&xsys";xsys = "&xsys"; ysys = "&ysys";ysys = "&ysys"; hsys = "&hsys";hsys = "&hsys";%mend;%mend;
alabelalabel/***********************************************************/************************************************************ Name: alabel ** Name: alabel ** Desc: Annotate macro to place text on a graph ** Desc: Annotate macro to place text on a graph ** Type: Graphics Utility - Annotate Macro ** Type: Graphics Utility - Annotate Macro ** ** ** Walt Smith November 30, 1999 ** Walt Smith November 30, 1999 ** 5/8/02 WJS Modify logic for pos parm ** 5/8/02 WJS Modify logic for pos parm ************************************************************/***********************************************************/%macro alabel (x,y,txt,coltxt,ang,rot,size,font,pos);%macro alabel (x,y,txt,coltxt,ang,rot,size,font,pos); %if ( %length( &x ) >0 ) %then x = &x %str(;);%if ( %length( &x ) >0 ) %then x = &x %str(;); %if ( %length( &y ) >0 ) %then y = &y %str(;);%if ( %length( &y ) >0 ) %then y = &y %str(;); %if ( %length( &ang ) >0 ) %then angle = &ang %str(;);%if ( %length( &ang ) >0 ) %then angle = &ang %str(;); %if ( %length( &rot ) >0 ) %then rotate = &rot %str(;);%if ( %length( &rot ) >0 ) %then rotate = &rot %str(;); %if ( %length( &size ) >0 ) %then size = &size%str(;);%if ( %length( &size ) >0 ) %then size = &size%str(;); %if ( %length( &txt ) >0 ) %then text = &txt %str(;);%if ( %length( &txt ) >0 ) %then text = &txt %str(;); %if ( %length( &font ) >0 ) %then style = "&font"%str(;);%if ( %length( &font ) >0 ) %then style = "&font"%str(;); %if ( %length( &pos ) =1 ) %then position = "&pos"%str(;);%if ( %length( &pos ) =1 ) %then position = "&pos"%str(;); %else %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(;);%if %length(&coltxt)>0 %then color = "&coltxt"%str(;); function='label';function='label'; output;output;%mend;%mend;
PROC SQL vs. PROC PROC SQL vs. PROC MEANSMEANS
by John Angby John AngSenior Merchandising AnalystSenior Merchandising Analyst
AutoZoneAutoZone
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
proc sql; 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
proc means 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 as137 select hoo, sum(foo) as goo138 from doo139 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
89 proc sql;10 create table sting2 as11 select time, sum(edge) as toto12 from limahl.omd13 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.