dsci 325: handout 11 – enhancing output with output ...course1.winona.edu/thooks/media/handout 11...

17
1 DSCI 325: Handout 11 – Enhancing Output with Output Delivery System (ODS) Spring 2016 In SAS 9.3, output that is produced by SAS is by default created in HTML and appears in the Results Viewer window. Technically, the procedures (i.e., PROCs) that we run in SAS don’t produce output. Instead, they produce “data” that is sent to the Output Delivery System (ODS), and then the output delivery system writes the “data” to the Results Viewer window. The “data” that has been printed to the Results Viewer window thus far in the semester has used the default settings. As you will see in this handout, you can change the settings in the output delivery system to: Change the location to which the output will be printed Specify style templates to change the appearance of the output Specify table templates to change the basic structure of your output A listing of the possible destinations for your output is given below. Some of these will be discussed in more detail later in this handout. Location Description LISTING Results appear in a SAS output window HTML Hypertext Markup language (web language) RTF Rich text format PRINTER High resolution printer output PS PostScript PCL Printer control language PDF Portable document format OUTPUT SAS output data set MARKUP Markup language including XML DOCUMENT Output document In addition to the various built-in destinations for your output, there also exist several built-in templates that can be used to modify the appearance of your output. To see the various styles available in SAS, run the following code. PROC TEMPLATE; LIST STYLES; RUN; A partial listing of the output is provided next.

Upload: others

Post on 16-Jun-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DSCI 325: Handout 11 – Enhancing Output with Output ...course1.winona.edu/thooks/Media/Handout 11 - Output Delivery Syst… · DSCI 325: Handout 11 – Enhancing Output with Output

1

DSCI 325: Handout 11 – Enhancing Output with Output Delivery System (ODS) Spring 2016

In SAS 9.3, output that is produced by SAS is by default created in HTML and appears in the Results Viewer window. Technically, the procedures (i.e., PROCs) that we run in SAS don’t produce output. Instead, they produce “data” that is sent to the Output Delivery System (ODS), and then the output delivery system writes the “data” to the Results Viewer window. The “data” that has been printed to the Results Viewer window thus far in the semester has used the default settings. As you will see in this handout, you can change the settings in the output delivery system to:

• Change the location to which the output will be printed • Specify style templates to change the appearance of the output • Specify table templates to change the basic structure of your output

A listing of the possible destinations for your output is given below. Some of these will be discussed in more detail later in this handout.

Location Description LISTING Results appear in a SAS output window HTML Hypertext Markup language (web language) RTF Rich text format PRINTER High resolution printer output PS PostScript PCL Printer control language PDF Portable document format OUTPUT SAS output data set MARKUP Markup language including XML DOCUMENT Output document

In addition to the various built-in destinations for your output, there also exist several built-in templates that can be used to modify the appearance of your output. To see the various styles available in SAS, run the following code. PROC TEMPLATE; LIST STYLES; RUN;

A partial listing of the output is provided next.

Page 2: DSCI 325: Handout 11 – Enhancing Output with Output ...course1.winona.edu/thooks/Media/Handout 11 - Output Delivery Syst… · DSCI 325: Handout 11 – Enhancing Output with Output

2

Note: You can also use these style codes directly within PROC PRINT, PROC REPORT, and PROC TABULATE to control the appearance of the output that results from these procedures.

TRACING AND SELECTING PROCEDURE OUTPUT

Tracing your output is a process in which SAS communicates (via the log window) its naming conventions for the various output that it creates. For example, consider the following code which reads in the CourseEnrollment data set. The CourseEnrollment data set is printed and sorted, and PROC MEANS is used to summarize Enrollment information. DATA CourseEnrollments; INFILE 'T:\20165001682\ReadOnly\Data\CourseEnrollments.txt'; INPUT Name $18. +1 Subject $4. CourseNum StartDate MMDDYY10. Section Credits Enrollment; RUN; PROC PRINT DATA=CourseEnrollments; RUN; PROC SORT DATA=CourseEnrollments; BY StartDate; RUN; PROC MEANS DATA=CourseEnrollments; VAR Enrollment; BY StartDate; RUN; Run this code to make sure that it works. Then, include the ODS TRACE ON statement before this code and the ODS TRACE OFF statement after this code.

Page 3: DSCI 325: Handout 11 – Enhancing Output with Output ...course1.winona.edu/thooks/Media/Handout 11 - Output Delivery Syst… · DSCI 325: Handout 11 – Enhancing Output with Output

