Fundamentals of Programming in SAS. James Blum

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

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

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

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

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 ‘Combined MPG Means’;

      proc sgplot data=work.cars;

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

      where typeB ne ‘Hybrid’;

      run;

      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;

       SAS code is written in statements, each of which ends in a semicolon. The statements indicated here (OPTIONS and TITLE) are examples of global statements. Global statements are statements that take effect as soon as SAS compiles those statements. Typically, the effects remain in place during the SAS session until another statement is submitted that alters those effects.

       The SAS DATA step has a variety of uses; however, it is primarily a tool for creation or manipulation of data sets. A DATA step is generally comprised of several statements forming a block of code, ending with the RUN statement, the role of which is described in .

       Procedures in SAS are used for a variety of tasks and, like the DATA step, are generally comprised of several statements. These are generically referred to as PROC steps.

       The PROC MEANS result includes the variables MPG_City, MPG_Highway, and MPG_Combo even though none of these are explicitly written in the procedure code. The colon (:) at the end of a variable name acts as a wildcard indicating that any variable name starting with the prefix given is part of the designated set, this shortcut is known in SAS as a name prefix list. For other types of variable lists, see Chapter Note 3 in Section 1.7.

       With DATA and PROC steps defined as blocks of code, each of these blocks is terminated with a step-boundary. The RUN statement is a commonly used as a step boundary, though it is not required for each DATA or PROC step. See Section 1.4.2 for details.

      SAS processing of code submissions includes two major components: compilation and execution. In some cases, individual statements are compiled and take effect immediately, while at other times, a series of statements is compiled as a set and then executed after the complete set is processed by the compiler. In general, statements that compile and take effect individually and immediately are global statements. Statements that compile and execute as a set are generally referred to as steps, with the SAS language including both DATA steps and procedure (or PROC) steps.

      The DATA step starts with a DATA statement, and a PROC step starts with a PROC statement that includes the name of the procedure, and all steps end with some form of a step boundary. As noted in Program 1.4.1, a commonly used step boundary in the SAS language is the RUN statement, but it is technically not required for each step. Any invocation of any DATA or PROC step is also defined as a step boundary due to the fact that DATA and PROC steps cannot be directly nested together in the SAS language. In general, it is considered a good programming practice to explicitly provide a statement for the step boundary, rather than implicitly through invocation of a DATA or PROC step. The code submissions in Figure 1.4.1 and Program 1.4.2 provide illustrations of the advantages of explicitly defining the end of a step.

      In either the SAS windowing environment or SAS University Edition, portions of code can be compiled and executed by highlighting that section and then submitting. Having clear definitions from beginning to end for any DATA or PROC step aids in the ability to submit portions of code, which can be accomplished by using the RUN statement as an explicit step boundary. Figures 1.4.1A and 1.4.1B show submissions of the two PROC steps from Program 1.4.1 along with their associated TITLE statements.

      Figure 1.4.1A: Submitting Portions of Code in SAS University Edition

Figure 1.4.1A: Submitting Portions of Code in SAS University Edition

      Figure 1.4.1B: Submitting Portions of Code in the SAS Windowing Environment

Figure 1.4.1B: Submitting Portions of Code in the SAS Windowing Environment

      This submission reproduces the bar chart and the table of statistics produced previously in Figure 1.3.7. However, notice that the result is somewhat different in the SAS windowing environments and SAS University Edition. In the SAS windowing environment, the output is added to the output from the previous

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