DSCI 325: Handout 4 If-Then Statements in SAS

[Pages:17]DSCI 325: Handout 4 ? If-Then Statements in SAS

Spring 2017

IF-THEN STATEMENTS IN SAS

We have already worked briefly with IF-THEN statements in SAS. Here, we will discuss using IF-THEN statements in a DATA step to assign a value to a variable using one (or more) condition(s). Note that an IF-THEN statement takes on the following general form. IF condition THEN action;

For example, consider once again the Grades data set. Suppose that the professor uses the following statements to calculate a final percentage grade for each student and to determine whether they pass or fail the course. Identify the condition and the action in each of the IFTHEN statements in the following code.

DATA Grades2; SET Hooks.Grades; TotalQuiz =

SUM(Quiz1,Quiz2,Quiz3,Quiz4,Quiz5,Quiz6,Quiz7,Quiz8,Quiz9,Quiz10,Quiz11,Quiz12);

TotalExam = SUM(Exam1,Exam2,Exam3); FinalPercent = SUM(TotalQuiz,TotalExam,EC,Final)/640; IF FinalPercent >= .70 THEN Status = 'Pass'; IF FinalPercent < .70 THEN Status = 'Fail'; RUN;

PROC PRINT Data=Grades2; VAR FirstName LastName FinalPercent Status; RUN;

Note that for each observation in the data set, when SAS sees the keyword IF it evaluates the condition to determine whether it is true or false. If the condition is true, SAS takes the action that follows the keyword THEN. If the condition is false, SAS ignores the THEN clause and simply proceeds to the next statement in the DATA step.

A portion of the result of the PROC PRINT is shown below:

1

The condition in an IF-THEN statement typically involves the use of a comparison operator (such as "greater than or equal to" as was used above). SAS uses the following syntax for the standard comparison operators. Note that you can use either syntax when programming.

Comparison Less than Greater than Less than or equal to Greater than or equal to Equal to Not equal to Equal to one of a list

SAS syntax using symbols < > = = ^= or ~= IN( )

SAS syntax using Mnemonic Equivalent

LT GT LE GE EQ NE

Change the IF-THEN statements in the above code to the following and verify that they work.

IF FinalPercent GE .70 THEN Status = 'Pass'; IF FinalPercent LT .70 THEN Status = 'Fail';

USING DO and END STATEMENTS

The IF-THEN statements discussed thus far allow only one action to take place. If multiple actions need to take place if the condition is true, then you should use DO and END statements.

Typical IF Statement (one action)

IF condition THEN action;

IF Statement using DO and END (multiple actions)

IF condition THEN DO; action 1; action 2; ...

END;

Consider the following example that has two action statements.

DATA Grades2; SET Hooks.Grades; ParenLocation = INDEX(ZipTownAreaCode,'('); Town = SUBSTR(ZipTownAreaCode,7,ParenLocation-8);

IF SUBSTR(Town,1,1) EQ 'A' THEN DO; ATown=1; BTown=0;

END; RUN;

2

Discuss what the IF statement should do in detail.

Consider the print-out from the above code. Is this what you expected?

Task: This dataset contains only towns that start with an A or B. Modify the code so that all B towns are given a value of 1 for BTown and 0 for ATown (instead of leaving them missing as was done above).

DATA Grades2; SET Hooks.Grades; ParenLocation = INDEX(ZipTownAreaCode,'('); Town = SUBSTR(ZipTownAreaCode,7,ParenLocation-8); IF SUBSTR(Town,1,1) EQ 'A' THEN DO; ATown=1; BTown=0;

END; RUN;

3

IF-THEN-ELSE Statements Next, note that the IF-THEN statement tells SAS what to do when the condition is true. To tell SAS what action to take when the condition is false, you could utilize an ELSE statement. For example, we could have also created the variable Status as follows.

DATA Grades2; SET Hooks.Grades; TotalQuiz =

SUM(Quiz1,Quiz2,Quiz3,Quiz4,Quiz5,Quiz6,Quiz7,Quiz8,Quiz9,Quiz10,Quiz11,Quiz12);

TotalExam = SUM(Exam1,Exam2,Exam3); FinalPercent = SUM(TotalQuiz,TotalExam,EC,Final)/640; IF FinalPercent >= .70 THEN Status = 'Pass';

ELSE Status = 'Fail'; RUN; PROC PRINT Data=Grades2; VAR FirstName LastName FinalPercent Status; RUN;

Task: Recall the example from the previous page. Use an ELSE statement to modify the code so that all B towns are given a value of 1 for BTown and 0 for ATown.).

DATA Grades2; SET Hooks.Grades; ParenLocation = INDEX(ZipTownAreaCode,'('); Town = SUBSTR(ZipTownAreaCode,7,ParenLocation-8); IF SUBSTR(Town,1,1) EQ 'A' THEN DO; ATown=1; BTown=0;

END; RUN;

4

Problems with Missing Data

Suppose that one of the students was ill and was unable to take the final exam as scheduled. In the data file Grades_missing.csv, this student's final exam score is a missing value. Read this file into your personal library and run code similar to the following. What happens?

DATA Grades3; SET Hooks.Grades_missing; TotalQuiz =