3

ODS TRACE ON; DATA CourseEnrollments; INFILE 'T:\20165001682\ReadOnly\Data\CourseEnrollments.txt'; INPUT Name $18. +1 Subject $4. CourseNum StartDate MMDDYY10. Section Credits Enrollment; RUN; PROC PRINT DATA=CourseEnrollments; RUN; PROC SORT DATA=CourseEnrollments; BY StartDate; RUN; PROC MEANS DATA=CourseEnrollments; VAR Enrollment; BY StartDate; RUN; ODS TRACE OFF; After running this code, view the log window. The log window should contain information about the output that was printed to the Results Viewer window. Output Added: ------------- Name: Print Label: Data Set WORK.COURSEENROLLMENTS Data Name: ProcPrintTable Path: Print.Print -------------

Output Added: ------------- Name: Summary Label: Summary statistics Template: base.summary Path: Means.ByGroup1.Summary -------------

Output Added: ------------- Name: Summary Label: Summary statistics Template: base.summary Path: Means.ByGroup2.Summary Once you have identified this information for each output object, you can control exactly what gets printed to the Results Viewer window by specifying the path. For example, consider the following code which forces SAS to print only the information from StartDate = 18277.

Page 4: DSCI 325: Handout 11 – Enhancing Output with Output ...course1.winona.edu/thooks/Media/Handout 11 - Output Delivery Syst… · DSCI 325: Handout 11 – Enhancing Output with Output

4

PROC MEANS DATA=CourseEnrollments; VAR Enrollment; BY StartDate; ODS SELECT Means.ByGroup2.Summary; RUN; The output produced by the above code is given below.

Note that instead of using the ODS SELECT statement, you could have used the ODS EXCLUDE statement to obtain the same results:

PROC MEANS DATA=CourseEnrollments; VAR Enrollment; BY StartDate; ODS Exclude Means.ByGroup1.Summary; RUN;

Page 5: DSCI 325: Handout 11 – Enhancing Output with Output ...course1.winona.edu/thooks/Media/Handout 11 - Output Delivery Syst… · DSCI 325: Handout 11 – Enhancing Output with Output

5

USING THE ODS OUTPUT STATEMENT

At times, you may want to create a new data set with the results from a procedure you have implemented in SAS. With the Output Delivery System, you can send procedure output to the OUTPUT destination to accomplish this. For example, suppose we wanted to save the results of the previous run of PROC MEANS for the CourseEnrollment data set. We have already viewed the trace in the log window: Output Added:

------------- Name: Summary Label: Summary statistics Template: base.summary Path: Means.ByGroup1.Summary ------------- NOTE: The above message was for the following BY group: StartDate=18141 Output Added: ------------- Name: Summary Label: Summary statistics Template: base.summary Path: Means.ByGroup2.Summary ------------- NOTE: The above message was for the following BY group: StartDate=18277

Note that the two output objects shown above have the same name, but different paths. The following program uses an ODS OUTPUT statement to create a new SAS data set named “CEMeans” from both of the output objects shown above. PROC MEANS DATA=CourseEnrollments; VAR Enrollment; BY StartDate; ODS OUTPUT Summary = CEMeans; RUN; PROC PRINT DATA=CEMeans; RUN;

You name the output data set

This is the Name of the output object(s) of interest (obtained from using ODS TRACE)

Page 6: DSCI 325: Handout 11 – Enhancing Output with Output ...course1.winona.edu/thooks/Media/Handout 11 - Output Delivery Syst… · DSCI 325: Handout 11 – Enhancing Output with Output

6

If we had wanted to include only the mean from StartDate=18277 in our data set named “CEMeans,” we could have used the following program, instead. PROC MEANS DATA=CourseEnrollments; VAR Enrollment; BY StartDate; ODS OUTPUT Means.ByGroup2.Summary = CEMeans; RUN; PROC PRINT DATA=CEMeans; RUN;

Comment: The ODS OUTPUT statement is not really a part of the PROC MEANS step. You could have also used the following program. ODS OUTPUT Means.ByGroup2.Summary = CEMeans; PROC MEANS DATA=CourseEnrollments; VAR Enrollment; BY StartDate; RUN; PROC PRINT DATA=CEMeans; RUN;

