{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter 5 Conditional Processing

Chapter 5 Conditional Processing - STAT1303 Data Management...

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

View Full Document Right Arrow Icon
STAT1303 Data Management 5. Conditional Processing 5 Conditional Processing 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’ A condition expression is either true or false. 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. * Example 5.1 - IF-THEN-ELSE construct; * numeric variable in IF-THEN; data sportcars; infile ’D:/temp/cars.dat’; input model $ year make $ seats color $; if year< 1975 then status = ’classic’; run; * character variable in IF-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
Image of page 1

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

View Full Document Right 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 ; For example, 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; * nested ELSE; 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
Image of page 2
STAT1303 Data Management 5. Conditional Processing IF condition-1 THEN DO;
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern