Tutorial - UCSC Directory of individual web sites

advertisement
Economics 211A
SAS for Windows Tutorial
Getting Started:
1. Copy the sas dataset, TESTDATA, located on my web page to your hard drive or a flash drive.
Go to http://people.ucsc.edu/~rfairlie/courses/econ211a/
2. Load SAS.
3. You first need to get acquainted with the three main windows within SAS. When you first
load SAS you will see the Log and Editor windows. You can jump between them using your
mouse. The third window is the Output window. The general descriptions of each window are:
Editor The program editor is where you will edit your SAS program and run it. Each SAS
program will contain lines of code which tell the computer things such as where to find
the data, how to use the data, and what procedures, including regressions, to use on the
data.
Log
The Log window is where you will receive comments on the execution of your program
and where you can check for errors in your program. If your SAS program does not work
you can examine the listing here to figure out what is wrong with it.
Output This window will show up after you submit or run a program. It is where the output from
your SAS program is displayed. For example, the output from a regression, including
coefficient and standard error estimates, will be displayed here.
Tutorials:
In the following tutorial, you will learn some SAS for Windows basics. This tutorial should
provide a starting point for learning SAS. Be careful in entering the SAS code. As you will
soon find out SAS is a very unforgiving programming language.
The following program will read and calculate the means of a very small dataset.
1. In the program editor, type in the following lines of program code.
* This is an example program to get started with SAS;
libname sasdata 'c:\econ211a';
* Print out all observations of the data set;
proc print data=sasdata.testdata;
title 'Print out of Dataset';
run;
It is useful to discuss what each line of the program does. The first line is a comment line
used to describe the program. This line does not produce any output. The comment lines (i.e.
those starting with an asterisk) describe what each group of lines of program code do. This is a
good programming technique so that later you won't forget what you have done. The next line
tells SAS the directory in which to find the data set. Note that the program assumes that the
dataset is stored in a directory called c:\econ211a on your hard drive. You can specify any
directory you want here or you could specify a flash drive.
The next set of lines prints out all observations and variables in the dataset. The dataset
should have 7 observations and two variables.
2. Save the SAS program on your hard drive or flash drive using the File-Save As menu. Name
it test1.sas.
3. From the program editor, submit or run the SAS program. You can click on the "running
person" icon at the top or right click and select "Submit."
4. Jump to the log window and examine the lines of code on the screen. Error messages will be
displayed in red. If there are errors you will need to correct them by returning to the program
editor. Fix the error and return to Step 2. Make sure that you resave the corrected program.
5. Read the output from your SAS program by jumping to the output window. If the program ran
correctly it will automatically take you to this window and you should have mean values for the
two variables in the data set. You can print the listing file by clicking on File-Print or you can
save the output to a file. To do this, use the File-Save As menu. This is especially useful if you
plan to read the output file from this regression into any spreadsheet or wordprocessing program.
There you can use the output to create a polished table. You can also copy and paste the output
into Excel and use Text-Columns to format it.
6. Add the following lines of code to the end of the program to calculate means of the dataset.
* Calculates means for the data set;
proc means data=sasdata.testdata;
title 'Means of All Variables';
run;
7. Save the new SAS program as test2.sas. Submit the program and inspect the output.
8. For practice, re-open the original SAS program that is saved in your directory on cfile
(test1.sas). To do this use the File-Open-Read File menu in SAS for Windows. Now, you should
have separate windows for the sas programs, test1.sas and test2.sas.
2
Download