This works because the ODS OUTPUT statement opens a SAS data set and waits until the next encounter with the end of a PROC step. So, it will apply to whatever PROC is currently being processed or to the next PROC if there is not a current PROC. To ensure that you get the correct output, you should get in the habit of putting the ODS OUTPUT statement after the PROC statement of interest and before the next PROC, DATA, or RUN statement. This is what was shown initially.

PROC MEANS DATA=CourseEnrollments; VAR Enrollment; BY StartDate; ODS OUTPUT Means.ByGroup2.Summary = CEMeans; RUN;

Page 7: DSCI 325: Handout 11 – Enhancing Output with Output ...course1.winona.edu/thooks/Media/Handout 11 - Output Delivery Syst… · DSCI 325: Handout 11 – Enhancing Output with Output

7

USING ODS TO CREATE HTML/RTF OUTPUT

As mentioned previously, the default location for output to be placed is in the Results Viewer window (with the HTML destination). In older versions of SAS, the output was placed in the Output window by default (i.e., the LISTING destination). As mentioned earlier in the handout, you can use the ODS statement to change the destination and also to write output to certain files. For example, the following code creates an HTML document in the C:\Users\THooks directory. The name of the HTML document created is CEMeans.html. ODS HTML FILE= 'C:\Users\THooks\CEMeans.html'; DATA CourseEnrollments; INFILE 'T:\20165001682\ReadOnly\Data\CourseEnrollments.txt'; INPUT Name $18. +1 Subject $4. CourseNum StartDate MMDDYY10. Section Credits Enrollment; RUN; PROC SORT DATA=CourseEnrollments; BY StartDate; RUN; PROC FORMAT; VALUE Semester 18141 = 'Fall' 18277 = 'Spring'; RUN; PROC MEANS DATA=CourseEnrollments; VAR Enrollment; BY StartDate; FORMAT StartDate Semester.; RUN; ODS HTML CLOSE; The Results Viewer window shows you the output that was written to the CEMeans.html file.

Don’t forget to “close” this! Otherwise, SAS keeps writing output to this file. Also, remember to include an ODS HTML OPEN; statement after this in your program in order to “open” the Results Viewer window again.

Page 8: DSCI 325: Handout 11 – Enhancing Output with Output ...course1.winona.edu/thooks/Media/Handout 11 - Output Delivery Syst… · DSCI 325: Handout 11 – Enhancing Output with Output

8

Note that you can now go to the C:\Users\THooks directory and open the CEMeans.html file. This file is shown in a web browser because of the *.html format.

Note: Notice that SAS has placed a few titles at the top of this output. You can remove the title “The MEANS Procedure” by using the ODS NOPROCTITLE command as in shown below.

ODS HTML FILE= 'C:\Users\THooks\CEMeans.html'; ODS NOPROCTITLE; . . . ODS HTML CLOSE; Instead of creating HTML files, you can create RTF files in a similar way. RTF files are portable and work well with Microsoft products. The following ODS statement creates an RTF file named CEMeans.rtf. The program also contains various options (highlighted in yellow) that have been specified to control the appearance of the output. ODS RTF FILE='C:\Users\THooks\CEMeans.rtf' BODYTITLE COLUMNS=2 STYLE=BANKER; ODS NOPROCTITLE; DATA CourseEnrollments; INFILE 'T:\20165001682\ReadOnly\Data\CourseEnrollments.txt'; INPUT Name $18. +1 Subject $4. CourseNum StartDate MMDDYY10. Section Credits Enrollment; RUN; PROC SORT DATA=CourseEnrollments; BY StartDate; RUN;

Page 9: DSCI 325: Handout 11 – Enhancing Output with Output ...course1.winona.edu/thooks/Media/Handout 11 - Output Delivery Syst… · DSCI 325: Handout 11 – Enhancing Output with Output

9

PROC FORMAT; VALUE Semester 18141 = 'Fall' 18277 = 'Spring'; RUN; PROC MEANS DATA=CourseEnrollments; VAR Enrollment; BY StartDate; FORMAT StartDate Semester.; RUN; PROC PRINT DATA=CourseEnrollments; VAR Name Enrollment; RUN; ODS RTF CLOSE; The following is a partial listing of the contents in the CEMeans.rtf file.

Note: SAS will not recreate the CEMeans.rtf file unless the file has been closed. That is, you must close this file before SAS can recreate it. The following error informs you of this in the log file.

Tasks:

