Fundamentals of Programming in SAS. James Blum

Чтение книги онлайн.

Читать онлайн книгу Fundamentals of Programming in SAS - James Blum страница 6

Автор:
Жанр:
Серия:
Издательство:
Fundamentals of Programming in SAS - James Blum

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

In SAS University Edition, the output is replaced, and the sub-tab for Output Data is not present because the DATA step did not run. With default settings in place, submissions are cumulative for both log and output in SAS windowing environment; conversely, replacement is the default in SAS University Edition. For more information about managing results in either environment, see Chapter Note 1 in Section 1.7.

      Program 1.4.2 shows the code portion submitted in Figure 1.4.1 with the first RUN statement removed. Delete the RUN statement and re-submit the selection, review the output (Figure 1.4.2) and details below for another example of why explicitly ending steps in SAS is a good programming practice.

      Program 1.4.2: Multiple Steps Without Explicit Step Boundaries

      title ‘Combined MPG Means’;

      proc sgplot data=work.cars;

      hbar typeB / response=mpg_combo stat=mean limits=upper;

      where typeB ne ‘Hybrid’;

      

      title ‘MPG Five-Number Summary’;

      title2 ‘Across Types’;

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

      class typeB;

      var mpg:;

      run;

       The first statement compiled and executed is this TITLE statement, which assigns the quoted/literal value as the primary title line.

       The SGPLOT procedure is invoked for compilation and execution by this statement. Subsequent statements are compiled as part of the SGPLOT step until a step boundary is reached.

       This is the position of the RUN statement in Program 1.4.1 and, when it is compiled in that program, it signals the end of the SGPLOT step. Assuming no errors, PROC SGPLOT executes at that point; however, with no RUN statement present in this code, compilation of the SGPLOT step is not complete and execution does not begin.

       These two TITLE statements, which are global, now compile and take effect. Since the SGPLOT procedure still has not completed compilation, nor started execution, this TITLE statement replaces the first title line assigned in .

       This statement starts the MEANS procedure which, due to the fact that steps cannot be nested, indicates that the SGPLOT statements are complete. Compilation of the SGPLOT step ends and it is executed, with the titles in  now placed erroneously on the graph.

      Figure 1.4.2: Failing to Define the End of a Step

Figure 1.4.2: Failing to Define the End of a Step

      In any interactive session, the final step boundary must be explicitly stated. For a discussion of the differences between interactive and non-interactive sessions in the SAS windowing environment and SAS University Edition, see Chapter Note 2 in Section 1.7.

      The remainder of Program 1.4.1 is a DATA step, which is shown as Program 1.4.3 with a few details about its operation highlighted. The DATA step is a powerful tool for data manipulation, offering a variety of functions, statements, and other programming elements. The DATA step is of such importance that it is featured in every chapter of this book.

      Program 1.4.3: DATA Step from Program 1.4.1

      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;

       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

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