James Blum

Fundamentals of Programming in SAS


Скачать книгу

1-5-4.rtf’ style=journal;

      ods pdf file=’PDF Output 1-5-4.pdf’;

      ods trace on;

      proc corr data=sashelp.cars;

      var mpg_city;

      with mpg_highway;

      ods select pearsoncorr;

      run;

      ods rtf exclude onewayfreqs;

      proc freq data=sashelp.cars;

      table type;

      run;

      ods pdf exclude summary;

      proc means data=sashelp.cars;

      class origin;

      var mpg_city;

      run;

      ods rtf close;

      ods pdf close;

       This opens an RTF destination and applies a style named Journal to it. The style templates available in a given SAS session can be viewed via PROC TEMPLATE, see Chapter Note 8 in Section 1.7 for details. Most tables in this book are the result of delivery to RTF with a template called CustomSapphire—the code required to create the CustomSapphire template is provided in the code that comes with the book, and details about how to set it up and use it are given in Chapter Note 9 in Section 1.7.

       This opens a PDF destination, since no STYLE= option is provided, the default style template is used.

       The ODS TRACE ON statement is included to ensure that information about output objects is transmitted to the SAS log. To understand the role of  and , review this information in the log.

       This PROC FREQ generates a single table named OneWayFreqs. Rather than using ODS EXCLUDE, which would leave it out of both destinations, ODS RTF EXCLUDE keeps it out of the RTF file, but it is included in the PDF file—see Output 1.5.4A and 1.5.4B.

       PROC MEANS generates only one table named SUMMARY, and this statement stops it from being put into the PDF file, but it does get delivered to the RTF file.

       The ODS RTF CLOSE and ODS PDF CLOSE statements close the destinations opened in  and , completing the writing of those files. In this case, as both destinations are effectively closed at the same time, a single ODS _ALL_ CLOSE statement can replace these two statements.

      Output 1.5.4A: Multiple Output Destinations—RTF Results

Pearson Correlation Coefficients, N = 428Prob > |r| under H0: Rho=0
MPG_City
MPG_HighwayMPG (Highway)0.94102<.0001
Analysis Variable : MPG_City MPG (City)
OriginN ObsNMeanStd DevMinimumMaximum
Asia15815822.01265826.733306613.000000060.0000000
Europe12312318.73170733.289509312.000000038.0000000
USA14714719.07482993.982992010.000000029.0000000

      Output 1.5.4B: Multiple Output Destinations—PDF Results

Pearson Correlation Coefficients, N = 428 Prob > |r| under H0: Rho=0
MPG_City
MPG_HighwayMPG (Highway)0.94102<.0001
TypeFrequencyPercentCumulative FrequencyCumulative Percent
Hybrid30.7030.70
SUV6014.026314.72
Sedan26261.2132575.93
Sports4911.4537487.38
Truck245.6139892.99
Wagon307.01428100.00

      This chapter concludes with a review of the sample programs presented previously, highlighting language rules and variations in style and structure that are permitted.

      The following rules govern the structure of SAS programs:

       The SAS language is not case-sensitive. For example, PROC SGPLOT, proc SGPLOT, Proc SGplot, and other casing variations are all equivalent. This applies to all statements, names, functions, keywords, and other SAS language elements.

       In general, SAS statements end with a semicolon; otherwise, the SAS language is relatively free-form. The line breaks and indentations in Program 1.4.1, for example, are chosen to improve readability. In fact, as seen in later chapters, there are times when it is helpful to write one SAS statement across many lines with several levels of indentation. Good programming practice relating to code structure includes two fundamental rules:Develop standards for easy readability of codeFollow these standards consistently

       Comments are available in SAS code, and good programming practice requires that code is commented to a level that makes its method and purpose clear. Two ways to write comments are shown in the following samples:/*this is a comment*/*this is also a comment;

      SAS language elements such as statements, names, functions, and keywords follow a standard set of naming conventions, as follows:

       Permitted characters include letters, numbers, and underscores.

       Names must begin with a letter or underscore

       Maximum length is 32 characters

      Some methods are available to avoid these rules when it is deemed necessary, such as connections to other data sources that follow different rules, see Chapter Note 10 in Section 1.7 for more information. There are also some exceptions to these rules for certain SAS language elements. For example, the limitation to 8 characters for a library reference is discussed in Section 1.4.3. SAS formats, introduced in Chapter 2, are another example of a language element having some exceptions to these rules; however, most language elements, including data set and variable names, follow all three exactly.

      Text literal values, such as title text or file paths, are encased in single or double quotation marks, as long as the opening quotation mark type matches the closing quotation mark. Be careful when copying text from other applications into SAS, various characters that fill the role of a quotation mark in other software are not interpreted in that manner by SAS—the color coding in the editors is often helpful in diagnosing this problem. In the role of a path, the first example of a text literal given below is generally interpreted as distinct from the second and third due to spacing. However, whether the second and third are taken as distinct due to casing is dependent on the operating system—for example, Microsoft Windows is not case-sensitive.

      1. ‘C:\MyFolder’

      2. ‘C:\My Folder’

      3. ‘C:\my folder’

      1. Managing Results in the SAS Windowing Environment. In the SAS windowing environment, under default conditions, results of code submissions are cumulative in the HTML destination and in the Log window. If the listing destination is active, results are also cumulative in the Output window. For the Log and Output windows, the command Clear All from the Edit menu is used to clear either of these provided that window is active (take care not to use this command when an Editor window is active). Since the SAS windowing environment only allows for a single Log window and a single Output window during the session, the New command (from the File menu or using the toolbar button) also clears either window when active. Managing the HTML window is a bit more difficult; it too can be controlled by the ODS statements shown in this chapter. See the SAS Documentation for additional details. By default, SAS University Edition replaces the log and results on any code submission, so these steps are not necessary. As stated in Chapter Note 2, SAS University Edition also supports interactive