SUM(Quiz1,Quiz2,Quiz3,Quiz4,Quiz5,Quiz6,Quiz7,Quiz8,Quiz9,Quiz10,Quiz11,Quiz12);

TotalExam = SUM(Exam1,Exam2,Exam3); FinalPercent = SUM(TotalQuiz,TotalExam,EC,Final)/640; IF FinalPercent >= .70 THEN Status = 'Pass';

ELSE Status = 'Fail'; RUN; PROC PRINT Data=Grades3; VAR FirstName LastName FinalPercent Status; RUN;

Note: The SUM function returns the sum of all of the non-missing arguments, so SAS still calculates a value for FinalPercent. Alternatively, consider calculating FinalPercent as follows.

DATA Grades3; SET Hooks.Grades_missing; TotalQuiz =

SUM(Quiz1,Quiz2,Quiz3,Quiz4,Quiz5,Quiz6,Quiz7,Quiz8,Quiz9,Quiz10,Quiz11,Quiz12);

TotalExam = SUM(Exam1,Exam2,Exam3); FinalPercent = (TotalQuiz + TotalExam + EC + Final)/640; IF FinalPercent >= .70 THEN Status = 'Pass';

ELSE Status = 'Fail'; RUN;

Note: The `+' operator returns a missing value if any of the arguments are missing.

5

Also, note that SAS considers a missing value to be smaller than any other numerical value. Thus, it is always a good idea to program for missing values as shown below.

DATA Grades3; SET Hooks.Grades_missing;

TotalQuiz =

SUM(Quiz1,Quiz2,Quiz3,Quiz4,Quiz5,Quiz6,Quiz7,Quiz8,Quiz9,Quiz10,Quiz11,Quiz12);

TotalExam = SUM(Exam1,Exam2,Exam3); FinalPercent = (TotalQuiz + TotalExam + EC + Final)/640;

RUN;

IF FinalPercent =. THEN Status = ' '; ELSE IF FinalPercent >= .70 THEN Status = 'Pass'; ELSE Status = 'Fail';

Questions:

1. Run this program. Is this the result you expected?

2. Why did you get this result?

3. Add one statement to the DATA step to fix this problem.

DATA Grades3; SET Hooks.Grades_missing;

TotalQuiz =SUM(Quiz1,Quiz2,Quiz3,Quiz4,Quiz5,Quiz6,Quiz7,Quiz8,Quiz9, Quiz10,Quiz11,Quiz12);

TotalExam = SUM(Exam1,Exam2,Exam3); FinalPercent = (TotalQuiz + TotalExam + EC + Final)/640;

IF FinalPercent = . THEN Status = ' '; ELSE IF FinalPercent >= .70 THEN Status = 'Pass'; ELSE Status = 'Fail';

RUN;

6

Using Logical Operators in IF-THEN Statements

In addition to comparison operators, we can also use logical operators when writing a condition for an IF-THEN statement.

Operation All conditions must be true At least one condition must be true Reverse the logic of a condition

SAS syntax using symbols & | or ! ^ or ~

SAS syntax using Mnemonic Equivalent

AND OR NOT

For example, we could assign grades to the students in the file Grades.csv as follows.

DATA Grades2; SET Hooks.Grades; TotalQuiz =

SUM(Quiz1,Quiz2,Quiz3,Quiz4,Quiz5,Quiz6,Quiz7,Quiz8,Quiz9,Quiz10,Quiz11,Quiz12);

TotalExam = SUM(Exam1,Exam2,Exam3); FinalPercent = SUM(TotalQuiz,TotalExam,EC,Final)/640;

IF (FinalPercent >= 0.90)

THEN Grade='A';

IF (FinalPercent >= 0.80) AND (FinalPercent < 0.90) THEN Grade='B';

IF (FinalPercent >= 0.70) AND (FinalPercent < 0.80) THEN Grade='C';

IF (FinalPercent >= 0.60) AND (FinalPercent < 0.70) THEN Grade='D';

IF (FinalPercent < 0.60)

THEN Grade='F';

RUN;

PROC PRINT Data=Grades2; VAR FirstName LastName Final FinalPercent Grade; RUN;

Comment: Though it is not necessary, it is good practice to write programs that are easy to read (the earlier you develop this habit, the better off you'll be). For example, in the above code, the conditions and actions have been aligned, and optional parentheses have been used above to offset the conditions.

These statements create the following data set (only a portion is shown here).

7

Task: Now, suppose the instructor wants to give bonus points to students who show some sign of improvement from the beginning of the course to the end of the course. Suppose that two percentage points will be added to a student's final percentage grade if either their Exam 1 grade is less than their Exam 3 and Final grade or their Exam 2 grade is less than their Exam 3 and Final grade. Call this new variable FinalPercent_Adjusted. Finish the following SAS code to do this, and verify that your program works.

DATA Grades2; SET Hooks.Grades; TotalQuiz =

SUM(Quiz1,Quiz2,Quiz3,Quiz4,Quiz5,Quiz6,Quiz7,Quiz8,Quiz9,Quiz10,Quiz11,Quiz12);

TotalExam = SUM(Exam1,Exam2,Exam3); FinalPercent = SUM(TotalQuiz,TotalExam,EC,Final)/640;

8

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download