44
Welcome to the Exciting World of ! Lessons to familiarize yourself with

Welcome to the Exciting World of !

Embed Size (px)

DESCRIPTION

Welcome to the Exciting World of !. Lessons to familiarize yourself with. Getting to Know SAS. SAS is a computer program used to analyze data. In this course you will learn how to write programs for your data sets, run these programs, and interpret the output they produce. Resources. - PowerPoint PPT Presentation

Citation preview

Page 1: Welcome to the Exciting World of !

Welcome to the Exciting World of !

Lessons to familiarize yourself with

Page 2: Welcome to the Exciting World of !

Getting to Know SAS

SAS is a computer program used to analyze data. In this course you will learn how to write programs for your data sets, run these programs, and interpret the output they produce.

Page 3: Welcome to the Exciting World of !

Resources

The tutorials that follow will help you learn how to use SAS for the various statistical analyses covered in this course. Other useful resources are:

• SAS Official Tutorial (available in an open SAS window under Help/Getting Started with SAS Software)

• The Little SAS Book: A Primer, 3rd ed., by L. Delwiche & S. Slaughter, (2003) SAS Institute

Page 4: Welcome to the Exciting World of !

Resources, cont.

• Applied Statistics and SAS Programming Language, 4th ed., by R. Cody & J. Smith, (1997) Prentice-Hall Inc.

• UCLA Academic Technology Services

• South Dakota State SAS Tutorial

• Numerous online resources are also available through search engines, like Google

Page 5: Welcome to the Exciting World of !

Let’s Get Started!

Open your SAS program. If you do not have a shortcut to SAS on your desktop, you will most likely find SAS under Programs from your Start Menu.

You may be asked if you would like to run the SAS tutorial. If you opt not to see this notice every time you open SAS, the tutorial can be reached at any time by clicking on Help/Getting Started with SAS Software.

Page 6: Welcome to the Exciting World of !

SAS Tutorial

Page 7: Welcome to the Exciting World of !

SAS Windows

When you open SAS, you will see 5 main windows:

• Editor• Log• Output• Explorer• ResultsYou can easily navigate between windows by

clicking on the tabs along the bottom of the SAS window. Clicking on a tab will make that window active. Try it!

Page 8: Welcome to the Exciting World of !

Editor Window

The Editor Window is where you write the SAS programs. You can type your data sets directly into the Editor window, or you can import data from an outside source, such as Excel. You will see examples of SAS programs shortly.

Page 9: Welcome to the Exciting World of !

SAS Windows: Editor

Page 10: Welcome to the Exciting World of !

Log Window

The log window displays messages such as notes, errors, and warnings about your programs. It is important to always check the log after you have submitted a program to SAS. NOTEs are simply comments about the programs. WARNINGs are sometimes of concern when running your programs. ERRORs will stop your program from running, and they provide important information about what needs to be fixed before you can continue with the analysis.

Page 11: Welcome to the Exciting World of !

SAS Windows: Log

Page 12: Welcome to the Exciting World of !

Output Window

The Output window will display the results from your analyses as well as anything you explicitly tell SAS to print, such as a data set. The output window is currently empty, because we have not run any programs.

Page 13: Welcome to the Exciting World of !

SAS Windows: Output

Page 14: Welcome to the Exciting World of !

Explorer Window

The Explorer window gives you access to all of your files and data sets. The current program and data sets you are working with are stored temporarily in a “Work” file, which can be accessed by clicking on the file cabinet icon “Libraries.” We will explore this in more depth later.

Page 15: Welcome to the Exciting World of !

SAS Windows: Editor

Page 16: Welcome to the Exciting World of !

Results Window

The Results window systematically lists each section of your output, making it easy to search through your output. You can also use the results window to save or print selected portions of your output. Because we have not submitted any programs, our results section is currently empty.

Page 17: Welcome to the Exciting World of !

SAS Windows: Results

Page 18: Welcome to the Exciting World of !

SAS Programs

Now that you are familiar with the general appearance of SAS windows, let’s get to the fun part: SAS programs!

SAS programs consist of ordered statements, which tell SAS what you would like it to perform. The programming language may seem awkward at first, but after some practice you will become comfortable using it.

