12. The Output Delivery System and Graphics

In this lesson, we’ll learn how to use the SAS System’s Output Delivery System (ODS) to create other forms of output, such as HTML output that can be viewed by your web browser, PDF files that are formatted for high-resolution printers, and RTF files that can be easily imported into Microsoft Word. Along the way, we’ll also learn how to modify the appearance of the output that is obtained by default from any procedure. Finally, we’ll learn how to use the Output Delivery System to create SAS data sets instead of the default output from various procedures.

We will also return to the SG plotting procedures to investigate how to adjust graphical parameters such as line types, colors, axes labels, legends, etc.

12.1. The Output Delivery System

You might be getting the impression that by learning about the Output Delivery System (ODS) in this lesson that it will be the first time we use it. In reality, SAS has been using it behind the scenes all along to create the listing output that the procedures we’ve used generates by default. All we want to do in this lesson is learn how ODS works and how to change the default settings so that we can get the output that we want rather than the output that SAS wants.

12.1.1. How ODS Works

So how does ODS work? Whenever you submit a program that creates output, ODS does the following:

  1. ODS creates your output in the form of output objects. Each output object is comprised of two components. The data component contains the results — think numbers — of a procedure or a DATA step. The table definition tells SAS how to render the results — think structure. For example, suppose we executed the FREQ procedure so that it created the following output:

A B
Frequency Percent 1 2 Total
1 60 40 100
30.00 20.00 50.00
2 40 60 100
20.00 30.00 50.00
Total 100 100 200
50.00 50.00 100.00

SAS actually creates this piece of output from its two parts, the table definition:

A B
Frequency Percent 1 2 Total
1      
     
2      
     
Total      
     

and the data component:

A B COUNT PERCENT
  1 60 30
1 2 40 20
2 1 40 20
2 2 60 30

2) Once SAS creates all of the output objects from an executed program, it then just needs to figure out where to send the objects. It's actually pretty easy... SAS sends the output to whatever ODS destination(s) you tell SAS to send it. And when doing so, SAS sends the output in the format specified by the destination. This is where ODS is really powerful and therefore really neat! Besides the listing output generated by SAS procedures by default, and among others, you can send your output to...

this destination ... to produce...
HTML output that is formatted in HyperText Markup Language (HTML), and therefore viewable by web browsers
Output SAS data sets
Printer Family output that is formatted for a high-resolution printer, such as Post Script (PS), Portable Document Format (PDF), and Printer Control Language (PCL) files
RTF rich text format output for use with Microsoft Word

In the next section, we'll learn how to tell SAS where to send the output it generates by "opening" and "closing" these various ODS destinations.

12.1.2. Opening and Closing ODS Destinations

If you are perfectly content with your output being sent to the output window in the default HTML format, then you don’t have tell SAS anything at all. That’s because the HTML destination is open by default. On the other hand, if you want to tell SAS to send your output to another ODS destination, PDF say, then you have to open the destination before the SAS code that generates your output.

To open a destination, you simply submit the following ODS statement:

ODS open-destination;;

where open-destination is a keyword (as well as any required options for the destination) that tells SAS where you want to send your output. In this lesson, we’ll focus only on the most commonly used keyword destinations: Listing, HTML, RTF, and PDF.

After the SAS code that generates your output, you have to tell SAS to close the destination so that you can access your output. To close a destination, you simply submit the following ODS statement:

ODS close-destination CLOSE;

where close-destination is the same keyword as the open-destination.

In theory, you can submit ODS statements in any order, depending on whether you need to open or close an ODS destination. In practice, however, most ODS destinations are closed by default, so that you open them at the beginning of your program and close them at the end. The exception is the HTML destination, which is open by default. Let’s take a look at an example.

Example

You might recall that the SAS data set called penngolf contains information, such as the total yardage and par, of eleven golf courses in Pennsylvania. The following program opens the HTML destination so that a subset of the penngolf data set can be printed in HTML format as well as the default HTML format in the output window:

LIBNAME phc6089 "/folders/myfolders/SAS_Notes/data";
 
ODS HTML file = '/folders/myfolders/SAS_Notes/output/html/golf.html';
 