1. Remove the BODYTITLE option. What changes? 2. Remove the COLUMNS=2 option. What changes? 3. Remove the STYLE=BANKER option. What changes? 4. Change the above option to STYLE=ANALYSIS. How different is the resulting output? 5. Modify the code to create a file named CEMeans.pdf with the output, instead.

Page 10: DSCI 325: Handout 11 – Enhancing Output with Output ...course1.winona.edu/thooks/Media/Handout 11 - Output Delivery Syst… · DSCI 325: Handout 11 – Enhancing Output with Output

10

CUSTOMIZING TITLES AND FOOTNOTES

When using ODS, you can specify options for titles and footnotes. The following general structure is used for the title statement. TITLE options ‘title1’ options ‘title2’ … Some common options are listed below.

COLOR= Color of text BCOLOR= Color of background HEIGHT= Height of text JUSTIFY= Left/center/right justification FONT= Specifies font for text BOLD Bold text ITALIC Italic text

This statement allows you to completely control the appearance of the title. For example, consider the following program. ODS HTML FILE='C:\Users\Thooks\CourseEnrollment.html'; DATA CourseEnrollments; INFILE 'T:\20165001682\ReadOnly\Data\CourseEnrollments.txt'; INPUT Name $18. +1 Subject $4. CourseNum StartDate MMDDYY10. Section Credits Enrollment; Course = CATX(' ', Subject, CourseNum); RUN; PROC FORMAT; VALUE Semester 18141 = 'Fall' 18277 = 'Spring'; RUN; PROC PRINT DATA=CourseEnrollments; VAR Name Course StartDate Enrollment; FORMAT StartDate Semester.; RUN; TITLE1 HEIGHT=18pt 'Course Enrollments'; ODS NOPROCTITLE; ODS HTML CLOSE;

Page 11: DSCI 325: Handout 11 – Enhancing Output with Output ...course1.winona.edu/thooks/Media/Handout 11 - Output Delivery Syst… · DSCI 325: Handout 11 – Enhancing Output with Output

11

A portion of the SAS output that is produced is shown below.

As mentioned previously, you have complete control over the appearance of text using the ODS system. Consider the following change to the title statement. Notice these changes affect only the ‘Enrollment’ portion of the title and not the ‘Course’ portion. TITLE1 HEIGHT=18pt 'Course' HEIGHT=24pt BCOLOR='#000000' COLOR='#FFFFFF' JUSTIFY=RIGHT 'Enrollments';

The colors in the above statement are specified using RGB hexademical code. Note that you could have also specified these options as follows:

TITLE1 HEIGHT=18pt 'Course' HEIGHT=24pt BCOLOR=black COLOR=white JUSTIFY=RIGHT 'Enrollments';

SAS actually recognizes several different naming schemes for color specification. For more information, see the following link:

http://support.sas.com/documentation/cdl/en/graphref/63022/HTML/default/viewer.htm#colors-specify-color.htm

Page 12: DSCI 325: Handout 11 – Enhancing Output with Output ...course1.winona.edu/thooks/Media/Handout 11 - Output Delivery Syst… · DSCI 325: Handout 11 – Enhancing Output with Output

12

Finally, note that you could also include more than one TITLE statement.

TITLE1 HEIGHT=12pt FONT=Arial BOLD 'Course Enrollments'; TITLE2 HEIGHT=8pt FONT='Times New Roman' ITALIC 'Department of Mathematics and Statistics';

The FOOTNOTE statement works similarly. PROC PRINT DATA=CourseEnrollments;

VAR Name Course StartDate Enrollment; FORMAT StartDate Semester.; WHERE Name = 'Tisha Hooks'; RUN; TITLE1 HEIGHT=12pt FONT=Arial BOLD 'Course Enrollments'; FOOTNOTE1 HEIGHT=8pt FONT='Times New Roman' ITALIC 'Department of Mathematics and Statistics';

Page 13: DSCI 325: Handout 11 – Enhancing Output with Output ...course1.winona.edu/thooks/Media/Handout 11 - Output Delivery Syst… · DSCI 325: Handout 11 – Enhancing Output with Output

13

USING THE STYLE= OPTION IN PROC PRINT AND PROC TABULATE

We can use the STYLE statement within the PROC PRINT procedures to enhance the appearance of the output. Consider the following addition to the CourseEnrollment data set.

• If Enrollment > 40, then Enrollment Level will be set to High • If Enrollment < 10, then Enrollment Level will be set to Low • Otherwise, Enrollment Level will be set to OK

