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-15]


OUTPUT Statement

The OUTPUT statement can be used to control how the observations are written onto the data set.

Example

Data Test;
Var1 = 123;
Var1 = 456;
Var1 = 789;

Run;

In this example, the VAR1 variable is assigned a different value for 3 times. 

We might hope that SAS writes the 3 different values onto the data set with 3 observations. However, that is not the case.

Before SAS writes the value to the data set, the last value assigned to the variable VAR1 (i.e. 789) overwrites the first two values (i.e. 123 and 456). 

The TEST data set created will contain only 1 observation with the last assigned value, 789.
Picture

An OUTPUT statement can be used to tell SAS to write each value to the data set.

Example

Data Test;
Var1 = 123; Output;
Var1 = 456; Output;
Var1 = 789; Output;
Run;

With the OUTPUT statement, each value assigned to the VAR1 variable is written onto the data set.

The TEST data set has now 3 observations:
Picture

Creating Multiple Data Sets

The OUTPUT statement can also be used along with the If-Then statement to create multiple data set within just one data step.

Example

​
Picture

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

Now, we will create two data sets, PASS and FAIL, to capture the students who passed and failed the course, respectively.

Example

Data Pass Fail;
Set Exam;
If MidTerm>50 and Final>50 then Output Pass;
Else Output Fail;
Run;

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.