bas 150 lesson 5 lecture

37
BAS 150 Lesson 5: Conditional Statements and Iterative Processing

Upload: wake-tech-bas

Post on 22-Jan-2017

34 views

Category:

Education


0 download

TRANSCRIPT

Page 1: BAS 150 Lesson 5 Lecture

BAS 150Lesson 5: Conditional Statements and Iterative Processing

Page 2: BAS 150 Lesson 5 Lecture

• 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

Page 3: BAS 150 Lesson 5 Lecture

Conditional Statements

Page 4: BAS 150 Lesson 5 Lecture

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

Page 5: BAS 150 Lesson 5 Lecture

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

Page 6: BAS 150 Lesson 5 Lecture

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)

Page 7: BAS 150 Lesson 5 Lecture

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.

Page 8: BAS 150 Lesson 5 Lecture

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.

Page 9: BAS 150 Lesson 5 Lecture

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

Page 10: BAS 150 Lesson 5 Lecture

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

Page 11: BAS 150 Lesson 5 Lecture

Boolean Operators

And

Or

Not

Logical Operators

Page 12: BAS 150 Lesson 5 Lecture

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

Page 13: BAS 150 Lesson 5 Lecture

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.)

Page 14: BAS 150 Lesson 5 Lecture

Iterative Processing

Page 15: BAS 150 Lesson 5 Lecture

DO Groups (1 of 5)

IF <condition> THEN DO;

<X>; <Y>; <Z>;

END;

If Score >= 90 Then Do;Grade = 'A'; Pass_Fail = 'Pass';

End;

Page 16: BAS 150 Lesson 5 Lecture

DO Groups (2 of 5)

Page 17: BAS 150 Lesson 5 Lecture

DO Groups (3 of 5)

Page 18: BAS 150 Lesson 5 Lecture

DO Groups (4 of 5)

Page 19: BAS 150 Lesson 5 Lecture

DO Groups (5 of 5)

Page 20: BAS 150 Lesson 5 Lecture

Sum Statement (1 of 5)

Variable + expression

Running totals or counters

Page 21: BAS 150 Lesson 5 Lecture

Sum Statement (2 of 5)

Page 22: BAS 150 Lesson 5 Lecture

Sum Statement (3 of 5)

Page 23: BAS 150 Lesson 5 Lecture

Sum Statement (4 of 5)

Page 24: BAS 150 Lesson 5 Lecture

Sum Statement (5 of 5)

Page 25: BAS 150 Lesson 5 Lecture

Iterative DO Loop (1 of 7) $100

3.75%

Page 26: BAS 150 Lesson 5 Lecture

Iterative DO Loop (2 of 7) $100

3.75%

Page 27: BAS 150 Lesson 5 Lecture

Iterative DO Loop (3 of 7) $100

3.75%

Page 28: BAS 150 Lesson 5 Lecture

Iterative DO Loop (4 of 7)

Page 29: BAS 150 Lesson 5 Lecture

Iterative DO Loop (5 of 7)• $100• 3.75%

Page 30: BAS 150 Lesson 5 Lecture

Iterative DO Loop (6 of 7)

Page 31: BAS 150 Lesson 5 Lecture

Iterative DO Loop (7 of 7)

Page 32: BAS 150 Lesson 5 Lecture

DO Until

Page 33: BAS 150 Lesson 5 Lecture

DO While

Page 34: BAS 150 Lesson 5 Lecture

DO Loop Error

Page 35: BAS 150 Lesson 5 Lecture

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

Page 36: BAS 150 Lesson 5 Lecture

• Utilize conditional statements to analyze data

• Apply iterative processing to SAS programs

• Organize SAS code to run efficient SAS programs

Summary - Learning Objectives

Page 37: BAS 150 Lesson 5 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