Search the site...

SASCRUNCH TRAINING
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
Sentry Page Protection
Please Wait...
SAS Functions [14-14]


IF-THEN STATEMENT

The If-Then statement allows you to perform different actions based on whether the pre-specified condition(s) are met.
Picture

The EXAM data set above contains a list of mid-term and final exam results.

In order to pass the course, each student has to pass both the mid-term and final exam.

We can create a variable to indicate whether the student passes the course or not, based on the mid-term and final exam results.

Example

Data Exam2;
Set Exam;
If MidTerm>50 and Final>50 then Result="Pass";
Run;
Picture

We can also perform another action using the ELSE statement were the condition(s) not met.

Example

Data Exam3;
Set Exam;
If MidTerm>50 and Final>50 then Result="Pass";
Else Result = "Fail";
Run;
Picture

DO STATEMENT

You can perform multiple actions within an If-Then statement using a Do statement.

Now, let's assume the student passes the course if they pass the final exam, regardless of how they did in the mid-term.

Example

Data Exam4;
Set Exam;
If Final > 50 then do;
Result = "Pass";
Mark = 0.5*midterm + 0.5*final;
end;

Run;
Picture

In this example, two variables (i.e. RESULT and MARK) are created when the student passed the final exam.

The RESULT variable indicates whether the student has passed the course.

The MARK variable contains the final mark for each student.

Since there are two actions to take place when the condition is met, a DO statement is needed.

The DO statement starts with the syntax "DO". 

You can execute as many actions as you want within the DO statement. 

However, you must end the DO statement with an END statement.

Example

Data Exam4;
Set Exam;
If Final > 50 then do;
Result = "Pass";
Mark = 0.5*midterm + 0.5*final;
end;
Run;


SUBSETTING DATA SET

As discussed in DATA SET [7-14], an IF statement can also be used to subset a data set.

Example

Data Exam5;
Set Exam;
If Final > 80;
Run;

Picture
 
The IF statement above subsets the data set and keep only the observations where the exam result is greater than 80. 

Only 4 observations are left in the data set.

Exercise

Locate the CARS data set from the SASHELP library.

Write a SAS program to classify each car as either Economy or Luxury. 

The Economy cars have the MSRP (Manufacturer's Suggested Retail Price) below $40,000. The Luxury cars have the MSRP above $40,000.

Create any data set or variable if needed.
Next

Need some help? 


HINT:
A simple If-Then statement should do the job.


SOLUTION:
Data Cars;
Set SASHelp.Cars;
if msrp < 40000 then Class = "Economy";
else Class = "Luxury";
Run;


Fill out my online form.

Already a member? Go to member's area.