Published on :
Reporting SASHELP

Creating an ODS document with multiple outputs

This code is also available in: Deutsch Español Français
Awaiting validation
The program begins by creating three work tables: `class`, `shoes`, and `cars`, based on the corresponding tables in the SASHELP library. The `shoes` table is filtered to keep only 'Canada' and 'Pacific' regions. Then, it closes all open ODS destinations and initiates a new ODS document named `doc_results`. Several reports are generated with `PROC REPORT` on the different tables, some of which are produced dynamically via a `%loopTroughMake` macro that iterates through car makes. A graph is also added with `PROC SGPLOT`. Finally, the ODS document is closed, consolidating all outputs into a single file.
Data Analysis

Type : SASHELP


Data comes exclusively from the SASHELP library (class, shoes, cars tables).

1 Code Block
DATA STEP Data
Explanation :
Creates the `class` table in the WORK library by copying `sashelp.class` and adding a constant column `const`.
Copied!
1DATA class;
2 SET sashelp.class;
3 const = 1;
4RUN;
2 Code Block
PROC SORT
Explanation :
Sorts the `class` table by `const` and `sex` variables.
Copied!
1PROC SORT DATA=class; BY const sex;RUN;
3 Code Block
DATA STEP Data
Explanation :
Creates the `shoes` table by filtering `sashelp.shoes` to keep only 'Canada' and 'Pacific' regions.
Copied!
1DATA shoes;
2 SET sashelp.shoes;
3 WHERE region in ('Canada', 'Pacific');
4RUN;
4 Code Block
PROC SORT
Explanation :
Sorts the `shoes` table by `region` and `product`.
Copied!
1PROC SORT DATA=shoes; BY region product; RUN;
5 Code Block
DATA STEP Data
Explanation :
Creates the `cars` table by copying `sashelp.cars` and adding a constant column `const`.
Copied!
1DATA cars;
2 SET sashelp.cars;
3 const = 1;
4RUN;
6 Code Block
ODS
Explanation :
Closes all currently open ODS destinations, then opens a new `ODS DOCUMENT` destination named `doc_results` in write mode.
Copied!
1ODS _ALL_ CLOSE;
2 
3* start new ODS DOCUMENT;
4ODS DOCUMENT NAME=doc_results(WRITE);
7 Code Block
PROC REPORT
Explanation :
Generates a tabular report of shoe sales (`shoes`) grouped by region (`region`). The ODS label and title are defined to identify it in the output.
Copied!
1ODS PROCLABEL="Table 1: By Group Report about shoes";
2TITLE "Table 1: By Group Report about shoes";
3PROC REPORT DATA=shoes CONTENTS="";
4 BY region;
5 COLUMN region product sales;
6 DEFINE region / ORDER NOPRINT;
7 BREAK BEFORE region / CONTENTS="" page;
8RUN;
8 Code Block
PROC REPORT
Explanation :
Generates a report listing the students from the `class` table. A page break is forced before the start of the report.
Copied!
1TITLE "Table 2: Table Class Output";
2ODS PROCLABEL "Table 2: Table Class Output";
3PROC REPORT DATA=class CONTENTS="";
4 COLUMN const name sex age height weight;
5 DEFINE const / ORDER NOPRINT;
6 BREAK BEFORE const / CONTENTS="" page;
7RUN;
9 Code Block
Macro
Explanation :
Defines a `%loopTroughMake` macro that generates a `PROC REPORT` for a given car make (`make`). The macro accepts the make and a table number `i` as parameters to customize the title and ODS label.
Copied!
1%MACRO loopTroughMake(make,i);
2 TITLE "Table &i: Multiple outputs - Cars for make = &make";
3 ODS PROCLABEL "Table &i: Multiple outputs - Cars for make = &make";
4 PROC REPORT DATA=cars(WHERE=(make = "&make")) nowd headline spacing=2 CONTENTS="";
5 COLUMN const make model type msrp;
6 DEFINE const / ORDER NOPRINT;
7 BREAK BEFORE const / CONTENTS="" page;
8 RUN;
9 TITLE;
10%MEND;
10 Code Block
Macro
Explanation :
Calls the `%loopTroughMake` macro three times to generate reports for Acura, Audi, and BMW makes.
Copied!
1%loopTroughMake(Acura,3);
2%loopTroughMake(Audi,4);
3%loopTroughMake(BMW,5);
4 
11 Code Block
PROC REPORT
Explanation :
Generates another report on the `class` table, similar to the second report but with a different title and ODS label.
Copied!
1ODS PROCLABEL="Table 6: Different label";
2TITLE "Table 6: Different title and label";
3PROC REPORT DATA=class CONTENTS="";
4 COLUMN const name sex age height weight;
5 DEFINE const / ORDER NOPRINT;
6 BREAK BEFORE const / CONTENTS="" page;
7RUN;
12 Code Block
PROC SGPLOT
Explanation :
Creates a vertical bar chart (`VBAR`) with `PROC SGPLOT` showing age (`age`) grouped by sex (`sex`) from the `sashelp.class` table.
Copied!
1ODS PROCLABEL="Figure 1: Class graphic";
2PROC SGPLOT DATA = sashelp.class;
3 VBAR age / GROUP = sex;
4 TITLE 'Figure 1: Class overview by sex and age';
5RUN;
13 Code Block
ODS
Explanation :
Closes the `ODS DOCUMENT` destination, which finalizes and saves the `doc_results` document with all generated outputs.
Copied!
1ODS DOCUMENT CLOSE;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Project: SMILE - SAS Macros, Intuitive Library Extension, Author: Katja Glass, License: MIT