Upload
wake-tech-bas
View
37
Download
0
Embed Size (px)
Citation preview
BAS 150Lesson 5: Conditional Statements and Iterative Processing
• Utilize conditional statements to analyze data
• Apply iterative processing to SAS programs
• Organize SAS code to run efficient SAS programs
This Lesson’s Learning Objectives
Conditional Statements
IF <condition> THEN <X>;
ELSE <Y>;
If Score >= 70 Then Grade = 'Passing Grade';
Else Grade = 'Failing Grade';
Using If, Then, Else
Student Score GradeJane 75 Passing Grade
Dave 56 Failing Grade
Jack 90 Passing Grade
Sue 68 Failing Grade
Assignment statementUsed to create new variables
IF <condition> THEN <X>;
ELSE IF <condition2> THEN <Y>;
ELSE <Z>;
If Score >= 70 Then Grade = 'Passing Grade';
Else If 60 <= Score <= 69 Then Grade = 'Incomplete';
Else Grade = 'Failing Grade';
Using If, Then, Else, Else If (1 of 2)
Student Score Grade
Jane 75 Passing GradeDave 56 Failing GradeJack 90 Passing GradeSue 68 Incomplete
Use when more than 2 choices exist in a conditional world…
Used to create new variables
When using ELSE IF:
o Processes IF-THEN conditions until first true
statement is met, then it moves on to the next
observation
oOnce a condition is met, the observation is not
reevaluated
Using If, Then, Else, Else If (2 of 2)
If Score >= 90 Then Grade = 'A';
If Score >= 80 Then Grade = 'B';
If Score >= 70 Then Grade = 'C';
If Score >= 60 Then Grade = 'D';
If Score < 60 Then Grade = 'F';
Efficient Coding … If, Then, Else (1 of 2)
Student Score GradeJane 75 D
Dave 56 F
Jack 90 D
Sue 68 D
Q: Why is this happening?
A: Most recent “If Then” Statement overrides prior
one.
If Score >= 90 Then Grade = 'A';
ELSE If Score >= 80 Then Grade = 'B';
ELSE If Score >= 70 Then Grade = 'C';
ELSE If Score >= 60 Then Grade = 'D';
ELSE If Score < 60 Then Grade = 'F';
Efficient Coding…If, Then, Else (2 of 2)
Student Score GradeJane 75 C
Dave 56 F
Jack 90 A
Sue 68 D
More efficient and accurate.
Arithmetic Symbol Example
Addition + Xplus = 4+2;Subtraction – Xminus = 4-2;Multiplication * Xmult = 4*2;Division / Xdiv = 4/2;Exponents ** Xexp = 4**2;Negative numbers – Xneg = -2;
Arithmetic Operators
Logical comparison Mnemonic Symbol
Equal to EQ =
Not equal to NE ^= or ~=
Less than LT <
Less than or equal to LE <=
Greater than GT >
Greater than or equal to GE >=
Equal to one in a list IN
Not equal to any in a list NOT IN
Comparison Operators
Note: <> also used for not equal to, but only in PROC SQL
Boolean Operators
And
Or
Not
Logical Operators
1. Arithmetic operators
2. Comparison operators (<, >, =, LIKE, etc.)
3. Logical operators a. NOTb. ANDc. OR
Use parentheses to control the order of operations
Order of Operations
Logical conditions can be as complicated as
you need them to be
o Just make sure your order of operations is correct
If, Then, Else (cont.)
Iterative Processing
DO Groups (1 of 5)
IF <condition> THEN DO;
<X>; <Y>; <Z>;
END;
If Score >= 90 Then Do;Grade = 'A'; Pass_Fail = 'Pass';
End;
DO Groups (2 of 5)
DO Groups (3 of 5)
DO Groups (4 of 5)
DO Groups (5 of 5)
Sum Statement (1 of 5)
Variable + expression
Running totals or counters
Sum Statement (2 of 5)
Sum Statement (3 of 5)
Sum Statement (4 of 5)
Sum Statement (5 of 5)
Iterative DO Loop (1 of 7) $100
3.75%
Iterative DO Loop (2 of 7) $100
3.75%
Iterative DO Loop (3 of 7) $100
3.75%
Iterative DO Loop (4 of 7)
Iterative DO Loop (5 of 7)• $100• 3.75%
Iterative DO Loop (6 of 7)
Iterative DO Loop (7 of 7)
DO Until
DO While
DO Loop Error
A Review of DODO Group Processin
g
Designates a group of
statements to be executed
as a unit.
Iterative DO Loop
Executes statements repetitively
based on the value of an
index variable.
DO Until
Executes a DO Loop until a condition is
true
Checks the condition after the iteration of each DO Loop
Do While
Executes a DO Loop until a condition is
false
Checks the condition before the iteration of
each DO Loop
• Utilize conditional statements to analyze data
• Apply iterative processing to SAS programs
• Organize SAS code to run efficient SAS programs
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