When reports are created with multiple procedures that use a BY statement in each step, the default output is organized so that one procedure processes all the BY groups and outputs the tables or graphs, then the next procedure does the same, and so on. In addition, when writing output to the ODS destinations, such as PDF, a Table of Contents is created that shows the order of the output contained in the report. What if the desired report is to organize the output by the value of the BY variable so that each BY group's output appears together? This sample will show two ways to do that - one using ODS DOCUMENT and PROC DOCUMENT and the other using macro logic.
The technique used by DOCUMENT is to create the default output and then use the appropriate statements to reorder the output objects that get created. The technique used by macro is to create a unique macro variable for each value of the BY variable and then loop through the procedure steps in a macro %DO loop.
See related SAS Sample: SN-034626
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
proc datasets lib=work kill;
run;
quit;
/* Example using DOCUMENT */
goptions reset=all device=sasprtc;
proc sort data=sashelp.class out=class;
by sex;
run;
/* create the work document */
ods _all_ close;
ods document name=reorder;
proc tabulate data=class contents='';
by sex;
class age;
var height;
table age, height*mean / contents='Table';
run;
proc gplot data=class;
by sex;
plot age*height / description='Plot';
run;
quit;
ods document close;
/* this output shows the default order */
ods pdf file='before.pdf';
proc document name=reorder;
replay;
run;
quit;
ods pdf close;
ods listing;
/* List the contents of the REORDER */
/* document */
proc document name=reorder;
list / levels=all;
run;
quit;
/* at this point, you need to take a look at */
/* your output window to see the document level names */
/* now start reordering the output using */
/* COPY statements and save to a new document */
proc document name=reorder1;
make toc;
run;
/* this will be the text at the top of the table of contents */
setlabel toc
'Table of contents ordered by Gender';
/* this is for the first value of BY variable SEX */
make F;
run;
setlabel F
'Gender=F';
dir F;
copy \Work.Reorder\Tabulate#1\ByGroup1#1\Table#1 to ^;
copy \Work.Reorder\Gplot#1\ByGroup1#1\Gplot#1 to ^;
dir ^^;
run;
/* this is for the second value of BY variable SEX */
make M;
run;
setlabel M
'Gender=M';
dir M;
copy \Work.Reorder\Tabulate#1\ByGroup2#1\Table#1 to ^;
copy \Work.Reorder\Gplot#1\ByGroup2#1\Gplot1#1 to ^;
dir ^^;
run;
run;
quit;
/* replay the newly ordered output to the PDF destination */
/* this shows the new order based on BY variable values */
ods listing close;
ods pdf file='after.pdf';
proc document name=reorder1;
replay;
run;
quit;
ods pdf close;
ods listing;
/* Example using macro */
proc sort data=sashelp.class out=class;
by sex;
run;
/* create a unique macro variable for each value of */
/* the grouping (BY) variable */
data class;
retain count 0;
set class;
by sex;
if first.sex then do;
count+1;
call symput('val'||left(count),trim(left(sex)));
end;
call symput('last',left(count));
run;
%put &last;
%macro test;
ods listing close;
ods pdf file='test.pdf';
%do i = 1 %to &last;
ods proclabel="Gender=&&val&i";
title "Sex=&&val&i";
proc tabulate data=sashelp.class contents='';
where sex="&&val&i";
class age;
var height;
table age, height*mean / contents="Table";
run;
ods proclabel="Gender=&&val&i";
proc gplot data=sashelp.class;
where sex="&&val&i";
plot age*height / description="Plot";
run;
quit;
%end;
ods pdf close;
ods listing;
%mend test;
%test
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Type: | Sample |
Date Modified: | 2009-07-10 16:29:57 |
Date Created: | 2009-07-10 12:32:38 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | z/OS | 9.1 TS1M3 | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.1 TS1M3 | |||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.1 TS1M3 | |||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.1 TS1M3 | |||
Microsoft Windows XP 64-bit Edition | 9.1 TS1M3 | |||
Microsoft Windows 2000 Advanced Server | 9.1 TS1M3 | |||
Microsoft Windows 2000 Datacenter Server | 9.1 TS1M3 | |||
Microsoft Windows 2000 Server | 9.1 TS1M3 | |||
Microsoft Windows 2000 Professional | 9.1 TS1M3 | |||
Microsoft Windows NT Workstation | 9.1 TS1M3 | |||
Microsoft Windows Server 2003 Datacenter Edition | 9.1 TS1M3 | |||
Microsoft Windows Server 2003 Enterprise Edition | 9.1 TS1M3 | |||
Microsoft Windows Server 2003 Standard Edition | 9.1 TS1M3 | |||
Microsoft Windows XP Professional | 9.1 TS1M3 | |||
Windows Vista | 9.1 TS1M3 | |||
64-bit Enabled AIX | 9.1 TS1M3 | |||
64-bit Enabled HP-UX | 9.1 TS1M3 | |||
64-bit Enabled Solaris | 9.1 TS1M3 | |||
HP-UX IPF | 9.1 TS1M3 | |||
Linux | 9.1 TS1M3 | |||
OpenVMS Alpha | 9.1 TS1M3 | |||
Tru64 UNIX | 9.1 TS1M3 |