bas 150 lesson 3 lecture

31
BAS 150 Lesson 3: Writing your first SAS program and importing data into SAS

Upload: wake-tech-bas

Post on 12-Apr-2017

105 views

Category:

Education


0 download

TRANSCRIPT

Page 1: BAS 150 Lesson 3 Lecture

BAS 150Lesson 3: Writing your first SAS program and importing data into SAS

Page 2: BAS 150 Lesson 3 Lecture

• Utilize SAS Studio knowledge to write a SAS program

• Print data from your SAS program

• Import various data types in SAS

This Week’s Learning Objectives

Page 3: BAS 150 Lesson 3 Lecture

Don't worry yet about understanding

the "code" for this program.

The lines between the DATA statement

and the first RUN statement tell SAS to

read in the grades.

The lines between the PROC PRINT

statement and the second RUN

statement tell SAS to print out the

Student ID and and final grade for

each student.

A Sample SAS Program

Page 4: BAS 150 Lesson 3 Lecture

SAS Fundamentals (1 of 2) As is true for any other programming language, a SAS program is a series

of instructions written in the SAS language that are executed in order.

Just as you read words in a book, SAS reads and executes

programs from top to bottom and from left to right.

In life, you must adhere to certain language rules so others may

understand you. With SAS, you must adhere to certain SAS rules, known

as "syntax“, in order for SAS to be able to read and run your programs

properly.

Page 5: BAS 150 Lesson 3 Lecture

SAS Fundamentals (2 of 2)SAS code can be broken down into 3 major parts:

o SAS Statements

o SAS Names

o Proc and Data steps

Page 6: BAS 150 Lesson 3 Lecture

SAS Statements (1 of 2) All SAS statements must end with a semicolon (;)

SAS statements typically begin with a SAS keyword o Examples: OPTIONS, TITLE, DATA, INPUT, DATALINES, RUN, PROC, and VAR

SAS programs can be freely formatted:o Any number of SAS statements can appear on a single line provided they

are separated by a semicolon.

o A SAS statement can be continued from one line to the next as long as no

word is split.

o SAS statements can begin in any column.

Page 7: BAS 150 Lesson 3 Lecture

SAS Statements (2 of 2) SAS statements are not case sensitive

Words in SAS statements are separated by blanks or special

characterso =, +, or *

Comments should be used to annotate your programo Two comment methods are:

Forward slash-asterisk (/*) and ends with an asterisk-forward slash (*/) Comment begins with an asterisk (*) and ends with a semicolon (;)

All text within the delimiters are ignored by SAS.

Page 8: BAS 150 Lesson 3 Lecture

SAS Names (1 of 2) SAS names are used for data set names and variable names

All names must contain between 1 and 32 characters.

The first character in a name must be a letter or an underscore (_)

o No other characters, such as $, %, or & are permitted

Blanks cannot appear in SAS names.

SAS names are not case sensitive without quotation marks

SAS is only case sensitive within quotation marks

Page 9: BAS 150 Lesson 3 Lecture

Examples of Invalid Variable Names

Why is it invalid?

Fix suggestion

Price per pound Spaces in between Price_per_pound

Month-total Dash in between Month_total

Num% Contains invalid character (%)

Num

Examples of Valid Variable Names

Parts

LastName

FirstName

DATA

Time

_2Dat2

X2134

SAS Names (2 of 2)

Page 10: BAS 150 Lesson 3 Lecture

SAS DATA and PROC steps (1 of 4)The DATA and PROC steps are the building blocks of any SAS program

DATA step - Begins with a DATA statement and ends with:

o a RUN statement

o another DATA statement

o or a PROC statement

DATA steps are used to manage data

Page 11: BAS 150 Lesson 3 Lecture

SAS DATA and PROC steps (2 of 4)The DATA and PROC steps are the building blocks of any SAS program

DATA step - Begins with a DATA statement and ends with:

o a RUN statement

o another DATA statement

o or a PROC statement

Any portion of a SAS program that begins with a PROC statement and ends with

a RUN statement, a DATA statement, or another PROC statement is called

a PROC step.

Page 12: BAS 150 Lesson 3 Lecture

SAS DATA and PROC steps (3 of 4)The DATA and PROC steps are the building blocks of any SAS program

PROC step - Begins with a PROC statement and ends with:

o a RUN statement

o another DATA statement

o or a PROC statement

PROC steps are pre-written routines that allow us to analyze the data

contained in a SAS data set

Page 13: BAS 150 Lesson 3 Lecture

SAS DATA and PROC steps (4 of 4)

