Fundamentals of Programming in SAS. James Blum

Fundamentals of Programming in SAS - James Blum


Скачать книгу
the Results and Log tabs are cumulative as they are in the SAS windowing environment.

      2. Interactive Mode. The SAS windowing environment runs in interactive mode by default, while SAS University Edition runs in non-interactive mode by default, but can be set to run in interactive mode. There are two major differences between the two modes. First, results and logs from multiple code submissions are cumulative in interactive mode, while each code submission results in replacements for the log and results in non-interactive mode. (See Chapter Note 1 above.) Next, the final statement in any code submission made in non-interactive mode is taken as a step boundary, which is not the case in interactive mode, so any submission in interactive mode must end with a step boundary.

      3. SAS Variable Lists. To aid in simplifying references to sets of variables, SAS provides four types of variable lists:

      a. Numbered Range Lists. Numbered range lists are of the form VarM-VarN, where M and N are positive, whole numbers. This syntax selects all variables with the prefix given and numerical suffixes from M and N; for example, Name3-Name5 is equivalent to the list Name3 Name4 Name5. There is no restriction on the order of M and N, so Value6-Value3 is legal and is equivalent to the list Value6 Value5 Value4 Value3. All variables names corresponding to such a reference must be legal and, unless the variables are being created, all in the list must exist.

      b. Name Range Lists. Named range lists are of the form StartVar-EndVar, with no special restrictions on the variable names beyond their being legal. The set selected is the complete set of columns between the two variables in column order in the data set—PROC CONTENTS with the VARNUM options provides a method for checking column order. Referring to Output 1.5.2B and the Sashelp.Cars data set, the list Make--Origin is equivalent to Make Model Type Origin. For this list, the order of the two variable names is important, the first variable listed must precede the second variable listed in column order; for example, Origin--Make generates an error when used with Sashelp.Cars. It is possible to insert either of the keywords CHARACTER or NUMERIC between the two dashes, limiting the list to the variables of the chosen type.

      c. Name Prefix Lists. As used in example code in this chapter, a name prefix list is of the form var:, referencing all variables, in their column order, that start with the given prefix. For example, MPG: references MPG_City MPG_Highway in Sashelp.cars.

      d. Special SAS Name Lists. SAS also provides special lists for selection of variables without actually naming any variables. These are:

      i. _NUMERIC_ : All numeric variables in the data set, in column order

      ii. _CHARACTER_ : All character variables in the data set, in column order

      iii._ALL_ : All variables in the data set, in column order

      4. Data View in SAS University Edition and SAS Windowing Environments. Section 1.4.3 shows how to open a data set for viewing in each of the environments and the difference in appearance; however, there is another important difference in how these viewing utilities operate. The ViewTable in the SAS windowing environment maintains an active control over the data set in use, while the tab displayed in SAS University Edition does not. To see one problem that this can cause, submit the code from Program 1.4.1 in the SAS windowing environment, open the Cars data set from the Work library, then re-submit Program 1.4.1 (or, at least, the DATA step at the top of that program). An error message appears in the log, as shown in Figure 1.7.1, indicating the data set being open in a ViewTable has locked out any modifications to it.

      Figure 1.7.1: Error Message for Updating a Data Set Open in a View Table

Figure 1.7.1: Error Message for Updating a Data Set Open in a View Table

      With this active control, the resource overhead in having large tables open in a ViewTable can be substantial. In contrast, the data views in SAS University Edition are based on results of a query of 100 records of the data set (and potentially a limited number of variables when many are present). Once the query is made, control of the data set is released and no resources beyond the current display are in use.

      5. LABEL/NOLABEL System Option. By default, most procedures in SAS use variable labels (when present) in their output; however, this is in conjunction with the default system option LABEL. It is possible to suppress the use of most labels with the NOLABEL option in an OPTIONS statement, but some labels are still displayed (for example, labels defined in axis statements on a graph or chart). This not only affects variable labels, but also procedure labels—data sets can also have labels, which are unaffected by the NOLABEL option.

      6. Error Types. The SAS Documentation separates errors into several types. Syntax errors are cases where programming statements do not conform to the rules of the SAS language—for example, misspellings of keywords or function names, missing semi-colons, or unbalanced parentheses. Semantic errors are those where the language element is correct, but the element is not valid for that usage—examples include using a character variable where a numeric variable is required or referencing a library or data set that does not exist. Execution-time errors are errors that occur when proper syntax leads to problems in processing—for example, invalid mathematical operations (such as division by zero) or incorrect sort orders when working with grouped data. Data errors are cases where data values are invalid, such as trying to store character values in numeric variables. Other error types involve SAS language elements that are beyond the scope of this book.

      7. Graphics File Setup. All graphs shown in the book in Chapters 2 through 8 are generated as TIF files with a specific size and resolution. While running code copied directly from the book often produces similar results, as Output 1.5.3A and B show, it is not guaranteed to be the same. To match the specifications for the graphs in the book exactly, the following statements should precede any graph code (mostly generated with the SGPLOT and SGPANEL procedures):

      ods listing image_dpi=300;

      ods graphics / reset imagename=’—give file name here--’ width=4in imagefmt=tif;

      The ODS LISTING statement directs the graphics output to a file, IMAGE_DPI= sets the resolution in dots per inch. The ODS GRAPHICS statement includes options after the slash (/): RESET resets all options to their default, including the sequence of file names. (Image files are not replaced by default, new files are given the same name with counting numbers attached as a suffix.) IMAGENAME= allows for a filename to be specified (a default name is given if none is specified). This can be given as a full-path reference or be built off the working directory. WIDTH= specifies the width with various units available, HEIGHT= can also be specified—when only one of height or width is specified, the image is produced in a 4:3 ratio. IMAGEFMT= allows for a file type to be chosen, most standard image types are available.

      8. Viewing Available Style Templates. Lists of available style templates can be viewed using the TEMPLATE procedure with the LIST statement. The following code lists all style templates available in the default location—a template store named Tmplmst in the Sashelp library.

      proc template;

      list styles;

      run;

      9. Setting Up the CustomSapphire Template. Nearly all output tables in the book are built as RTF tables using a custom template named CustomSapphire. The code to generate this template is provided as one of the files included with the text: CustomSapphire.sas. It is designed to store the CustomSapphire template in the BookData library in a template store named Template (which can be changed in the STORE= option in the DEFINE statement in the provided code). If the BookData library is assigned, submitting the CustomSapphire.sas code creates the template in that library. To use the template, two items are required. First, an ODS statement must be submitted to direct SAS to look for templates at this location, such as:

      ods path (prepend) BookData.Template;

      The ODS PATH includes a list of template stores that SAS searches whenever a request for a template has been made. Since multiple stores may have templates with the same name, the sequence matters, so the PREPEND option ensures the listed stores are at the start of the list (with the possible exception of the default template store in the WORK library). To see


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