Page 19: Welcome to the Exciting World of !

StatementsSAS programs are made up of statements. Each

statement is like a command to SAS, and each statement ends with a semicolon (;). This tells SAS that one statement has ended and another will begin. The following is an example of a program containing three statements. The first line tells SAS to do a univariate analysis on the data set called “example.” It is only going to run this analysis on the variable “height.”

PROC UNIVARIATE DATA = example; VAR height; RUN;

Page 20: Welcome to the Exciting World of !

Notice on the previous example that certain words were in blue. These are words that SAS considers “key words.” You will become familiar with these key words and the color-coding system in SAS.

NOTE: SAS is NOT case sensitive. Some people type key words in CAPS for easier reading and organization, but it is not necessary. Feel free to find a system that works for you.

Also, SAS does not require statements to be typed on separate lines. It is a common practice to give each statement its own line and to indent each statement after the first line of the program for better organization. Again, this is not necessary, but it generally makes your programs easier to read.

Page 21: Welcome to the Exciting World of !

Comments

Comments are statements that are ignored by SAS. They are very useful when organizing your data, because they can provide titles or headings throughout your code. They are set apart by either an asterisk at the beginning of the comment and a semicolon at the end, or by /* at the beginning and */ at the end:*This is one way to write a comment; /*This is another way to write a comment. This method is valuable when you want to retain an entire section of code but not include it in the SAS program you submit.*/

Page 22: Welcome to the Exciting World of !

Entering Data in SAS

Before you can analyze data in SAS, you must first enter a data set to work with. This is done using a DATA step, which contains statements that do the following:

1. name your new data set2. name each variable 3. tell SAS that you are inputting the data4. input the values of the data. A semi-colon on the next line completes your data

set.

Page 23: Welcome to the Exciting World of !

Data Sets

A SAS data set consists of rows and columns. Each row represents an individual unit (such as a person, plant, country, etc.) on which data was collected. Each column represents a variable which contains information about that experimental unit (such as gender, age, height, education, income, race, blood pressure, etc.)

Page 24: Welcome to the Exciting World of !

Variable Names

When inputting the variable names, put a $ after labels for categorical variables. This tells SAS that the variable is not quantitative. Variable names must start with a letter or underscore (_), and they can also contain numbers.

Let’s try creating a simple data set!

Page 25: Welcome to the Exciting World of !

The following program creates a data set called “example1” with five variables: clinic, diastolic blood pressure at 6 months, systolic blood pressure at 6 months, diastolic blood pressure at baseline, and systolic blood pressure at baseline. The program also creates a sixth variable called increase, depending on whether diastolic pressure was higher after six months than at baseline. The output is given a title, and then variables clinic, sbpbl, sbp6, and increase are printed in the output.

Page 26: Welcome to the Exciting World of !

Type this program into SAS:DATA example1; INPUT clinic $ dbp6 sbp6 dbpbl sbpbl; if dbp6 > dbpbl then increase = 'Y'; else increase = 'N'; DATALINES; C 84 138 83 143 D 89 150 91 140 A 78 116 100 162 A 77 155 76 144 C 81 145 86 140 ; RUN; TITLE 'Pretend study data at baseline and 6 months (Blood Pressure)'; PROC PRINT DATA = example1; VAR clinic sbpbl sbp6 increase; RUN;

Page 27: Welcome to the Exciting World of !

Running a ProgramYou can run this program in multiple ways. The

simplest is by clicking on the icon of the running man at the top of your toolbar (you will see the word “Submit” if you put your cursor over this icon).

You can also right-click anywhere on your Editor window and select “Submit All.”

If you would like to submit only a portion of your code, highlight the portion you would like to run, then either click on the running man icon or right-click in the editor window and select “Submit selection.”

NOTE: You must submit your data set before you can analyze it. If you try to analyze a data set that has not been submitted, SAS will not be able to run the program.

Page 28: Welcome to the Exciting World of !

Always Check Your Log!

The first step after running a program is to check your log to make sure there are no warnings or error messages. If your program did not run, the log will give clues about why it had to stop.

Take a look at your log.

Page 29: Welcome to the Exciting World of !
Page 30: Welcome to the Exciting World of !

