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!
DATA class;
SET sashelp.class;
const = 1;
RUN;
1
DATA class;
2
SET sashelp.class;
3
const = 1;
4
RUN;
2 Code Block
PROC SORT
Explanation : Sorts the `class` table by `const` and `sex` variables.
Copied!
PROC SORT DATA=class; BY const sex;RUN;
1
PROC SORTDATA=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!
DATA shoes;
SET sashelp.shoes;
WHERE region in ('Canada', 'Pacific');
RUN;
1
DATA shoes;
2
SET sashelp.shoes;
3
WHERE region in ('Canada', 'Pacific');
4
RUN;
4 Code Block
PROC SORT
Explanation : Sorts the `shoes` table by `region` and `product`.
Copied!
PROC SORT DATA=shoes; BY region product; RUN;
1
PROC SORTDATA=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!
DATA cars;
SET sashelp.cars;
const = 1;
RUN;
1
DATA cars;
2
SET sashelp.cars;
3
const = 1;
4
RUN;
6 Code Block
ODS
Explanation : Closes all currently open ODS destinations, then opens a new `ODS DOCUMENT` destination named `doc_results` in write mode.
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!
ODS PROCLABEL="Table 1: By Group Report about shoes";
TITLE "Table 1: By Group Report about shoes";
PROC REPORT DATA=shoes CONTENTS="";
BY region;
COLUMN region product sales;
DEFINE region / ORDER NOPRINT;
BREAK BEFORE region / CONTENTS="" page;
RUN;
1
ODS PROCLABEL="Table 1: By Group Report about shoes";
2
TITLE "Table 1: By Group Report about shoes";
3
PROC REPORTDATA=shoes CONTENTS="";
4
BY region;
5
COLUMN region product sales;
6
DEFINE region / ORDER NOPRINT;
7
BREAK BEFORE region / CONTENTS="" page;
8
RUN;
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!
TITLE "Table 2: Table Class Output";
ODS PROCLABEL "Table 2: Table Class Output";
PROC REPORT DATA=class CONTENTS="";
COLUMN const name sex age height weight;
DEFINE const / ORDER NOPRINT;
BREAK BEFORE const / CONTENTS="" page;
RUN;
1
TITLE "Table 2: Table Class Output";
2
ODS PROCLABEL "Table 2: Table Class Output";
3
PROC REPORTDATA=class CONTENTS="";
4
COLUMN const name sex age height weight;
5
DEFINE const / ORDER NOPRINT;
6
BREAK BEFORE const / CONTENTS="" page;
7
RUN;
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!
%MACRO loopTroughMake(make,i);
TITLE "Table &i: Multiple outputs - Cars for make = &make";
ODS PROCLABEL "Table &i: Multiple outputs - Cars for make = &make";
PROC REPORT DATA=cars(WHERE=(make = "&make")) nowd headline spacing=2 CONTENTS="";
COLUMN const make model type msrp;
DEFINE const / ORDER NOPRINT;
BREAK BEFORE const / CONTENTS="" page;
RUN;
TITLE;
%MEND;
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";
Explanation : Generates another report on the `class` table, similar to the second report but with a different title and ODS label.
Copied!
ODS PROCLABEL="Table 6: Different label";
TITLE "Table 6: Different title and label";
PROC REPORT DATA=class CONTENTS="";
COLUMN const name sex age height weight;
DEFINE const / ORDER NOPRINT;
BREAK BEFORE const / CONTENTS="" page;
RUN;
1
ODS PROCLABEL="Table 6: Different label";
2
TITLE "Table 6: Different title and label";
3
PROC REPORTDATA=class CONTENTS="";
4
COLUMN const name sex age height weight;
5
DEFINE const / ORDER NOPRINT;
6
BREAK BEFORE const / CONTENTS="" page;
7
RUN;
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!
ODS PROCLABEL="Figure 1: Class graphic";
PROC SGPLOT DATA = sashelp.class;
VBAR age / GROUP = sex;
TITLE 'Figure 1: Class overview by sex and age';
RUN;
1
ODS PROCLABEL="Figure 1: Class graphic";
2
PROC SGPLOTDATA = sashelp.class;
3
VBAR age / GROUP = sex;
4
TITLE 'Figure 1: Class overview by sex and age';
5
RUN;
13 Code Block
ODS
Explanation : Closes the `ODS DOCUMENT` destination, which finalizes and saves the `doc_results` document with all generated outputs.
Copied!
ODS DOCUMENT CLOSE;
1
ODS 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
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.