It is good programming practice to close all DATA and PROC

statements with a RUN statement.

DATA and PROC statements must be written as distinct operations in

your SAS code

You cannot combine a PROC step within a DATA step and vice versa

Page 14: BAS 150 Lesson 3 Lecture

Reminders:o All SAS statements end with a semicolon (;)

o Titles are always enclosed in quotes (“”)

o Step boundaries

All step boundaries are defined by the RUN; statement

In some cases, QUIT statement can also be used

Beginning of another step (DATA or PROC statement ) is also a step

boundary

SAS Fundamentals

Page 15: BAS 150 Lesson 3 Lecture

Parts of a SAS Program (1 of 5)

PROC Step

DATA Step

Global Statements

Annotation

Page 16: BAS 150 Lesson 3 Lecture

Parts of a SAS Program (2 of 5)Code Window –

Write Code

Log Window – Messages about

your SAS session

Results WindowSAS program

output

Output Data Window –

Data set data

Page 17: BAS 150 Lesson 3 Lecture

Parts of a SAS Program (3 of 5)

Log Window – Messages about

your SAS session

Page 18: BAS 150 Lesson 3 Lecture

Parts of a SAS Program (4 of 5)

Results Window – SAS program output

Page 19: BAS 150 Lesson 3 Lecture

Parts of a SAS Program (5 of 5)

Results Window – SAS program output

Page 20: BAS 150 Lesson 3 Lecture

Overview of Tasks:1. Log on to SAS Studio

2. Use “Student Grades” example from this lecture to use as a guide for coding

3. Use annotation to describe what your SAS program does

4. Title your SAS program “BAS 150 My First SAS Program”

5. Name your Data step Soccer Scores

6. Use data on next page to type into your SAS program

7. Print out the player, age and goals

Writing Your First SAS Program (1 of 4)

Page 21: BAS 150 Lesson 3 Lecture

Writing Your First SAS Program (2 of 4)Player Goals Age Years Playing

Rachel 10 8 4Bobby 8 8 2Jodie 15 10 5Lori 5 7 2

Danny 6 6 1Pat 7 5 1

John 15 9 5Betty 12 10 3

Page 22: BAS 150 Lesson 3 Lecture

Writing Your First SAS Program (3 of 4)

If done correctly, this is

what your finished code

looks like…

Page 23: BAS 150 Lesson 3 Lecture

Writing Your First SAS Program (4 of 4)

If done correctly, this is

what your finished

results look like…

Page 24: BAS 150 Lesson 3 Lecture

Importing Other Types of Data Files into SAS

Page 25: BAS 150 Lesson 3 Lecture

Data File Types

For this course, we will mainly use the following ways to import

data into SAS…

Datalines (entering data in data step) - Your first program

CSV files (*.csv)

Excel files (*.xlsx), (*.xls)

SAS data sets (*.sas7bdat) – More on this later in the course

Page 26: BAS 150 Lesson 3 Lecture

Importing Data Into SAS (1 of 4)Importing a CSV file…

Page 27: BAS 150 Lesson 3 Lecture

Importing Data Into SAS (2 of 4)Importing an Excel *.xlsx file…

Page 28: BAS 150 Lesson 3 Lecture

Importing Data Into SAS (3 of 4)Importing an Excel *.xls file…

Page 29: BAS 150 Lesson 3 Lecture

Importing Data Into SAS (4 of 4)The results for the Retail

Store data are identical for:

CSV file (*.csv)

Excel file (*.xlsx)

Excel file (*.xls)

Page 30: BAS 150 Lesson 3 Lecture

• Utilize SAS Studio knowledge to write a SAS program

• Print data from your SAS program

• Import various data types in SAS

Summary - Learning Objectives

Page 31: BAS 150 Lesson 3 Lecture

“This workforce solution was funded by a grant awarded by the U.S. Department of Labor’s

Employment and Training Administration. The solution was created by the grantee and does not

necessarily reflect the official position of the U.S. Department of Labor. The Department of Labor

makes no guarantees, warranties, or assurances of any kind, express or implied, with respect to such

information, including any information on linked sites and including, but not limited to, accuracy of the

information or its completeness, timeliness, usefulness, adequacy, continued availability, or

ownership.”

Except where otherwise stated, this work by Wake Technical Community College Building Capacity in

Business Analytics, a Department of Labor, TAACCCT funded project, is licensed under the Creative

Commons Attribution 4.0 International License. To view a copy of this license, visit

http://creativecommons.org/licenses/by/4.0/

Copyright Information