Basic Introduction to SAS Programming

Running SAS Tasks through Point-and-Click Windows

You can use various point-and-click windows to perform tasks in SAS. This method is relatively simple to use, and favored by many people. If you were using the point-and-click options you could open and use SAS products that work like this, such as SAS Enterprise Guide or JMP. SAS Studio also has a version of this sort of approach built in, called the ”Visual Programmer.”
Point-and-click has serious disadvantages, however, because there are often a great number of check boxs and options, and SAS does not remember your settings. Therefore, every time you re-start a certain section of SAS you have to re-enter many check box options. For this reason, we will not use the point-and-click options very much in this book, as they are very slow and inefficient.

Doing SAS Tasks through Programming Code (Syntax)

Advantages of Programming Code

Instead of point-and click, SAS usually uses programming code in the SAS 9 Editor window or the SAS Studio Code window to input keywords that tell SAS what you want. Note the following about programming code in general:
  1. Programming is efficient: The programming code input method is very efficient and advantageous. It is far quicker than using point-and-click. You can save programming code for later use more easily than you can in many point-and-click programs. Finally, point-and-click takes a lot of time to go through if you are in a classroom teaching situation, whereas opening and running a programming code file is quick.
  2. Saving and re-using programming code: You can save your programming code files and re-use them time and time again (see for instance the programming code files in the “Textbook Materials” folder). Generally, once you have the programming files you like to use, the only thing you have to do is change the names of the datasets and variables.
  3. This book mostly uses programming code: Because of the advantages of programming code, I will mostly use and teach this input method in this book. You will not have to learn what programming to use; the textbook comes with pre-written programming code files (see the “Textbook Materials” folder at http://support.sas.com/publishing/authors/lee.html). Each time we run an analysis, you will be directed to open and run a pre-existing file as described below.

First Lessons on SAS Programming

Programming can be a daunting task for many people. However, it is actually a very easy language simply composed of a few keywords, as well as a basic structure to which you need to stick.
For instance, take a look at Figure 6.2 Example of programming code in a SAS Editor or Code window, which shows an example of SAS code in either the Editor window of SAS 9 or the Code window of SAS Studio. Here, you can see various keywords and variable names that tell SAS what dataset to analyze, which variables to analyze, and what statistical analysis to do on these variables.
Figure 6.2 Example of programming code in a SAS Editor or Code window
Figure 6.2 Example of programming code in a SAS Editor or Code window is a specific type of code that runs a statistical analysis. We can see the following in this figure:
  1. To run a SAS statistical procedure, you usually start with the keyword PROC followed by a specific keyword that identifies which particular statistical analysis you want. For instance, in Figure 6.2 Example of programming code in a SAS Editor or Code window the keyword MEANS asks SAS to do basic descriptive statistics on variables, as described in later chapters.
  2. When running procedures, we next usually identify the dataset to be analyzed by its library and then its dataset name, i.e. the general structure is “<Name of the library>.<Name of the dataset>.” In Figure 6.2 Example of programming code in a SAS Editor or Code window, the dataset to be analyzed is the “Profits” dataset within the “MBA” library, as identified by the “Data=MBA.Profits” part of the code.
  3. There are often extra keywords to identify further statistical options.
  4. Usually, the middle section of SAS procedure code contains a description of the variables to be analyzed. In the simple example in Figure 6.2 Example of programming code in a SAS Editor or Code window, we simply list the variables to be analyzed after the keyword VAR. In more complex procedures that are mostly beyond the scope of this book, we sometimes also have to tell SAS how the variables are related.
There are also certain general SAS programming rules that can be seen in the example in Figure 6.2 Example of programming code in a SAS Editor or Code window:
  1. Capitalization of words in SAS code:
    1. SAS programs usually do not care about capitalization of words. For instance, in Figure 6.2 Example of programming code in a SAS Editor or Code window, keywords such as “Proc Means” could easily be spelled “PROC means” or any combination or lower and uppercase, as can the dataset names.
    2. Almost the only time that SAS cares about capitalization is if you are referring to specific text data within a dataset. For instance if “Gregory Lee” is a field in a dataset, then if you need to refer to this data in code, you must get the exact capitalization correct.
  2. Spacing, lines and tabs in SAS code:
    1. It does matter that you keep at least one space between different keywords of SAS programming (e.g. you can’t put “PROCMEANS” above).
    2. However, other than that, SAS does not mind where in the code or editor window you place code so long as the basic statements are in the right order. You can place different statements on different lines, run them together without line breaks, or use multiple spaces or tabs between pieces of code, etc.
  3. Semicolons as the key for endings of sections: Sections of SAS programs end with a semicolon (“;”). If you try to run a SAS program and find that it does not work, it is often because you have failed to add the semicolon at the end of a section.
  4. The Run command as the key for the end of a program: SAS programs usually end with a “Run;” command.
  5. Running a SAS program: To actually make the program run, you click the little running person icon in the SAS 9 or SAS Studio toolbar, as seen in Figure 6.3 Running a SAS Program below.
Figure 6.3 Running a SAS Program
One cardinal rule is to always check the SAS log after running code to see if the program has worked and to determine if there are errors (e.g. misspelling the dataset name). In such cases, SAS will warn you in the log with red error sections. This is particularly easy in SAS Studio, which lists any errors at the top of the log section.
Finally, note that “PROC”-type code to invoke SAS statistical analyses are not the only form of programming. Notably, the very important DATA keyword is used to create and manipulate datasets, as described below in Major Task #1: Data Manipulation in SAS.

Opening Existing SAS Code Files

As I have discussed above, this book does not expect the reader to become a SAS programmer immediately. All the analyses taught in the book are given to you as pre-written programming code files that you simply have to open and run to get the results. As you work with these files, you will quickly see how the underlying programs work, and soon be able to apply them to your own datasets and variables with little change.
Even if you were to write your own programs from scratch, you would usually save the code files and then re-open and run them later when you wish to recreate the analysis.
To open existing programming code files like those in the “Textbook Materials” folder, do the following:
  • In SAS 9, go to File > Open Program and navigate to where the file is stored on your hard drive.
  • In SAS Studio, go to the Server Files and Folders section, and open the code file by double clicking on it (for instance, see the many code files in the “Textbook Materials SAS Studio” folder).
As mentioned in the chapter introduction, there are three big tasks in SAS, namely, data manipulation, data analysis, and report generation. The following sections discuss these steps further.
Last updated: April 18, 2017
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.27.178