Fundamentals of Programming in SAS. James Blum

Читать онлайн книгу.

Fundamentals of Programming in SAS - James Blum


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

      title ‘MPG Five-Number Summary’;

      title2 ‘Across Types’;

      proc means data=cars min q1 median q3 max maxdec=1;

      class typeB;

      var mpg:;

      run;

      After downloading the code to a known directory, there are multiple ways to navigate to and open this code. Figure 1.3.2 shows two methods to open the file, each requiring the Editor window to be active.

      Figure 1.3.2: Methods for Opening SAS Code Files in the SAS Windowing Environment

Figure 1.3.2: Methods for Opening SAS Code Files in the SAS Windowing Environment

      Either of these choices launches a standard Microsoft Windows file selection window, which is used to navigate to and select the file of interest. Upon successful selection of the code, it appears in the Editor window, and is displayed with some color coding as shown in Figure 1.3.3 (assuming the Enhanced Program Editor is in use, the Program Editor window provides different color coding). It is not important to understand the specific syntax or how the code works at this point, for now it is used simply to provide an executable program to introduce some SAS fundamentals.

      Code submission can also occur in multiple ways, two of which are shown in Figure 1.3.3, again each method requires the Editor window to be the active window in the session. If multiple Editor windows are open, only code from the active window is submitted.

      Figure 1.3.3: Submitting SAS Code in the SAS Windowing Environment

Figure 1.3.3: Submitting SAS Code in the SAS Windowing Environment

      Typically, after any code submission the Results window activates and displays an index of links to various entities produced by the program, including output tables. While not all SAS code generates output, Program 1.3.1 does, and it may be routed to different destinations (and possibly more than one destination simultaneously) depending on the version of SAS in use and current option settings.

      In SAS 9.4, the default settings route output to an HTML file which is displayed in the Results Viewer, a viewing window internal to the SAS session. Previous versions of SAS rely on the Output window for tables, an option which remains available for use in the SAS 9.4 windowing environment, and other specialized destinations for graphics. Default output options can be set by navigating to the Tools menu, selecting Options, followed by Preferences from that sub-menu, and choosing the Results tab in the window that appears, as shown in Figure 1.3.4.

      Figure 1.3.4: Managing Output for Program Submissions

Figure 1.3.4: Managing Output for Program Submissions

      Among other options, Figure 1.3.4 shows the option for Create HTML checked and Create Listing unchecked. For tables, the listing destination is the Output window, so when Create Listing is checked, tables also appear in the Output window in what appears as a plain text form. It is possible to check both boxes, and it is also possible to check neither, whichever is preferred.

      In the remainder of this book, output tables are shown in an RTF form embedded inside the book text, outside of any SAS Results window. Appearance of output tables and graphs in the book is similar to what is produced by a SAS session, but is not necessarily identical when default session options are in place. Later in this chapter, the ability to use SAS code to control delivery of output to each of these destinations is demonstrated, along with use of the listing destination as an output destination for graphics files.

      SAS Studio and SAS University Edition (which are, for the remainder of this text, singularly referred to as SAS University Edition) interface with SAS through a web browser. Typically, the browser used is the default browser for the machine hosting the SAS University Edition session, but this is not a requirement. Figure 1.3.5 shows a typical result of launching SAS University Edition (in this case using the Firefox browser on Microsoft Windows), launching in visual programmer mode by default. A closer match to the structure of the SAS windowing environment is provided by selecting SAS Programmer from the toolbar as shown.

      Figure 1.3.5: SAS University Edition

Figure 1.3.5: SAS University Edition

      Opening a program is accomplished via the Open icon on the toolbar, as illustrated in Figure 1.3.6, and the opened code is displayed in a manner very similar to the that of the Enhanced Program Editor display shown in Section 1.3.1.

      Figure 1.3.6: Opening a Program in SAS University Edition

Figure 1.3.6: Opening a Program in SAS University Edition

      Though in a different position, the toolbar icon for submission is the same as in the SAS windowing environment, and selecting it produces output in the Results tab as shown in Figure 1.3.7.

      Figure 1.3.7: Execution of a Program in SAS University Edition

Figure 1.3.7: Execution of a Program in SAS University Edition

      A few important differences to note in SAS University Edition: first, the output is displayed starting at the top, rather than at the bottom of the page as in the SAS windowing environment. Second, there is an additional tab for Output Data in this session. In Section 1.4.3, libraries, data sets, and navigation to each are discussed; however, SAS University Edition also includes a special tab whenever a program generates new data sets, which aids in directly viewing those results. Finally, note that the Code, Log, Results, and Output Data tabs are contained within the Program 1.3.1 tab, and each program opened is given its own set of tabs. In contrast, the SAS windowing environment supports multiple Editor windows in a single session, but they all share a common Log window, Output window, and (under default conditions) output HTML file. As discussed in other examples and in Chapter Notes 1 and 2 in Section 1.7, submissions from any and all Editor windows in the SAS windowing environment are cumulative in the Log and Output windows; therefore, managing results in each environment is quite different.

      To build an initial understanding of how to work with programs in SAS, Program 1.3.1 is used repeatedly in this section to introduce various SAS language elements and concepts. For both SAS windowing environment and SAS University Edition, the features of each environment and navigation within them are discussed in conjunction with the language elements that relate to them.

      Program 1.4.1 is a duplicate of Program 1.3.1 with certain elements noted numerically throughout the code, followed by notes on the specific code in the indicated position. Throughout this book, this style is used to detail important features found in sample code.

      Program 1.4.1: Program 1.3.1, Revisited

      options ps=100 ls=90 number pageno=1 nodate;

      data work.cars;

      set sashelp.cars;

      MPG_Combo=0.6*mpg_city+0.4*mpg_highway;

      select(type);

      when(‘Sedan’,’Wagon’) TypeB=’Sedan/Wagon’;

      when(‘SUV’,’Truck’) TypeB=’SUV/Truck’;

      otherwise TypeB=type;

      end;

      label mpg_combo=’Combined MPG’ typeB=’Simplified Type’;

      run;

      title


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