SAS is a comprehensive system which integrates utilities for storing, modifying, analyzing, and graphing data. Learning to write SAS programs is difficult at first, but doing so will enable you to perform the specific tasks which are most appropriate for your data. Unlike other packages which are easier to use, SAS offers the flexibility for you to make necessary modifications to standard analytic techniques or to select from several possible ways to analyze the same data.
In this lesson, you will write a simple SAS program in which data will be entered into the computer and printed. The instructions below are appropriate for SAS Versions 6.11 and 6.12 on a PC with the Windows 95 operating system. For other computers, practically all of the SAS statements will remain the same, but there will be some differences in the way those statements are furnished to the computer.
The following example will show you how to enter data into SAS and obtain a printout. The data were obtained from the Citrus Production Forecast Page provided by the Florida Agricultural Statistics Service. The data represent December 1999 estimates of yields of early-season oranges (e.g. navel) and late-season oranges (e.g. Valencia) in four U. S. states. Crop estimates are in units of millions of boxes. The SAS dataset will have three variables (state, early-season yield estimates, and late-season yield estimates) and four observations, where each observation corresponds to one state.
Click once on the Program Window editor to make it the active window. You may view or work within any of the three windows, but you must first activate the window you choose. The active window will have a differently-colored title bar. Starting on the first line of the Program Editor window, type in the following:
DATA oranges;The word DATA instructs SAS that you are going to provide data in following steps. SAS has two main categories of operations. Data steps are used to input and modify datasets. Procedure steps, or PROCs, perform operations on the data. The word oranges gives a name to the dataset that will be created. Any name can be assigned to a dataset, as long as it follows these rules:
In the next line, add the following:
INPUT state $ 1-10 early 12-14 late 16-18;Three variables are named in this statement: STATE, EARLY, and LATE. The rules for naming variables are the same as the rules for naming datasets. The numbers refer to columns in the PROGRAM EDITOR window; 1 is the leftmost column. Each of these indicates the range within which the data for the preceding variable will be entered. For example, the data for STATE will be listed beginning in the leftmost column and using up to 10 characters. The $ specifies that STATE should be regarded as a character variable and not a numeric variable. By default, SAS assumes that all variables are numeric.
The next line is:
DATALINES;This indicates that the actual data will appear next. In most SAS references, the statement
CARDS;is used instead. This originally referred to the punch cards that were used to input data in the early days of computer programming. The two commands are equivalent, and SAS will currently accept both commands.
Next, enter the data. Put a single semicolon on a line by itself after the last line of data.
Florida 124 90 California 40 27 Texas 1.3 .3 Arizona .4 .5 ;Now, add the instructions for SAS to print the data. One of the procedures in SAS, PROC PRINT, can be used to provide a simple printout of the data.
PROC PRINT DATA=oranges;Finally, end with a RUN statement. This tells SAS to process the data and provide the information you have requested.
RUN;The SAS program you have written in the Program Editor window should look like this:
DATA oranges; INPUT state $ 1-10 early 12-14 late 16-18; DATALINES; Florida 124 90 California 40 27 Texas 1.3 .3 Arizona .4 .5 ; PROC PRINT DATA=oranges; RUN;At this point, check your program for errors. The PROGRAM EDITOR has some rudimentary text editing features, such as cut, copy, paste, undo, and find and replace. Use these to edit the program, if necessary. Now, you should save the program you have written. On the top menu bar, choose File, then Save As... Next, choose the appropriate directory and assign a name to the program file. By default, SAS will save the file with the extension .sas . For example, you can save the program as oranges.sas . The program will be saved as an ASCII text file. If you use another word processor, such as WordPerfect of Microsoft Word, to write SAS programs, you must save the file as an ASCII text file in order for SAS to read it correctly.
So far, you have only written instructions for SAS to follow, but SAS has not processed that information yet. To run the program, click on the button with a running stick figure at the top of the screen, or click on Locals, then Submit. Statements similar to the following should appear in the LOG window.
NOTE: Copyright (c) 1989-2000 by SAS Institute Inc., Cary, NC, USA. NOTE: SAS (r) Proprietary Software Release 6.12 TS020 Licensed to UNIVERSITY OF FLORIDA, Site 0009337001. 1 data oranges; 2 input state $ 1-10 early 12-14 late 16-18; 3 datalines; NOTE: The data set WORK.ORANGES has 4 observations and 3 variables. NOTE: The DATA statement used 0.81 seconds. 8 ; 9 proc print data=oranges; 10 run; NOTE: The PROCEDURE PRINT used 0.05 seconds.The OUTPUT window should appear as follows.
OBS STATE EARLY LATE 1 Florida 124.00 90.00 2 California 40.00 27.00 3 Texas 1.30 0.30 4 Arizona 0.40 0.50
If your program did not work correctly, use the statements in the LOG window to see if you can trace your mistakes. To bring back the program you just submitted, click on the PROGRAM EDITOR window to activate it, then click on Locals, then Recall text. You can then make any necessary changes, save the edited program, submit it again, and see if it works correctly.
To print the contents of a window, first click on that window to make it active, then choose File, then Print..., then OK. When you are ready to end the SAS session, make sure that you have saved your program. Then, click on File, then Exit... You may be asked to confirm exiting SAS.
In this lesson, you learned the mechanics of writing and running a simple SAS program. Future lessons will show you how to manipulate and analyze data.
K.M. Portier and the University of Florida, 2004