Fundamentals of Programming in SAS. James Blum

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

Fundamentals of Programming in SAS - James Blum


Скачать книгу
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;

       The DATA statement that opens this DATA step names a SAS data set Cars in the Work library. Work.Cars is populated using the SAS data set referenced in the SET statement, also named Cars and located in the Sashelp library. Data set references are generally two-level references of the form library.dataset. The exception to this is the Work library, which is taken as the default library if only a data set name is provided. Details on navigating through libraries and data sets are given in Section 1.4.3.

       MPG_Combo is a variable defined via an arithmetic expression on two of the existing variables from the Cars data set in the Sashelp library. Assignments of the form variable = expression; do not require any explicit declaration of variable type to precede them, the compilation process determines the appropriate variable type from the expression itself. SAS data set variables are limited to two types: character or numeric.

       The variable TypeB is defined via assignment statements chosen conditionally based on the value of the Type variable. The casing of the literal values is an exact match for the casing in the data set as shown subsequently in Figures 1.4.4 and 1.4.5—matching of character values includes all casing and spacing. Various forms of conditional logic are available in the DATA step.

       Naming conventions in SAS generally follow three rules, with some exceptions noted later. Names are permitted to include only letters, numbers, and underscores; must begin with a letter or underscore; and are limited to 32 characters. Given these naming limitations, labels are available to provide more flexible descriptions for the variable. (Labels are also available for data sets and other entities.) Also note that references to the variables MPG_Combo and TypeB use different casing here than in their assignment expressions; in general, the SAS language is not case-sensitive.

      Program 1.4.1 involves data sets in each of its programming steps. The DATA step uses one data set as the foundation for creating another, and the data set it creates is used in each of the PROC steps that follow. Again, data set references are generally in a two-level form of library.dataset, other than the exception for the Work library noted in the discussion of Program 1.4.3. The PROC steps in Program 1.4.1 each use one of the possible forms to reference the Cars data located in the Work library.

      Navigation to data sets in various libraries is possible in either the SAS windowing environment or SAS University Edition. In the SAS windowing environment, the Explorer window permits navigation to any assigned library, while in SAS University Edition, the left panel contains a section for libraries. In either setting, opening a library potentially reveals a series of table icons representing various SAS data sets, which can be opened to view the contents of the data. As an example, navigation to and opening of the Cars data set in the Sashelp library is shown below for each of the SAS windowing environment and SAS University Edition. Figures 1.4.3 and 1.4.4 demonstrate one way to open the Cars data in the SAS windowing environment.

      Figure 1.4.3: Starting Points for Library Navigation, SAS Windowing Environment

Figure 1.4.3: Starting Points for Library Navigation, SAS Windowing Environment

      Figure 1.4.4: Accessing the Cars Data Set in the Sashelp Library in the Windowing Environment

Figure 1.4.4: Accessing the Cars Data Set in the Sashelp Library in the Windowing Environment

      Figure 1.4.5 shows how to open the Cars data set in a SAS University Edition session, revealing several differences in the library navigation and the data view, which opens in a separate tab in the University Edition session.

      Figure 1.4.5: Accessing the Cars Data Set in the Sashelp Library in University Edition

Figure 1.4.5: Accessing the Cars Data Set in the Sashelp Library in University Edition

      In the SAS windowing environment, options for the data view are driven by menus and toolbar buttons for the active window, while in SAS University Edition, each data set tab contains a set of buttons and menus in its toolbar. As part of this tab, SAS University Edition also offers boxes to select a subset of variables and gives properties for each variable as it is selected. Such changes are possible in the SAS windowing environment as well, but are menu-driven. For more information, see the SAS Documentation for the chosen environment.

      Another major difference between the two data views is that the ViewTable in the SAS windowing environment has active control over the data set selected. The view in SAS University Edition is generated when the data set is opened, or re-generated if new options are selected, and control of the data set is released. For further detail on the implications of these differences, see Chapter Note 4 in Section 1.7.

      Though there are ultimately several different forms of SAS libraries, the most basic simply assigns a library reference (or libref) to a folder which the SAS session can access. A library can be assigned in a program via the LIBNAME statement or through other tools available in the SAS windowing environment or SAS University Edition. In order to use this book, it is essential to assign library references to the data sets downloaded from the author web pages. Figures 1.4.6 and 1.4.7 show an assignment of a library named BookData to an assumed location. The path must be set to the actual location of the downloaded files, and the choice of library name must follow the naming conventions given previously in Program 1.4.3, with the additional restriction that the library reference is limited to 8 characters.

      Figure 1.4.6: Assigning a Library in the SAS Windowing Environment

Figure 1.4.6: Assigning a Library in the SAS Windowing Environment

      Figure 1.4.7: Assigning a Library in SAS University Edition

Figure 1.4.7: Assigning a Library in SAS University Edition

      Submitting the following LIBNAME statement is equivalent to the assignments shown in the Figures 1.4.6 and 1.4.7, except for the fact that the assigned library is not re-created at start-up of the next session.

      libname bookdata ‘C:\Book Data’;

      Any of these assignments creates what is known as a permanent library, meaning that data sets and other files stored there remain in place until an explicit modification is made to them. Temporary libraries are expunged when the SAS session ends—in the SAS Windowing environment, Work is a temporary library; in SAS University Edition, Work and Webwork are temporary.

      The PRINT and CONTENTS procedures provide information about data and metadata as program output. Program and Output 1.4.4 provide a demonstration of their use.

      Program 1.4.4: Using the CONTENTS and PRINT Procedures to Display Metadata and Data

      proc contents data=sashelp.cars;

      run;

      proc print data=sashelp.cars(obs=10) label;

      var make model msrp mpg_city mpg_highway;

      run;

       The CONTENTS procedure output shows a variety of metadata, including the number of variables, number of observations, and the full set of variables and their attributes. Adding the option VARNUM to the PROC CONTENTS statement reorders


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