Chapter 5 Conditional Processing

Chapter 5 Conditional Processing - STAT1303 Data Management...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
STAT1303 Data Management 5. Conditional Processing 5C o n d i t i o n a lP r o c e s s i n g In previous chapter, we have covered the creation of various types of variables by using SAS functions. In this chapter, the creation of variables is further complicated in the presence of logical expressions based on other variables. 5.1 IF-THEN-ELSE Construct To select observations for action in a Data Step, we need some control statements. The most common control statement is the statement IF-THEN and its syntax is IF condition THEN action ; where the condition is called a conditional expression, e.g. country = ’Japan’ country = nation grade > ’D’ Acond it ionexpress ionise ithertrueorfa lse .When condition is true, SAS takes the action in the THEN clause. The action must be SAS executable statement, e.g. IF country = ’Japan’ THEN nation_code = ’JPN’; IF grade > ’D’ THEN result = ’fail’; IF grade > ’D’ THEN gpa = 0; Example 5.1. IF-THEN-ELSE construct. *Example5.1-IF-THEN-ELSEcon s t r u c t ; *numericvari ableinIF-THEN; data sportcars; infile ’D:/temp/cars.dat’; input model $ year make $ seats color $; if year< 1975 then status = ’classic’; run; *characterva riableinIF-THEN; data sportcars; infile ’D:/temp/cars.dat’; input model $ year make $ seats color $; if year < 1975 then status = ’classic’; if model = ’Corvette’ then make = ’Chevy’; run; An alternative action can be added to the IF-THEN statement through the use of the ELSE statement, i.e., HKU STAT1303 (2011-12, Semester 1) 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
STAT1303 Data Management 5. Conditional Processing IF condition THEN action-1 ; ELSE action-2 ; For example, IF grade > ’D’ THEN result = ’fail’; ELSE result = ’pass’; *IF-THEN-ELSE; data sportcars; infile ’D:/temp/cars.dat’; input model $ year make $ seats color $; length yrgroup $ 11; if year = . then yrgroup = ’missing’; else yrgroup = ’Non-missing’; run; The IF-THEN-ELSE construct can be nested to create a series of mutually exclusive conditions, i.e., IF condition-1 THEN action-1 ; ELSE IF condition-2 THEN action-2 ; ELSE action-3 ; IF grade = ’A’ THEN gpa = 4; ELSE IF grade = ’B’ THEN gpa = 3; ELSE IF grade = ’C’ THEN gpa = 2; ELSE IF grade = ’D’ THEN gpa = 1; ELSE gpa = 0; *nestedELSE; data sportcars; infile ’D:/temp/cars.dat’; input model $ year make $ seats color $; length yrgroup $ 10; if year = . then yrgroup = ’missing’; else if year < 1990 then yrgroup = ’Group 1’; else if year < 2000 then yrgroup = ’Group 2’; else yrgroup = ’Group 3’; run; 5.1.1 DO-END Construct The action clause can be extended to a series of action statements through the use of DO-END statement and its syntax is given by HKU STAT1303 (2011-12, Semester 1) 2
Background image of page 2
STAT1303 Data Management 5. Conditional Processing IF condition-1 THEN DO; Action-1 ;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/09/2012 for the course STAT 1301 taught by Professor Smslee during the Spring '08 term at HKU.

Page1 / 10

Chapter 5 Conditional Processing - STAT1303 Data Management...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online