PROC PRINT data = phc6089.penngolf NOOBS;
   title 'Some of the penngolf data set variables';
   ID name;
   var year type par yards;
RUN;
 
ODS HTML CLOSE;
ODS HTML;
SAS Output

Some of the penngolf data set variables

Name Year Type Par Yards
Toftrees 1968 Resort 72 7018
Penn State Blue 1921 Public 72 6525
Centre Hills 1921 Private 71 6392
Lewistown CC . Private 72 6779
State College Elks 1973 SemiPri 71 6369
Park Hills CC 1966 SemiPri 70 6004
Sinking Valley CC 1967 SemiPri 72 6755
Williamsport CC 1909 Private 71 6489
Standing Stone GC 1973 SemiPri 70 6593
Bucknell GC 1960 SemiPri 70 6253
Mount Airy Lodge 1972 Resort 72 7123

This code illustrates the standard ODS practice mentioned earlier... open your destinations at the top of your program, and close them at the bottom. Here, the first ODS statement tells SAS to open the HTML destination and to save the HTML output generated by the PRINT procedure that follows to the specified file name. The second ODS statement tells SAS to close the HTML destination so that we can access the created HTML file.

Download and save the penngolf (see the data folder on the course website if you don't already have it) data set to a convenient location on your computer. Then, launch the SAS code, and edit the LIBNAME statement so that it reflects the location in which you saved the data set. Also, edit the first ODS statement's FILE= option so that it reflects the location and name of the file where you want the resulting HTML output to be sent. (Make sure that you give your filename the standard .html extension.) Finally, run the SAS program. In doing so, you should note that SAS generates two pieces of output. The default output is displayed, as always, in the output window (as shown above). An html file copy of this output is save to at the PATH you specified in the FILE statement of ODS HTML.

You can simultaneously create output in multiple outputs by using ODS, so you can have multiple output destinations open at any given time. Each open output destination uses resources, so if you do not need to output to that particular destination, it is a good idea to close it. When you have more than one open ODS destination you can use the keyword shortcut ALL to close all of the destinations concurrently. That is, the following statement:

ODS _ALL_ CLOSE;

closes all currently open destinations at once. Then you can reopen whichever output desination you would like to use.

12.1.3. Producing HTML Output

We have been using HTML output as our default output to the Results Viewer, but if we would like to save the HTML output to an external file, then we can use the HTML keyword in the ODS statement with a save option such as FILE. In this section, we’ll extend what we learned there by:

  • creating HTML output from multiple procedures at once;

  • creating HTML output with a table of contents; and

  • using options to specify links and paths.

Example

The following program uses the penngolf data set to simultaneously create HTML output from the PRINT and REPORT procedures:

ODS HTML body = '/folders/myfolders/SAS_Notes/output/html/golf2.html';
 
PROC PRINT data = phc6089.penngolf NOOBS;
    title 'Some Par 72 Pennsylvania Golf Courses';
    ID name;
    var year type yards;
    where par = 72;
RUN;
 
PROC REPORT data = phc6089.penngolf NOWINDOWS HEADLINE HEADSKIP;
    title 'Average Size of Some PA Courses';
    column type par yards;
    define type /group;
    define yards / analysis mean format = 6.1 width = 10;
    define par / analysis mean format = 4.1 width = 10;
RUN;
 
ODS HTML CLOSE;
ODS HTML;
SAS Output

Some Par 72 Pennsylvania Golf Courses

Name Year Type Yards
Toftrees 1968 Resort 7018
Penn State Blue 1921 Public 6525
Lewistown CC . Private 6779
Sinking Valley CC 1967 SemiPri 6755
Mount Airy Lodge 1972 Resort 7123

Average Size of Some PA Courses

Type Par Yards
Private 71.3 6553.3
Public 72.0 6525.0
Resort 72.0 7070.5
SemiPri 70.6 6394.8

Before launching and running the program, let's take a quick look at the code to make sure we know what it's doing:

  • The first ODS statement tells SAS to open the HTML destination and to save the HTML output generated by the code to the specified file name. Note that rather than using the ODS HTML statement's FILE= option, we used the BODY= option to tell SAS where to save the HTML output. The two options are interchangeable. That is, the BODY= option is an alias for the FILE= option.
  • Then, we use the PRINT procedure to tell SAS to print some information about the par 72 golf courses.
  • Then, we use the REPORT procedure to tell SAS to calculate the average yardage and average par for each of the four types of golf courses.
  • The second ODS statement tells SAS to close the HTML destination so that we can access the created HTML file.
  • And, the last ODS statement tells SAS to re-open the HTML destination.

Now, go ahead and launch the SAS program. Again, you'll have to edit the first ODS HTML statement to reflect where you would like your HTML file stored. Then, run the SAS program, and review the output as it appears in the SAS Results Viewer. You should first see the output from the PRINT procedure and then the output from the REPORT procedure as shown above.

You should also note that SAS saves the generated HTML output in the file specified in the first ODS HTML statement. To see the file, go to the folder in which you told SAS to store the HTML file. Here's what my folder looks like after running the program.

When we run the above code, SAS creates the golf2.html file. You should see the same output that SAS displays in the SAS Results Viewer. It is this physical golf2.html file though that you could easily post to a public web site or e-mail to someone else.

Example: Creating HTML Output with a Table of Contents

When you have a program that creates many pages of output, you might find it useful for SAS to create a table of contents for the output. The following program is identical to the previous program, except the first ODS HTML statement has been modified to tell SAS to create a table of contents for the output that SAS generates:

ODS HTML path = '/folders/myfolders/SAS_Notes/output/html/' (url = none)
         body = 'golf3.html'
         contents = 'golf3toc.html'
         frame = 'golf3frame.html';
 
PROC PRINT data = phc6089.penngolf NOOBS;
    title 'Some Par 72 Pennsylvania Golf Courses';
    ID name;
    var year type yards;
    where par = 72;
RUN;
 
PROC REPORT data = phc6089.penngolf NOWINDOWS HEADLINE HEADSKIP;
    title 'Average Size of Some PA Courses';
    column type par yards;
    define type /group;
    define yards / analysis mean format = 6.1 width = 10;
    define par / analysis mean format = 4.1 width = 10;
RUN;
 
ODS HTML CLOSE;
ODS HTML;
SAS Output

Some Par 72 Pennsylvania Golf Courses

Name Year Type Yards
Toftrees 1968 Resort 7018
Penn State Blue 1921 Public 6525
Lewistown CC . Private 6779
Sinking Valley CC 1967 SemiPri 6755
Mount Airy Lodge 1972 Resort 7123

Average Size of Some PA Courses

Type Par Yards
Private 71.3 6553.3
Public 72.0 6525.0
Resort 72.0 7070.5
SemiPri 70.6 6394.8

Since the code is almost identical to the previous program, the only code that needs explanation this time around is that first ODS HTML statement. The PATH= option tells SAS where to store the subsequent files. The BODY=, CONTENTS=, and FRAME= specify the name of the output html files which store the body (the actual tables), the table of contents, and a combined webpage of the body and table of contents in the folder specified by PATH. The url=none option in PATH= essentially tells SAS to form HTML files that use relative paths to reference links. In this case, the body and contents file must be in the same folder for the frame file to work.

If you’re not familiar with at least the concept of Hypertext Markup Language (HTML), then you would find this topic quite challenging. In short, HTML is the behind-the-scenes language that tells your web browser what to display. If you go to any web page, and view the page source, you’ll see the HTML code that displays the web page that you are viewing. (Using Mozilla’s Firefox browser, you can view the page source by selecting View and then Page Source. Using an Internet Explorer browser, you can view the page source by selecting Page and then View Source). This topic concerns the pathnames that SAS creates when it creates HTML output files for you. If the pathnames aren’t well specified, then you would have trouble sharing your SAS-created HTML output files with others.

12.1.4. Producing Other Types of Output

Thus far, we have used ODS statements to tell SAS to create HTML output. As mentioned earlier, we can also use ODS statements to tell SAS to create other kinds of output. In this section, we’ll take a look at two examples in which we tell SAS to make different kinds of output. In the first example, we make RTF output that can be easily copied into Microsoft Word. In the second example, we make PDF output that can be then sent to a high-resolution printer.

Example

The following program tells SAS to print a subset of the penngolf data set and when doing so to send the output to an RTF destination:

ODS HTML CLOSE;
ODS RTF file = '/folders/myfolders/SAS_Notes/output/rtf/golf5.rtf'
    BODYTITLE;
        
PROC PRINT data = phc6089.penngolf NOOBS;
    title 'Some Par 72 Pennsylvania Golf Courses';
    ID name;
    var year type yards;
    where par = 72;
RUN;
        
ODS RTF CLOSE;
ODS HTML;
SAS Output

Some Par 72 Pennsylvania Golf Courses

Name Year Type Yards
Toftrees 1968 Resort 7018
Penn State Blue 1921 Public 6525
Lewistown CC . Private 6779
Sinking Valley CC 1967 SemiPri 6755
Mount Airy Lodge 1972 Resort 7123

As you can see, to tell SAS to send output to the RTF destination, we simply use the RTF keyword in an ODS statement. By default, titles and footnotes are put into Word headers and footers. The BODYTITLE option in the ODS RTF statement tells SAS to instead put titles and footnotes in the main part of the RTF document. Note that the second-to-last ODS statement tells SAS to close the RTF destination, while the last ODS statement tells SAS again to re-open the HTML destination.

If you are on Windows, then you may see a pop-up window asking to save or open the output rtf file. RTF files can be opened with word or pages.

Example

The following program does exactly the same thing as the previous program, except the output here is sent to a PDF file:

ODS HTML CLOSE;
ODS PDF file = '/folders/myfolders/SAS_Notes/output/pdf/golf5.rtf';
        
PROC PRINT data = phc6089.penngolf NOOBS;
    title 'Some Par 72 Pennsylvania Golf Courses';
    ID name;
    var year type yards;
    where par = 72;
RUN;
        
ODS PDF CLOSE;
ODS HTML;
SAS Output

Some Par 72 Pennsylvania Golf Courses

Name Year Type Yards
Toftrees 1968 Resort 7018
Penn State Blue 1921 Public 6525
Lewistown CC . Private 6779
Sinking Valley CC 1967 SemiPri 6755
Mount Airy Lodge 1972 Resort 7123

Pretty straightforward... as you can see, to tell SAS to send output to the PDF destination, we simply use the PDF keyword in an ODS statement. Note again that the second-to-last ODS statement tells SAS to close the PDF destination, while the last ODS statement tells SAS again to re-open the Listing destination.

12.1.5. Tracing and Selecting Procedure Output

As discussed earlier, when ODS receives data from a procedure, it combines the data component with a table definition to create an output object. For many procedures, ODS creates just one output object, while for others it produces several. Procedures involving a BY statement, for example, typically produce an output object for each BY group. When a procedure does create more than one output object, you might not want SAS to include all of them in your output. You might instead want to tell SAS to select just one or two of the output objects. In this section, we learn how to use the ODS TRACE and ODS SELECT statements to choose the specific output objects that you want SAS to display in your output.

The ODS TRACE ON statement tells SAS to print information in the log about the output objects created by all of the code in your program between the ODS TRACE ON statement and a closing ODS TRACE OFF statement.

Example

The following program uses ODS TRACE statements to capture information about the output objects created by the MEANS procedure on a data set called golfbypar, which is just a sorted version of the penngolf data set:

PROC SORT data = phc6089.penngolf out = golfbypar;
    by par;
RUN;
 
ODS TRACE ON;
PROC MEANS data = golfbypar;
    by par;
    title 'Pennsylvania Golf Courses by Par';
RUN;
ODS TRACE OFF;
SAS Output

Pennsylvania Golf Courses by Par

The MEANS Procedure

Variable N Mean Std Dev Minimum Maximum
ID
Year
Yards
Slope
USGA
3
3
3
3
3
108.3333333
1966.33
6283.33
126.0000000
70.2333333
2.0816660
6.5064071
295.6692972
6.0000000
1.0692677
106.0000000
1960.00
6004.00
120.0000000
69.3000000
110.0000000
1973.00
6593.00
132.0000000
71.4000000
Variable N Mean Std Dev Minimum Maximum
ID
Year
Yards
Slope
USGA
3
3
3
3
3
105.3333333
1934.33
6416.67
127.3333333
71.3333333
2.5166115
34.0196022
63.6893502
4.0414519
0.5131601
103.0000000
1909.00
6369.00
123.0000000
70.9000000
108.0000000
1973.00
6489.00
131.0000000
71.9000000
Variable N Mean Std Dev Minimum Maximum
ID
Year
Yards
Slope
USGA
5
4
5
5
5
105.0000000
1957.00
6840.00
131.8000000
73.2600000
4.0620192
24.0970261
235.5546646
5.7619441
1.0830512
101.0000000
1921.00
6525.00
125.0000000
72.0000000
111.0000000
1972.00
7123.00
140.0000000
74.3000000

The SORT procedure, of course, just sorts the permanent data set phc6089.penngolf by par and stores the sorted result in a temporary data set called golfbypar. Then, the ODS TRACE ON statement tells SAS to start capturing information about any output objects that are created. The MEANS procedure tells SAS to summarize the golfbypar data set for each level of par, that is, when par equals 70, 71 and 72. Finally, the ODS TRACE OFF statement tells SAS to stop capturing information about any output objects that are created.

Launch and run the SAS program. You can go ahead and review the output from the MEANS procedure, but what we're really interested in here is the information SAS displays about the output objects in the log window:

SAS log window with TRACE output for PROC MEANS

As the log suggests, the MEANS procedure creates one output object for each BY group (par = 70, par = 71, and par = 72). The three output objects share the same name, label, and template, but different paths. The path for the par = 70 output object, for example, is called Means.ByGroup1.Summary, while the path for the par = 71 output objects is called Means.ByGroup2.Summary. Once we know the names of the output objects, we can use an ODS SELECT statement to tell SAS the specific output objects that we want displayed. To select specific output objects, simply place an ODS SELECT statement within the relevant procedure. By default, the ODS SELECT statement lasts only for the procedure in which it is contained.

Example

The following program uses an ODS SELECT statement and what we learned from tracing our MEANS procedure to print just the portion of the output that pertains to the par 70 golf courses:

PROC MEANS data = golfbypar;
    by par;
    title 'Par 70 Golf Courses';
    ODS SELECT Means.ByGroup1.Summary;
RUN;

ODS SELECT ALL; *Reset selection to all output tables;
SAS Output

Par 70 Golf Courses

The MEANS Procedure

Variable N Mean Std Dev Minimum Maximum
ID
Year
Yards
Slope
USGA
3
3
3
3
3
108.3333333
1966.33
6283.33
126.0000000
70.2333333
2.0816660
6.5064071
295.6692972
6.0000000
1.0692677
106.0000000
1960.00
6004.00
120.0000000
69.3000000
110.0000000
1973.00
6593.00
132.0000000
71.4000000

Launch and run the SAS program, and review the output to convince yourself that SAS displays only the portion of the MEANS procedure that pertains to the par 70 golf courses shown above.

ODS SELECT (and likewise ODS EXCLUDE) have two special keywords ALL and NONE which all you to SELECT/EXCLUDE ALL or NONE of the output. Here, we use ODS SELECT ALL to reset SAS from only SELECTING the table Means.ByGroup1.Summary, so that subsequent SAS PROC class will produce output. Otherwise, only this currently selected table will show and all other output would be hidden.

12.1.6. Changing the Appearance of Your Output

have good news and bad news for you about changing the appearance of your output. The good news is that if you had enough time to learn all of the ways in which you could change the appearance of your SAS output, you could create just about anything you wanted. The bad news is that we don’t have enough time in this course to explore all of the possibilities. In fact, we’ll barely nibble the surface. In this section, we will only investigate how to use the ODS HTML statement’s STYLE= option to change the appearance of the default HTML output by using one of the many predefined style templates built into SAS.

Example

The following program uses the ODS HTML statement's STYLE= option to tell SAS to use the meadow style when displaying the HTML output created by printing a subset of the phc6089.penngolf data set:

ODS HTML file = '/folders/myfolders/SAS_Notes/output/html/golf9.html'
        style = meadow;
 
PROC PRINT data = phc6089.penngolf NOOBS;
   title 'Some of the penngolf data set variables';
   ID name;
   var year type par yards;
RUN;
 
ODS HTML CLOSE;
ODS HTML;
SAS Output

Some of the penngolf data set variables

Name Year Type Par Yards
Toftrees 1968 Resort 72 7018
Penn State Blue 1921 Public 72 6525
Centre Hills 1921 Private 71 6392
Lewistown CC . Private 72 6779
State College Elks 1973 SemiPri 71 6369
Park Hills CC 1966 SemiPri 70 6004
Sinking Valley CC 1967 SemiPri 72 6755
Williamsport CC 1909 Private 71 6489
Standing Stone GC 1973 SemiPri 70 6593
Bucknell GC 1960 SemiPri 70 6253
Mount Airy Lodge 1972 Resort 72 7123

As you can see, telling SAS what style to use is as simple as adding the STYLE= option to the ODS HTML statement. Launch and run the SAS program, and review the output to see the appearance of the HTML output when created using the meadow style template.

Of course you are asking yourself “how would I know that meadow is one of the available predefined styles?” Fortunately, the answer is simple enough. The following TEMPLATE procedure produces a list of the predefined style templates that are available on your system:


PROC TEMPLATE;
    LIST STYLES;
RUN;

Launch and run the SAS code, and review the output to see the list of predefined styles that are shipped with SAS. You might want to try some of the styles out yourself. While I do find some of the styles rather nice, I personally find some of them rather hideous (and therefore useless to me).

12.1.7. Creating SAS DATA Sets from Procedure Output

You may recall that earlier we used an OUTPUT statement in the MEANS procedure to create a data set containing summary statistics, such as means and standard deviations. We’ll see in this section that we could have alternatively used ODS to first save the summary statistics and then send it to the OUTPUT destination. In fact, we can use ODS to save just about any part of any procedure’s output!

Example

The following program uses an ODS OUTPUT statement to create a temporary SAS data set called summout from the Summary output object created by the MEANS procedure, and then prints the resulting summout data set:

PROC MEANS data = golfbypar;
    by par;
    var yards;
    title 'Pennsylvania Golf Courses by Par';
    ODS OUTPUT Summary = summout;
RUN;
 
PROC PRINT data = summout NOOBS;
    title 'The summout data set';
RUN;
SAS Output

Pennsylvania Golf Courses by Par

The MEANS Procedure

Analysis Variable : Yards
N Mean Std Dev Minimum Maximum
3 6283.33 295.6692972 6004.00 6593.00
Analysis Variable : Yards
N Mean Std Dev Minimum Maximum
3 6416.67 63.6893502 6369.00 6489.00
Analysis Variable : Yards
N Mean Std Dev Minimum Maximum
5 6840.00 235.5546646 6525.00 7123.00

The summout data set

Par Yards_N Yards_Mean Yards_StdDev Yards_Min Yards_Max
70 3 6283.3333333 295.66929724 6004 6593
71 3 6416.6666667 63.689350235 6369 6489
72 5 6840 235.55466457 6525 7123

Now, something that might not be obvious from this code is that the name of the output object, Summary, was determined from first tracing the MEANS procedure. If you refer back to the information SAS displayed in the log for the previous example using ODS TRACE, you can see that, since we want to capture all of the output from the MEANS procedure, the desired output object is called Summary. The ODS OUTPUT statement tells SAS that we want to save the data contained in the Summary output object in a data set called summout. Of course, the PRINT procedure then tells SAS to print the summout data set. Launch and run the SAS program, and review the output to convince yourself that the summout data set does indeed contain the data summarized by the MEANS procedure.

You do need to be careful where you put ODS statements in your program. For example, if rather than putting the ODS OUTPUT statement just before the MEAN procedure's RUN statement, we had instead put it after the MEAN procedure's RUN statement and before the PRINT procedure's PROC PRINT statement, we would not have captured the Summary data set. Instead, we would get the following Warning message:

    WARNING: Output 'Summary' was not created. Make sure that the output
	Object name, label, or path is spelled corectly. Also
    Verify that the appropriate procedute options are used to
    produce the request output object. For example, verify that
    the NOPRINT option is not used
    

You might want to move the ODS statement as described, and re-run the SAS program just to see this for yourself.

12.2. Using Plot Options to Control Graph Appearance

The ODS Graphics procedures also enable you to control the appearance of particular graphics elements in a graph. Graphics elements include lines, bars, markers, text, and so on.

Many ODS Graphics procedure statements have options and suboptions that control the appearance of different parts of a plot or graph. Default visual attributes of various graphics elements are derived from the specific style elements of the active style. By using appearance options in your procedure statements, you can change the appearance of one or more aspects of your graph without changing the overall style.

For most of our examplew, we will use the child mortality dataset, indicatordeadkids35.csv.

FILENAME mortcsv '/folders/myfolders/SAS_Notes/data/indicatordeadkids35.csv';

PROC IMPORT datafile = mortcsv 
            out = mort(RENAME=(VAR1=Country)) 
            dbms = CSV 
            replace;
    getnames = yes;
    guessingrows = max;
RUN;

DATA long;
  SET mort;
  ARRAY years{*} '1760'n -- '2009'n  '2010'n '2030'n '2050'n '2099'n;
  DO i = 1 to dim(years);
    year = INPUT(vname(years{i}), 4.);
    morts = years{i};
    OUTPUT;
  END;
  DROP i '1760'n -- '2009'n  '2010'n '2030'n '2050'n '2099'n;
RUN;

PROC CONTENTS data = long;
RUN;

PROC PRINT data = long (obs = 5);
RUN;
SAS Output

The SAS System

The CONTENTS Procedure

Data Set Name WORK.LONG Observations 50038
Member Type DATA Variables 3
Engine V9 Indexes 0
Created 10/02/2020 15:23:12 Observation Length 2632
Last Modified 10/02/2020 15:23:12 Deleted Observations 0
Protection   Compressed NO
Data Set Type   Sorted NO
Label      
Data Representation SOLARIS_X86_64, LINUX_X86_64, ALPHA_TRU64, LINUX_IA64    
Encoding utf-8 Unicode (UTF-8)    
Engine/Host Dependent Information
Data Set Page Size 131072
Number of Data Set Pages 1022
First Data Page 1
Max Obs per Page 49
Obs in First Data Page 48
Number of Data Set Repairs 0
Filename /tmp/SAS_work760E00000C05_localhost.localdomain/long.sas7bdat
Release Created 9.0401M6
Host Created Linux
Inode Number 671607
Access Permission rw-r--r--
Owner Name sasdemo
File Size 128MB
File Size (bytes) 134086656
Alphabetic List of Variables and Attributes
# Variable Type Len Format Informat
1 Country Char 2614 $2614. $2614.
3 morts Num 8    
2 year Num 8    

The SAS System

Obs Country year morts
1 Afghanistan 1760 .
2 Afghanistan 1761 .
3 Afghanistan 1762 .
4 Afghanistan 1763 .
5 Afghanistan 1764 .

12.2.1. The SG Plotting Procedures

Let’s quickly review how to make some basic plots using PROC SGPLOT before we move on to how to customize their appearance. PROC SGPLOT can be used to make many common plots by using the corresponding statement:

  • SCATTER - produces a scatterplot

  • VBOX/HBOX - produces a vertical/horizontal boxplot

  • SERIES - produces a lines/series plot

  • DENSITY - produces a density plot

  • HISTOGRAM - produces a histgram

  • REG - Adds a least squares regression line fit to the plot

  • LOESS - adds a nonparametric smoothing curve to the plot

For example, we could make a scatterplot of year vs mortality rate. Let’s do this for the data from Sweden.

DATA sweden_long;
   SET long;
   WHERE country = "Sweden";
RUN;

PROC SGPLOT data = sweden_long;
   SCATTER Y = morts X = year;
RUN;
SAS Output
The SGPlot Procedure

I could have also made a line plot by simply changing SCATTER to SERIES.

PROC SGPLOT data = sweden_long;
   SERIES Y = morts X = year;
RUN;
SAS Output
The SGPlot Procedure

By using the LOESS statement, I can also add a nonparametric smoothing curve to this data and plot it ontop of the series plot.

PROC SGPLOT data = sweden_long;
   SERIES Y = morts X = year;
   LOESS  Y = morts X = year / smooth = 0.45 CLM;
RUN;
SAS Output
The SGPlot Procedure

Now, let’s look at the mortality rates over time using line plots for each of the countries: United States, United Kingdom, Sweden, Afghanistan, Rwanda. To get a line for each country individually, we need to specify the GROUP= option and assign it to the country variable.

DATA sub;
   SET long;
   WHERE country in ("United States" "United Kingdom" 
                     "Sweden" "Afghanistan" "Rwanda");
RUN;

PROC SGPLOT data = sub;
   SERIES Y = morts X = year / group = country;
RUN;
SAS Output
The SGPlot Procedure

Note that we get a single plot with trajectory over time of the mortality rates for each of these five countries. PROC SGPLOT will automatically assign default colors to differentiate each group and provide a legend. We will learn how to modify these later.

We could also make side by side boxplots of mortality rates by country by using the category= option in vbox/hbox.

PROC SGPLOT data = sub;
   VBOX morts / category = country;
RUN;
SAS Output
The SGPlot Procedure

12.2.2. Axes and Titles

The first adjustment we might want to make to a plot is adding descriptive axis labels and a title. We can set the axis labels using the XAXIS and YAXIS LABEL= option (or we can set a LABEL to the variable in a DATA step). To set different levels of titles, we use the TITLE statements. The TITLE statement allows TITLE1-TITLE10 where as the TITLE number increase the size of the title decrease, so we can make subtitles. Note that the TITLE statements are global and not part of SGPLOT, so we will need to reset these or change them before making another plot in order to prevent the same titles from carrying over.

PROC SGPLOT data = sub;
   SERIES Y = morts X = year / group = country;
   XAXIS LABEL = "Year";
   YAXIS LABEL = "Mortality Rate";
   TITLE "Child Mortality Rates";
   TITLE2 "Stratified by Country";
RUN;
SAS Output
The SGPlot Procedure

The x and y axis limits can be adjusted by using the MIN= and MAX= options in the XAXIS and YAXIS statments. For example, we can zoom in on the years 1900-2000 for the bottom three lines where the mortality rates range from 0 to 1.5.

PROC SGPLOT data = sub;
   SERIES Y = morts X = year / group = country;
   XAXIS LABEL = "Year" MIN = 1900 MAX = 2000;
   YAXIS LABEL = "Mortality Rate" MIN = 0 MAX = 1.5;
   TITLE "Child Mortality Rates";
   TITLE2 "Stratified by Country";
RUN;
SAS Output
The SGPlot Procedure

We may also want to change the default tic marks on the x and or y axes. This can be done using the VALUES= option in the XAXIS and YAXIS statements.

PROC SGPLOT data = sub;
   SERIES Y = morts X = year / group = country;
   XAXIS LABEL = "Year" VALUES = (1750 TO 2100 BY 50);
   YAXIS LABEL = "Mortality Rate" VALUES = (0 TO 5 BY 0.5);
   TITLE "Child Mortality Rates";
   TITLE2 "Stratified by Country";
RUN;
SAS Output
The SGPlot Procedure

If you want to change the text displayed at each of these tic marks to be different from the actual numbers, use the VALUESDISPLAY= option.

PROC SGPLOT data = sub;
   SERIES Y = morts X = year / group = country;
   XAXIS LABEL = "Year" VALUES = (1750 TO 2100 BY 50)
         VALUESDISPLAY = ('a' 'b' 'c' 'd' 'e' 'f' 'g' 'h');
   YAXIS LABEL = "Mortality Rate" VALUES = (0 TO 5 BY 0.5);
   TITLE "Child Mortality Rates";
   TITLE2 "Stratified by Country";
RUN;
SAS Output