Contents
Handling SAS Output Under Linux
Bob Jackson
Department of Sociology
Duke University
Version 1.0
September 2007
Herein are described basic methods handling of Linux SAS output on departmental compute servers verstehen and paradigm.
There are three network printers to which Linux output may be directed via print spooling commands. Each printer is referenced by its mneumonic name:
lab - HP LaserJet 4250 located in the Room 135 Computer Lab
copy2 - HP LaserJet 4100 located in the 258 second floor Copy Room
color - HP Color LaserJet 4700 located in the 268 Main Office subject to the limitations below
- use only for color SAS/GRAPH output
- available during business hours when main office is open
available only from verstehen
1. Interactive Mode Output
SAS interactive output is written to the Log and Output windows. The former is a listing of the SAS code as executed along with notes, warnings and error messages. The latter is the output produced by SAS procedures.
Output from either window is printed by selecting the File > Print... menu option. The SAS:Print window opens. The first time used it will look as follows:
Click on the Use Forms button in the upper, left corner to alter window content to the following [subsequent SAS sessions will default to Forms printing]:
This window presents the system-wide printing defaults for SAS text output. The enscript spooling command converts ASCII files to nicely formatted PostScript output printed two pages per sheet in landscape orientation. The printer name designates the default printer, which is set to copy2. Change copy2 to lab to print on the Computer Lab printer.
It is best practice to save SAS Log and Output window content to files when a permanent record is needed. Save to files with File > Save As... menu sequences. Log window output will default to a .log filename extension and Output window contents to a .lst extension. Use file names that match those of the SAS program file. When working remotely, output files are easily downloaded to your local PC with a secure file transfer program.
2. Batch Mode Ouput
To the novice SAS user batch mode seems arcane, but the experienced SAS user comes to appreciate it for several reasons:
- The SAS user interface may become cumbersome and annoying after a while.
- Batch mode captures output files in a rigorous fashion that well satisfies research audit trail requirements.
- Batch mode allows background execution of large jobs that may require hours or days to complete.
When run in batch mode, a SAS program will produce log and listing files. A log file (with file extension .log) is an authoritative listing of the SAS program as it is run accompanied by SAS notes, warnings and error messages. A listing file (with file extension .lst) is typically the output from any SAS procedure steps run by the job.
Batch mode SAS is run from the Linux command line, as demonstrated below. The command invokes SAS and instructs it to run the program sg101a.sas. By convention, SAS programs are given the extension .sas. The & (ampersand) instructs SAS to run in the background and return the user command prompt.
bobj:~/sas/sasgraph > sas sg101a.sas & bobj:~/sas/sasgraph >
Execution of program sg101a.sas produces in the same directory the log file sg101a.log. If the program also generates procedural output, it is written to sg101a.lst. When debugging a SAS program, the listing file is often not produced until the program runs without errors. Batch programs can be modified and rerun. Each time it is run with the same filename, the log files are overwritten. So when doing a series of runs, make sure that each is done from a uniquely named SAS control statement file. Duplicate a SAS program with the copy (cp) statement and then modify it.
bobj:~/sas/sasgraph > cp sg101a.sas sg101b.sas
A SAS batch program is self-contained and independent of other SAS programs. This means that the SAS data sets used by the program must come from a permanent SAS library or be created by one or more DATA steps within the program. The WORK library exists only for the duration of the batch job.
Print batch output files from the command line with the enscript command. The command specifies the file to print, where to print it and what characteristics to apply to the output. Standard log and listing files are ASCII files. To conserve paper, print them in landscape orientation with 2 pages per sheet.
bobj:~/sas/sasgraph > enscript -2rG -Plab sg101a.log sg101a.lst
Breaking the above command options down:
-2rG - rotate (r) to landscape orientation, with 2 sheets per page and gaudy (G) headers.
-Plab - printer (P) destination of lab (or the alternate printer destination copy2).
sg101a.log sg101a.lst - log and listing files to be printed - each is printed as a separate output.
The second example below uses the alternate printer destination and adds a device (D) key to force duplex (Duplex:true) printing.
bobj:~/sas/sasgraph > enscript -2rG -Pcopy2 -DDuplex:true sg101a.log
Use the man enscript command to display the online manual page for a full list of options. Use the space bar to page forward through the manual, the b key to page backward, and the q key to terminate the listing.
3. Graphical Output
Building SAS/GRAPH output can be a daunting task. The tutorial by Mike Zdeb and Robert Allison, SAS/GRAPH 101, nicely deconstructs the steps by systematically refining a basic plot to presentation-ready status. By tweaking an example from this paper, we illustrate how to produce SAS/GRAPH output for various purposes:
A document suitable for use in a web page using PNG (Portable Network Graphic) format
A standalone document you can view or print from Linux or Windows as a PDF (Portable Document Format)
A document suitable for inclusion in a Word document using PostScript format
Fonts and Devices: Fonts in SAS/GRAPH are delivered as software fonts provided by SAS or as device specific hardware fonts. SAS supplies Bitstream software fonts from several font families, including Century, Hershey, Swiss and Zapf for all operating systems. Hardware fonts are associated with a particular graphics device supported by SAS. A graphics device can be a physical device such as a specific laser printer with standard built-in fonts or it can a be a particular type of output format (such as PDF), which allows a range of Adobe fonts.
We now proceed with a series of examples that incorporate slight variations to achieve different types of output. All are based on Example 14 from the Zdeb and Allison tutorial.
3.1. Example 1: Viewing Graph in a Display Manager Window
Below is the code used to generate the graph in an interactive SAS session and view it on screen in a Display Manager window. A typical approach would be to develop a graph like this in steps and viewing the results on the screen until you have a satisfactory product.
* Two line plot with a customized legend; * Adapted from SAS/GRAPH 101 - Example 14; * Filename: sg101p0.sas; options validvarname=upcase; goptions reset=all ftext=swiss htext=2 gunit=pct ctext=green csymbol=blue; symbol1 v=dot i=join h=2.5 l=1; symbol2 v=dot i=join h=2.5 l=3; axis1 label=(angle=90 "AMOUNT (IN BILLIONS)") minor=(n=3); axis2 order=(1970 to 2000 by 5) minor=(n=4) offset=(2,2); legend1 label=none value=(j=left "TOTAL" j=left "FEDERAL GOVT") mode=protect position=(top inside left) cborder=blue cshadow=blue across=1 shape=line(10); title h=4 f=swissb "National Health Care Expenditures: 1970-2000"; footnote j=right "Source: Health-United States-2003"; proc gplot data=t112; plot (nh_tot fg_tot)*year/overlay vaxis=axis1 haxis=axis2 caxis=blue legend=legend1; format nh_tot comma.; run; quit;
Note the following:
The GOPTIONS statement sets the overall defaults for SAS/GRAPH. Using reset=all as the first option clears all graphics options back to their defaults and cancels all global definitions such as SYMBOL, AXIS, LEGEND, TITLE and FOOTNOTE statements. You may not wish to reset, but doing so ensures the graph is completely encapsulated within this body of code and is not affected by prior statements. The ftext=swiss option defines the Bitstream Swiss software font as the default font for this graph.
goptions reset=all ftext=swiss htext=2 gunit=pct ctext=green csymbol=blue;
The TITLE statement provides specific font settings that override those of GOPTIONS. Height is set to 4 units (h=4) and the font is set to Bold Swiss (f=swissb).
title h=4 f=swissb "National Health Care Expenditures: 1970-2000";
3.2. Example 2: Output a Portable Network Graphic
Minor modification to the first example will produce a PNG file.
* Two line plot with a customized legend output to a PNG file; * Adapted from SAS/GRAPH 101 - Example 14; * Filename: sg101p1.sas; filename gout1 '~bobj/sas/sasgraph/ex14.png'; options validvarname=upcase; goptions reset=all device=png gsfname=gout1 xpixels=760 ypixels=570 ftext=swiss htext=2 gunit=pct ctext=green csymbol=blue; symbol1 v=dot i=join h=2.5 l=1; symbol2 v=dot i=join h=2.5 l=3; axis1 label=(angle=90 "AMOUNT (IN BILLIONS)") minor=(n=3); axis2 order=(1970 to 2000 by 5) minor=(n=4) offset=(2,2); legend1 label=none value=(j=left "TOTAL" j=left "FEDERAL GOVT") mode=protect position=(top inside left) cborder=blue cshadow=blue across=1 shape=line(10); title h=4 f=swiss "National Health Care Expenditures: 1970-2000"; footnote j=right "Source: Health-United States-2003"; proc gplot data=t112; plot (nh_tot fg_tot)*year/overlay vaxis=axis1 haxis=axis2 caxis=blue legend=legend1; format nh_tot comma.; run; quit;
Note the following:
A FILENAME statement is added with FILEREF gout1 to specify the location of the output PNG file.
filename gout1 '~bobj/sas/sasgraph/ex14.png';
- The GOPTIONS statement is modified to include the options required to produce the PNG file.
goptions reset=all device=png gsfname=gout1 xpixels=760 ypixels=570
- ftext=swiss htext=2 gunit=pct ctext=green csymbol=blue;
device=png specifies Portable Network Graphic as the device driver. The PNG format is an open, extensible standard for producing compressed graphic images suitable for web publication.
gsfname=gout1 provides the FILEREF back to the FILENAME defining the output location.
xpixels=760 ypixels=570 specify the size of the image in pixels. These settings preserve a 4:3 aspect ratio and set an image size that fits the document display area.
The resulting graph is incorporated into this document below:
3.3. Example 3: Output a PDF
Due to the better quality of results, we illustrate a bit more complicated approach to producing a PDF document involving use of the PDF Universal Printer controlled through the SAS Output Delivery System. [These tools can be used to write a series of tables and graphs to one PDF document. See SAS Technical Support Document TS-659 Exporting SAS/GRAPH Output to PDF Files
* Two line plot with a customized legend output to a PDF file; * Adapted from SAS/GRAPH 101 - Example 14; * Filename: sg101p2.sas; options orientation=landscape validvarname=upcase; goptions reset=all device=sasprtc ftext='Helvetica' htext=2 gunit=pct ctext=green csymbol=blue; symbol1 v=dot i=join h=2.5 l=1; symbol2 v=dot i=join h=2.5 l=3; axis1 label=(angle=90 "AMOUNT (IN BILLIONS)") minor=(n=3); axis2 order=(1970 to 2000 by 5) minor=(n=4) offset=(2,2); legend1 label=none value=(j=left "TOTAL" j=left "FEDERAL GOVT") mode=protect position=(top inside left) cborder=blue cshadow=blue across=1 shape=line(10); title h=4 f='Helvetica/bo' "National Health Care Expenditures: 1970-2000"; footnote j=right "Source: Health-United States-2003"; ods listing close; ods pdf file='~bobj/sas/sasgraph/ex14.pdf' notoc; proc gplot data=t112; plot (nh_tot fg_tot)*year/overlay vaxis=axis1 haxis=axis2 caxis=blue legend=legend1; format nh_tot comma.; run; quit; ods pdf close; ods listing;
Note the following:
- The FILENAME statement is dropped. Output file handling is done later via an ODS statement.
The OPTIONS statement contains an orientation=landscape specification to rotate the image on the page.
options orientation=landscape validvarname=upcase;
The GOPTIONS statement includes the device=sasprtc to direct graphics output to default universal printer and sets the ftext='Helvetica' font specification, which must be enclosed in quotes. Helvetica is one of the 14 base fonts included in every Adobe Acrobat reader, so we switch to that from the Swiss font to ensure readability.
goptions reset=all device=sasprtc ftext='Helvetica'
- htext=2 gunit=pct ctext=green csymbol=blue;
The TITLE statement also includes a font change with the f='Helvetica/bo' specification. Universal Printer font references requires that modifications to the font (i.e.; bold, italic, oblique) be listed after the slash. In this case we want Helvetica Bold. Note that SAS accepts bo as an abbreviation for bold.
title h=4 f='Helvetica/bo' "National Health Care Expenditures: 1970-2000";
- The names of the 14 base fonts available with the PDF Universal Printer are:
Courier Courier/oblique Courier/bold Courier/bold/oblique Helvetica Helvetica/oblique Helvetica/bold Helvetica/bold/oblique Times Times/italic Times/bold Times/bold/italic Symbol ITC Zapf Dingbats
The sequence of ODS commands controls the output stream. Two come before the GPLOT procedure. The first curtails SAS/GRAPH output to any of the standard listing or display windows. The second opens the PDF Universal Printer for output to a specific PDF file. The notoc option means to include no table of contents.
ods pdf file='~bobj/sas/sasgraph/ex14.pdf' notoc;
- After the GPLOT procedure runs, the output stream to the PDF Universal Printer is closed and the standard output destinations are reopened.
You may open a PDF graph on verstehen with acroread (the Linux version of Adobe Acrobat) and print it to a standard or color printer. When using the File>Print menu sequence to print a color graph, make sure that the printer command is specified as /usr/bin/lp -d color.
bobj: ~/sas/sasgraph > acroread ex14.pdf &
A PDF graph can be included in a personal web page and accessed with the Acrobat plugin of your browser.
3.4. Example 4: Output a PostScript File
A PostScript image provides the best method for inserting a graph into a word processing document, as illustrated with this final example.
* Two line plot with a customized legend output to a PostScript file; * Adapted from SAS/GRAPH 101 - Example 14; * Filename: sg101p3.sas; filename gout3 '~bobj/sas/sasgraph/ex14.ps'; options validvarname=upcase; goptions reset=all device=ps gsfname=gout3 ftext='Helvetica' htext=2 gunit=pct ctext=green csymbol=blue; symbol1 v=dot i=join h=2.5 l=1; symbol2 v=dot i=join h=2.5 l=3; axis1 label=(angle=90 "AMOUNT (IN BILLIONS)") minor=(n=3); axis2 order=(1970 to 2000 by 5) minor=(n=4) offset=(2,2); legend1 label=none value=(j=left "TOTAL" j=left "FEDERAL GOVT") mode=protect position=(top inside left) cborder=blue cshadow=blue across=1 shape=line(10); title h=4 f='Helvetica-Bold' "National Health Care Expenditures: 1970-2000"; footnote j=right "Source: Health-United States-2003"; proc gplot data=t112; plot (nh_tot fg_tot)*year/overlay vaxis=axis1 haxis=axis2 caxis=blue legend=legend1; format nh_tot comma.; run; quit;
Note the following:
A FILENAME statement is required to specify the output file, which should have a .ps extension.
The GOPTIONS statement includes a PostScript device specification (device=ps) and requires a font specification consistent with the ps device. Available PostScript fonts are the same as for a PDF file, so we can use ftext='Helvetica'.
goptions reset=all device=ps gsfname=gout3 ftext='Helvetica'
- htext=2 gunit=pct ctext=green csymbol=blue;
The TITLE statement requires a different syntax for the font specification (f='Helvetica-Bold'). The 14 PostScript device fonts are the same as those of a PDF, but are named differently:
Courier Courier-Oblique Courier-Bold Courier-BoldOblique Times-Roman Times-Italic Times-Bold Times-BoldItalic Helvetica Helvetica-Oblique Helvetica-Bold Helvetica-BoldOblique Symbol ZapfDingbats
A PostScript file does not preserve color, so color attributes are translated to grayscale equivalents by SAS.
To incorporate a PostScript image into a Word 2003 document:
- Open a Word document.
Select Insert > Picture > From File....
- Navigate to the file location.
Set Files of type to All Files (*.*).
- Double-click the desired file.
In the Convert File dialog box, select Encapsulated PostScript and click OK.
- The document is loaded. It may be resized and repositioned.
4. References
SAS Technical Support Services, "Exporting SAS/GRAPH Output to PDF Files," Technical Support Document TS-659, SAS Institute, 2004 - http://support.sas.com/techsup/technote/ts659/ts659.html
Social Science Computing Cooperative, "Saving SAS Graphs for Printing or Other Uses," Publication 4-20, University of Wisconsin, 2007 - http://www.ssc.wisc.edu/sscc/pubs/4-20.htm
Mike Zdeb and Robert Allison, "SAS/GRAPH 101," SUGI 31 Proceedings, SAS Institute, 2006 - http://www2.sas.com/proceedings/sugi31/239-31.pdf