Lesson 1: Introduction to PC SAS
PC SAS is a Windows GUI for running SAS. When you open the program, you will notice three windows (probably overlapping) called “Editor,” “Log,” and “Output.” An example appears below. The windows have been arranged so you can see them all.
The editor window is used to write programs. Text output will appear in the output window, and the log window will contain error messages and other program execution information. The typical process is to type a program in the editor, then submit it (running man icon) and look at your log and output results. After a program has been submitted, it remains in the editor, so you can make modifications and submit it again.
You can save your programs and open previously saved programs from the file menu. However, the "Save" command will apply to the active (top) window. You can save editor, log, or output files, accordingly. An extension of .sas, .log, or .lst (respectively) will be added automatically unless you supply some other extension. (Using .txt may be helpful if you intend to open the file with Notepad or a word processor, but most of the time it is best to stick with SAS's defaults.) There is one little catch you should remember: In order to open a saved program, an editor window must be active when you select "Open" from the file menu. If you have closed your editor window, you must first open a new one by selecting "Enhanced Editor" from the "View" menu, then proceed to the "File-->Open" dialog..
If you have font problems (incorrect characters) when you open an output file in a word processor, try changing the font to "SAS Monospace." This should work if you are on a computer with SAS installed. If SAS is not installed, you may not be able to get all of the characters correct, but any monospace font, like Courier, will straighten out most of the formatting.
Take a look at the program statements in the editor window. Notice that each line ends with a semicolon . SAS uses semicolons to define the end of a statement. It doesn't matter how the text is arranged, whether there are extra spaces, indentions, extra lines, multiple statements on a line, or statements split across lines. Such formatting can, and should, be used to enhance readability for humans, but to SAS all that matters is where the semicolons are.
Enterprise Guide is a new environment for running SAS. Although the same programs work and produce similar results, there are some differences in the appearance and behavior of the interface. If you purchase the "SAS Learning Edition," this is the interface that will be presented after it is installed. An example appears below. In Enterprise Guide, your work is organized into projects, which appear in a collapsible tree structure on the left. The three windows mentioned above do not open automatically. In order to type in a program, you open a code window which replaces the editor. Output windows open as needed when programs run, but not necessarily automatically. Output, log, and code windows (also datasets) can be opened by double-clicking icons in the project tree. The contents do not accumulate in the log and output windows as they do in PC SAS. When you submit a program, you have the choice of overwriting previous output or starting a new node in the project tree. This keeps your results more organized. Note, the "running man" icon that is used for "submit" in the old version is replaced by a sheet of paper with a down-arrow beside it.
You can use the regular interface described under "PC SAS" with Learning Edition, but you have to find the sas.exe file in the program files directory. Make a shortcut to this file and place it on your desktop to use for starting SAS.
When you need more information about making SAS do what you want, there are several sources you can access. The first is the "Help" facility installed on your computer and found at the right end of the menu bar. Unfortunately, because SAS is a huge and powerful product, the help can be a challenge to navigate. The appearance and content of your help menu may vary depending on the version of SAS you are working with. The examples here use version 9.1.3. Under "Help," select "SAS Help and Documentation." Then you should see a window like this:
Click on the plus sign by SAS Products and the tree opens like this:
There is an important lesson right here in looking at this list in help. SAS is not just one program. It is a system of interconnected modules ("products"). In some ways it is like Microsoft Office, which has a number of components like Word, Excel, PowerPoint, etc. The list you see under "SAS Products" is not necessarily all of them, and you do not necessarily have all those listed available to you. In this course, we will only be dealing with a few of them, and most of the time, we will be in "Base SAS." Click the plus sign by "Base SAS" and you will see:
Again we have a long list of choices. The challenge is to learn where to look for the information you need, because there is simply so much that it can easily cause "information overload." As you begin learning, the most helpful sources of information will be under "SAS Command Reference," "SAS Procedures," or "SAS Language Dictionary." Spend some time looking around in "Help" and familiarizing yourself with what is there. Notice that there are also index and search facilities. Unfortunately, these often return too many "hits" from modules other than those you are interested in. Thus, there is no substitute for learning to navigate the help tree!
SAS also provides SAS OnlineDoc 9.1.3 for the Web which is similar to the help but may be more up-to-date. In fact, you can download it in PDF form and print whatever you want. The Online version has a search facility which allows you to restrict your search to a particular module or procedure. In some cases this can be extremely helpful for avoiding unwanted hits.
A statement in SAS is sort of like a complete sentence (although the analogy won't extend as far as having a subject and verb) or a single command. Every statement must end with a semicolon, which signals the end of a command. Inside a statement you will find such things as keywords , options , and user-supplied names . SAS is pretty flexible in its ability to interpret words within a statement. For example, you can separate words with spaces, tabs, or returns--any white space will be treated the same way. You can split statements across multiple lines or put multiple statements on one line. We will try to develop a style that is easy to read and follow by using indentation and comments. SAS is also forgiving, in that it tries to figure out things that might be mistakes. For example, you can put two statements together without a space, because as long as a semicolon is there, SAS can tell where one ends and the next begins.
SAS is designed for data analysis, so programs are organized into “ steps ” that correspond roughly to steps you go through in analyzing data. However, these steps are not just a matter of programming style. They are blocks of code that SAS treats as a whole. Some information is passed from one step to the next, but you should think of steps as independent units.
There are data steps and proc steps . The main purpose of data steps is to create data sets. Proc steps may perform analysis tasks or other actions. Every data step begins with the keyword "data" and every proc step begins with the keyword "proc." (How logical is that?) A " run " statement can signal the end of a step (we will deal with exceptions later) and triggers execution of statements preceding it. A step always ends when another statement that begins with "data" or "proc" is encountered, signaling a new step. Most of the time there must be a "run" statement at the end of a program or the last step will not be executed. Some procs also require a " quit " statement in order to stop them completely.
Some commands are called " global statements " because they are not really part of a step. Some examples are "options," "title," and "libname" statements, which will be described later. The effects of these commands typically hold across many steps.
We should understand the flow of events after a program is submitted. In most cases, SAS first reads all the commands in a step, checks them for errors, then executes the instructions before going on to the next step. If errors are found, SAS writes error messages and warnings to the log. If there are serious errors, SAS will not execute the statements in the step. If another step follows, SAS will go on and try to run the next step. This may produce unexpected results if a later step depends on output from a previous step that had errors. So, errors do not always cause SAS to stop reading and executing code. This is why it is important to ALWAYS CHECK THE LOG . You may get output that looks fine, but was produced in spite of errors that caused the results to be wrong!
A step could be just one line of code, but it could also be many lines. The program shown below has four steps. Each begins with a keyword, either "data" or "proc," and ends with a run statement (even though it is redundant to place a "run" before another data or proc step, it turns out to be useful in some cases). Don't worry about the meaning of the other statements right now, but study the structure of the steps. Note how the editor separates the steps with horizontal lines and adds color codes to various elements of syntax.
Incidentally, SAS has two editors. The one that opens by default in PC SAS is the "Enhanced Editor." We may explore uses of the other editor later, but for now we'll stick to this one. If you ever need to open another editor window (such as if you have closed one, or you want to have two programs showing at once), choose "Enhanced Editor" rather than "Program Editor" in the "View" menu.
The following may be copied into a word processing document, which may be edited to answer the questions and then submitted. There is no need to run SAS programs for these exercises.
1. How many steps are there in the following program? Mark the beginning and end of each step and label it as a data or proc step.
data one; do x=1 to 1000; y=int(ranuni(0)*6+1); output; end; run; proc freq; tables y; data two; set one; z=7-y; run; proc freq; tables z; run;
2. Suppose the program above was modified so that an error occurred in the second step. What would probably happen when SAS tried to process the remaining steps? (Be sure you have #1 right!)
3. Find three errors in the following program, based on the information in this lesson. (Assume there are no statements split across multiple lines.)
input subject brand rating;
Copyright reserved by Dr. Dwight Galster, 2006. Please request permission for reprints (other than for personal use) from email@example.com . "SAS" is a registered trade name of SAS Institute, Cary North Carolina. All other trade names mentioned are the property of their respective owners. This document is a work in progress and comments are welcome. Please send an email if you find it useful or if your site links to it.