The previous log shows no errors, and that means we are ready to look at our output. (Notice, now that you have printed your data set, you have something in your Results section with the title you gave it.)

If your program did not run, or if there are Error messages in your log, compare your code to the sample code. You may have forgotten something as simple as a semi-colon at the end of a statement. Make appropriate changes and run it again.

Page 31: Welcome to the Exciting World of !

Output

After you created the data set, you also told SAS to perform the procedure (PROC) of printing certain variables from that data set. You should now see those variables in your output window.

Take a look. Do you see them? Do you see the new variable, increase, you created? Notice the title you created at the top of the output.

Page 32: Welcome to the Exciting World of !
Page 33: Welcome to the Exciting World of !

Try Another One!

Now that you’ve seen the basic format of a data set, try entering the following data set into SAS and printing the variables in your output. In addition, create a variable called resid for residency, where resid = ‘Y’ if the student is from MN, and ‘N’ if the student is not from MN. Try inputting the data set in SAS, run the program, and compare your results to the answer. Also, give your output a title.

Page 34: Welcome to the Exciting World of !

Use these data for your data set (don’t forget to put a “$” after any categorical variables):

gender age marstat credits state

F 23 S 15 MN

F 21 S 15 WI

F 22 S 9 MN

F 35 M 2 MN

F 22 M 13 MN

F 25 S 13 WI

M 20 S 13 MN

M 26 M 15 WI

M 27 S 5 MN

M 23 S 14 IA

M 21 S 14 MN

M 29 M 15 MN

Page 35: Welcome to the Exciting World of !

ChecklistDid your program run successfully?

Did you:

• Check your log

• Remember a semi-colon after every statement

• Put a semi-colon on the following line after your last row of data

See the next slide for sample code.

Page 36: Welcome to the Exciting World of !

Sample Code for Data Set*This is an example of the code you might use to create the data set. Your data set may have a different name, labels for the variables, and/or title, but the values for the variables in the output should be the same; DATA practice; INPUT gender $ age marstat $ credits state $; if state = 'MN' then resid = 'Y'; else resid = 'N'; DATALINES; F 23 S 15 MN F 21 S 15 WI F 22 S 9 MN F 35 M 2 MN F 22 M 13 MN F 25 S 13 WI M 20 S 13 MN M 26 M 15 WI M 27 S 5 MN M 23 S 14 IA M 21 S 14 MN M 29 M 15 MN ; RUN; TITLE 'Practice Creating a Data Set'; PROC PRINT DATA = practice; VAR gender age marstat credits state resid; RUN;

Page 37: Welcome to the Exciting World of !

Output of Variables

Page 38: Welcome to the Exciting World of !

Now that you’ve seen the sample code, make any necessary adjustments to your code and re-run your program.

The next section will show you how to save your code and output.

Page 39: Welcome to the Exciting World of !

Saving SAS Programs

To save a SAS program, while in the Editor window, click on File, Save As…Choose a location to save your file and a name for it. It will automatically be saved as a .sas file.

After closing SAS, if you open that file, SAS will automatically start and your code will be in the Editor window.

Page 40: Welcome to the Exciting World of !

Saving Code as a Text File

Some of your homework assignments will require you to submit your SAS code. To do this, you will need to copy and paste your code into some type of text editor.

Sometimes it is possible to simply copy and paste your code into Word; however this does not always work and it is not recommended. As an alternative, copy and paste your code into a simple text editor, like Notepad. From Notepad, recopy the text, then paste it into Word. You can then save your code as a Word document.

Page 41: Welcome to the Exciting World of !

Example of Using Notepad and Word

Page 42: Welcome to the Exciting World of !

Saving Output

For your homework assignments you will also be asked to incorporate your output into write-ups on each problem. It is important to know how to save your output so that it can be used in a Word document.

While in the output window, click on File, Save As…A window will pop-up. Change the file type from List to RTF. Create a name for your file and decide where to save it. Your output is now saved as a .rtf file, and it can be used in Word.

Page 43: Welcome to the Exciting World of !

Save Output as a .RTF File

Page 44: Welcome to the Exciting World of !

Now that you have seen the basic SAS set-up, you will learn how to use SAS for data analysis! We start with descriptive statistics, then move on to hypothesis testing.