ODS HTML FILE='C:\Users\Thooks\CourseEnrollments.html'; TITLE1 HEIGHT=18pt 'Course Enrollments'; ODS NOPROCTITLE; DATA CourseEnrollments; INFILE 'T:\20165001682\ReadOnly\Data\CourseEnrollments.txt'; INPUT Name $18. +1 Subject $4. CourseNum StartDate MMDDYY10. Section Credits Enrollment; Course = CATX(' ', Subject, CourseNum); IF Enrollment > 40 THEN EnrollLevel = 'High'; ELSE IF Enrollment < 10 THEN EnrollLevel = 'Low'; ELSE EnrollLevel = 'OK'; RUN; PROC FORMAT; VALUE Semester 18141 = 'Fall' 18277 = 'Spring'; RUN; PROC SORT DATA=CourseEnrollments; BY EnrollLevel StartDate Course; RUN; PROC PRINT DATA=CourseEnrollments; VAR StartDate Course Enrollment; BY EnrollLevel; FORMAT StartDate Semester.; RUN; ODS HTML CLOSE;

Page 14: DSCI 325: Handout 11 – Enhancing Output with Output ...course1.winona.edu/thooks/Media/Handout 11 - Output Delivery Syst… · DSCI 325: Handout 11 – Enhancing Output with Output

14

A partial listing of this output is shown below.

Consider some of the following options specified within the PROC PRINT procedure and how the above output changes. PROC PRINT DATA=CourseEnrollments STYLE(DATA) = {BACKGROUND=blue FOREGROUND=white}; VAR StartDate Course Enrollment; BY EnrollLevel; FORMAT StartDate Semester.; RUN;

OBS

OBSHEADER

HEADER

DATA

Page 15: DSCI 325: Handout 11 – Enhancing Output with Output ...course1.winona.edu/thooks/Media/Handout 11 - Output Delivery Syst… · DSCI 325: Handout 11 – Enhancing Output with Output

15

PROC PRINT DATA=CourseEnrollments STYLE(DATA) = {BACKGROUND=white PREIMAGE='D:\DATA\SAS\WSULogo.jpg'}; VAR StartDate Course Enrollment; BY EnrollLevel; FORMAT StartDate Semester.; RUN;

PROC PRINT DATA=CourseEnrollments STYLE(DATA) = {BACKGROUND=white JUST=C CELLHEIGHT=80pt BACKGROUNDIMAGE='D:\DATA\SAS\WSULogo.jpg'}; VAR StartDate Course Enrollment; BY EnrollLevel; FORMAT StartDate Semester.; RUN;

PROC PRINT DATA=CourseEnrollments STYLE(DATA) = {BACKGROUND=white JUST=C} STYLE(HEADER)={BACKGROUND=White JUST=C URL='http://www.winona.edu'}; VAR StartDate Course Enrollment; BY EnrollLevel; FORMAT StartDate Semester.; RUN;

DATA

HEADER

Page 16: DSCI 325: Handout 11 – Enhancing Output with Output ...course1.winona.edu/thooks/Media/Handout 11 - Output Delivery Syst… · DSCI 325: Handout 11 – Enhancing Output with Output

16

ADDING TRAFFIC-LIGHTING TO OUTPUT

A ‘traffic light’ coding can be implemented in SAS. Consider the following code. Note that the STYLE statement that controls the ‘traffic light’ is specified as an option in the VAR statement so that this style is implemented for only this column. PROC FORMAT; VALUE LightEnroll 0 - < 10 = 'red' 10 - < 40 ='yellow' 40 - HIGH ='green'; PROC PRINT DATA=CourseEnrollments STYLE(DATA) = {BACKGROUND=white JUST=C} STYLE(HEADER)={BACKGROUND=White JUST=C URL='http://www.winona.edu'}; VAR StartDate Course; VAR Enrollment / STYLE(DATA) = {Background=LightEnroll.}; FORMAT StartDate Semester.; RUN;

A partial listing of the SAS output is shown below.

More Information on Style Attributes can be found at the following link:

http://support.sas.com/documentation/cdl/en/odsug/61723/HTML/default/viewer.htm#a002972093.htm

Page 17: DSCI 325: Handout 11 – Enhancing Output with Output ...course1.winona.edu/thooks/Media/Handout 11 - Output Delivery Syst… · DSCI 325: Handout 11 – Enhancing Output with Output

17