Fundamentals of Programming in SAS. James Blum
when using the VARNUM option is actually a replacement of the Variables table with the Position table.
If ODS table names (and other output object names, such as graphs) are known, other forms of ODS statements are available to choose which output to include or not. Program 1.5.2 shows how to modify each of the CONTENTS procedures in Program 1.5.1 to only display the variable information.
Program 1.5.2: Using ODS SELECT to Subset Output
proc contents data=sashelp.cars;
ods select Variables;
run;
proc contents data=sashelp.cars varnum;
ods select Position;
run;
ODS SELECT and ODS EXCLUDE each support a space-separated list of output object names. SELECT chooses output objects to be delivered; EXCLUDE chooses those that are not delivered. Only one should be used in any procedure, typically corresponding to whichever list of tables is shorter—those to be included or excluded. In place of the list of object names, one of the keywords of ALL or NONE can be used. ODS SELECT or ODS EXCLUDE can be placed directly before or within a procedure, and its effect is local if a list of objects is given, only applying to the execution of that procedure. If the ALL or NONE keywords are used, the effect is global, remaining in place until another statement alters it.
The VARNUM option produces a table (Output 1.5.2B) with the same variable information as the first PROC CONTENTS but, as shown in the trace, it is a different table with a different name.
Output 1.5.2A: Using ODS SELECT to Subset Output
Alphabetic List of Variables and Attributes | |||||
# | Variable | Type | Len | Format | Label |
9 | Cylinders | Num | 8 | ||
5 | DriveTrain | Char | 5 | ||
8 | EngineSize | Num | 8 | Engine Size (L) | |
10 | Horsepower | Num | 8 | ||
7 | Invoice | Num | 8 | DOLLAR8. | |
15 | Length | Num | 8 | Length (IN) | |
11 | MPG_City | Num | 8 | MPG (City) | |
12 | MPG_Highway | Num | 8 | MPG (Highway) | |
6 | MSRP | Num | 8 | DOLLAR8. | |
1 | Make | Char | 13 | ||
2 | Model | Char | 40 | ||
4 | Origin | Char | 6 | ||
3 | Type | Char | 8 | ||
13 | Weight | Num | 8 | Weight (LBS) | |
14 | Wheelbase | Num | 8 | Wheelbase (IN) |
Output 1.5.2B: Using ODS SELECT to Subset Output
Variables in Creation Order | |||||
# | Variable | Type | Len | Format | Label |
1 | Make | Char | 13 | ||
2 | Model | Char | 40 | ||
3 | Type | Char | 8 | ||
4 | Origin | Char | 6 | ||
5 | DriveTrain | Char | 5 | ||
6 | MSRP | Num | 8 | DOLLAR8. | |
7 | Invoice | Num | 8 | DOLLAR8. | |
8 | EngineSize | Num | 8 | Engine Size (L) | |
9 | Cylinders | Num | 8 | ||
10 | Horsepower | Num | 8 | ||
11 | MPG_City | Num | 8 | MPG (City) | |
12 | MPG_Highway | Num | 8 | MPG (Highway) | |
13 | Weight | Num | 8 | Weight (LBS) | |
14 | Wheelbase | Num | 8 | Wheelbase (IN) | |
15 | Length | Num | 8 | Length (IN) |
ODS statements can be used to direct output to various destinations, including multiple destinations at any one time. Output styles can vary across destinations, as Program 1.5.3 demonstrates by delivering the same graph to a PDF and PNG file.
Program 1.5.3: Setting Output Destinations Using ODS Statements
x ‘cd C:\Output’;
ods _ALL_ CLOSE;
ods listing;
ods pdf file=’Output 1-5-3.pdf’;
proc sgplot data=sashelp.cars;
styleattrs datasymbols=(square circle triangle);
scatter y=mpg_city x=horsepower/group=type;
where type in (‘Sedan’,’Wagon’,’Sports’);
run;
ods pdf close;
The X command allows for submission of command line statements. CD is the change directory command in both Windows and Linux, here its effect is to change the SAS working directory. The SAS working directory is the default destination for any file reference that does not include a full path—one that starts with a drive letter or name. This directory must exist to successfully submit this code; therefore, either create the directory C:\Output or substitute another that the SAS session has write access to.
The ODS _ALL_ CLOSE statement closes all output destinations.
The ODS LISTING statement activates the listing destination, which is the destination for all graphics files created by the SGPLOT procedure. In the SAS windowing environment the ODS LISTING statement also activates the Output window, but graphics generated by PROC SGPLOT are not displayed there.
The ODS PDF statement opens the PDF destination specified in the FILE= option (if this option is omitted the file is automatically named). Since the file name does not reference any path, it is placed in the location specified in . A full-path reference, starting with a drive letter or name, can be given here. Commonly used destinations include PDF, RTF, HTML, and LISTING, but several others are available.
Output 1.5.3A shows the graph generated by PROC SGPLOT and placed in the PDF file, while Output 1.5.3B shows the graphics file (a PNG file by default) generated. Note the difference in appearance between the two (and check the log)—different output destinations can have different options or styles in effect.
The ODS PDF CLOSE statement closes the PDF destination opened in and completes writing of the file, which includes all output generated between the opening and closing ODS statements. In general, any ODS statement that opens a destination should have a complementary CLOSE statement.
Output 1.5.3A: Graph Delivered to PDF File
Output 1.5.3B: Graph Delivered to PNG File (Listing Destination)
While the graph in the PDF file uses the same plotting shape and cycles the colors, the one delivered as an image file cycles through both different shapes and colors. The takeaway from this example, which applies in several instances, is that not all output destinations use the same styles. In this book, graphs are shown in the form generated by direct delivery to TIF files, see Chapter Note 7 in Section 1.7 for options used to generate these graphs.
Program 1.5.3 shows that ODS statements permit delivery of output to more than one destination at a time, and they also allow for different subsets of output to be delivered to each. While Program 1.5.3 shows that different destinations may have certain style elements that are different, it is also possible to specifically prescribe different styles to different output destinations. Program 1.5.4 opens a PDF and an RTF destination, sending different subsets of the output to each, and with different styles assigned to each.
Program 1.5.4: Setting Multiple Output Destinations and Styles Using ODS Statements