Upload
wake-tech-bas
View
109
Download
0
Embed Size (px)
Citation preview
BAS 150Lesson 3: Writing your first SAS program and importing data into SAS
• 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
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
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.
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
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.
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.
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
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)
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
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.
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
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
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
Parts of a SAS Program (1 of 5)
PROC Step
DATA Step
Global Statements
Annotation
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
Parts of a SAS Program (3 of 5)
Log Window – Messages about
your SAS session
Parts of a SAS Program (4 of 5)
Results Window – SAS program output
Parts of a SAS Program (5 of 5)
Results Window – SAS program output
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)
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
Writing Your First SAS Program (3 of 4)
If done correctly, this is
what your finished code
looks like…
Writing Your First SAS Program (4 of 4)
If done correctly, this is
what your finished
results look like…
Importing Other Types of Data Files into SAS
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
Importing Data Into SAS (1 of 4)Importing a CSV file…
Importing Data Into SAS (2 of 4)Importing an Excel *.xlsx file…
Importing Data Into SAS (3 of 4)Importing an Excel *.xls file…
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)
• Utilize SAS Studio knowledge to write a SAS program
• Print data from your SAS program
• Import various data types in SAS
Summary - Learning Objectives
“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