A Gentle Introduction to Using SAS (Part I) Statistics 101

29 downloads 63538 Views 17KB Size Report
1. A Gentle Introduction to Using SAS (Part I). Statistics 101. Spring 1999. The purpose of this introduction is to familiarize you with carrying out some basic tasks.
A Gentle Introduction to Using SAS (Part I) Statistics 101 Spring 1999

The purpose of this introduction is to familiarize you with carrying out some basic tasks in SAS. It is assumed that you are using one of the NT machines in the Math computer lab in Phillips 324 (see the lab’s web-site1 for hours). Furthermore, some familiarity with using PC software is also assumed. These notes complement the hints and instructions provided with the first computing assignment. PRELIMINARY TASKS: 1. Locate the SAS icon on the Windows NT desktop. 2. Click on this icon to start up a SAS session.

3. You should be able see to the following three windows: i. PROGRAM EDITOR (the SAS programs are typed in this window); ii. LOG (any errors or warnings are displayed in here); iii. OUTPUT (output generated by SAS programs appears here). You should keep in mind that only one of the three windows is active at anytime. To make a window active simple click on that window. If you want to do with the content of a specific window anything (e.g., save or print), that window must be active. When a window is active, its upper border turns blue. DATA SET: Most of the problems involve working with the data saved on a floppy disk. Below is a sample data set: the columns give the names, weights (in pounds), heights (in inches) and gender for twelve fictitious individuals. Joe Tim Moe Jim Al Ty Jill Ruth Anna Kate Mary Ada 1

180 225 300 184 190 142 125 110 119 127 140 100

72 68 70 79 61 67 51 60 59 55 70 53

M M M M M M F F F F F F

http://www.math.unc.edu/Computing/ulab/hours.html

1

OBJECTIVE: Our objective is to write a SAS program that carries out following tasks: 1. Reads in the above data and stores it to a data set called test. 2. Creates a new variable ratio, which equals weight/height. 3. Prints out all five variables (i.e., name, weight, height, ratio and gender). 4. Prints out only the name and weight columns. 5. Sorts the data set by weight in ascending order. 6. Sorts the data set by ratio in descending order. 7. Produces some sample statistics (e.g., sample mean and variance) for the weight and height variables. 8. Produces some sample statistics for the ratio variable grouped by gender.

2

SAMPLE SAS CODE: OPTIONS LS=80 PS=70; DATA test; INFILE 'a:\try.dat'; INPUT name $ weight height gender$; ratio=weight/height; RUN;QUIT; PROC PRINT DATA=test; RUN;QUIT; PROC PRINT DATA=test; VAR name weight; RUN;QUIT; PROC SORT DATA=test; BY weight; RUN;QUIT; PROC PRINT DATA=test; RUN;QUIT;

PROC SORT DATA=test; BY DESCENDING ratio; RUN;QUIT; PROC PRINT DATA=test; RUN;QUIT; PROC MEANS DATA=test; VAR weight height; RUN;QUIT; PROC MEANS DATA=test; VAR ratio; CLASS gender; RUN;QUIT;

3

HOW TO PROCEED: 1. Start up a SAS session by clicking on the SAS icon. 2. Download the file try.dat from the course web-site2 onto your floppy disk (see lab assistant if you do not know how to do this). 3. Type in above SAS code in to the PROGRAM EDITOR window. 4. While PROGRAM EDITOR is active, choose [Save] from the [File] menu to save the program on your floppy disk as try1.sas. 5. While PROGRAM EDITOR is active, choose [Submit] from the [Locals] menu to execute the program. 6. Look in the LOG window for any errors or warnings, which would appear in red and green colored fonts respectively. 7. If there are any warnings, compare the code that you have typed in with the code given above for any discrepancies. 8. Look in the OUTPUT window for the output. 9. Print the output from the OUTPUT window (make sure the window is active). 10. Print the SAS code from the PROGRAM EDITOR window (make sure the window is active). 11. Carefully study the output you have printed out and verify that all the tasks described previously have been carried out fully and correctly (carry out the tasks manually and repeat step 7 if necessary).

2

http://www.stat.unc.edu/students/owzar/stat101.html

4

FINAL NOTES: 1. In writing this SAS code following convention was used: all SAS keywords are typed using uppercase letters, while all user defined variables are typed using lowercase letters. 2. In writing this documentation all user defined variable names were italicized. 3. A semi-colon “;” must be appended to the end of each line. 4. Each procedure should end with a “RUN;QUIT;” sequence. 5. When reading in alphabetical variables, the variable name must be followed by a “$”. 6. Note that the sorting procedure (PROC SORT) by default sorts in ascending order. The keyword DESCENDING was used to override this default. 7. Read the SAS program and make sure that you fully understand what each and every single item in the SAS code is supposed to do. 8. Whenever a data set is read in, it is a good practice to print the data set to the OUTPUT window (using PROC PRINT) to ensure that the data is read in completely and properly. 9. Note that after submitting the program, the SAS code disappears from the PROGRAM EDITOR window. To retrieve the program, choose [Open] from the [File] menu and double click on try1.sas. 10. Make sure to save your program if you modify it. To do so choose [Save] from the [File] menu. 11. Whenever a program is submitted the corresponding output and log information is appended to the existing content of the OUTPUT and LOG windows respectively. Choose [Clear Text] from the [Edit] window to clear the existing content.

5