O'Reilly logo

Step-by-Step Programming with Base SAS 9.4, Second Edition, 2nd Edition by SAS Institute

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The following output displays the results.
Figure 11.6 Producing an Exact Subset with the Subsetting IF Statement
The result is a SAS data set with no missing values for Budget.
Conditionally Writing Observations to One or
More SAS Data Sets
Understanding the OUTPUT Statement
SAS enables you to create multiple SAS data sets in a single DATA step using an
OUTPUT statement:
OUTPUT <SAS-data-set(s)>;
When you use an OUTPUT statement without specifying a data set name, SAS writes
the current observation to all data sets that are named in the DATA statement. If you
want to write observations to a selected data set, then you specify that data set name
directly in the OUTPUT statement. Any data set name appearing in the OUTPUT
statement must also appear in the DATA statement.
Example for Conditionally Writing Observations to Multiple Data
Sets
One of the SAS data sets contains tours that are guided by the tour guide Lucas and the
other contains tours led by other guides. Writing to multiple data sets is accomplished by
performing the following tasks:
naming both data sets in the DATA statement
selecting the observations using an IF condition
using an OUTPUT statement in the THEN and ELSE clauses to output the
observations to the appropriate data sets
The following DATA step shows how to write to multiple data sets:
data lucastour othertours;
set mylib.arts;
if TourGuide='Lucas' then output lucastour;
else output othertours;
run;
Conditionally Writing Observations to One or More SAS Data Sets 175
proc print data=lucastour;
title "Data Set with TourGuide='Lucas'";
run;
proc print data=othertours;
title "Data Set with Other Guides";
run;
The following output displays the results.
Figure 11.7 Creating Two Data Sets with One DATA Step
Avoiding a Common Mistake When Writing to Multiple Data Sets
If you use an OUTPUT statement, then you suppress the automatic output of
observations at the end of the DATA step. Therefore, if you plan to use any OUTPUT
statements in a DATA step, then you must program all output for that step with
OUTPUT statements. For example, in the previous DATA step you sent output to both
LUCASTOUR and OTHERTOURS. For comparison, the following program shows
what would happen if you omit the ELSE statement in the DATA step:
data lucastour2 othertour2;
set mylib.arts;
if TourGuide='Lucas' then output lucastour2;
run;
proc print data=lucastour2;
title "Data Set with Guide='Lucas'";
run;
proc print data=othertour2;
title "Data Set with Other Guides";
run;
176 Chapter 11 Creating Subsets of Observations
The following output displays the results.
Figure 11.8 Failing to Direct Output to a Second Data Set
No observations are written to OTHERTOUR2 because output was not directed to it.
Understanding Why the Placement of the OUTPUT Statement Is
Important
By default SAS writes an observation to the output data set at the end of each iteration.
When you use an OUTPUT statement, you override the automatic output feature. Where
you place the OUTPUT statement, therefore, is very important. For example, if a
variable value is calculated after the OUTPUT statement executes, then that value is not
available when the observation is written to the output data set.
For example, in the following DATA step, an assignment statement is placed after the IF-
THEN/ELSE group:
/* first attempt to combine assignment and OUTPUT statements */
data lucasdays otherdays;
set mylib.arts;
if TourGuide='Lucas' then output lucasdays;
else output otherdays;
Days=Nights+1;
run;
proc print data=lucasdays;
title "Number of Days in Lucas's Tours";
run;
proc print data=otherdays;
title "Number of Days in Other Guides' Tours";
run;
The following output displays the results.
Figure 11.9 Unintended Results: Writing Observations Before Assigning Values
Conditionally Writing Observations to One or More SAS Data Sets 177
The value of Days is missing in all observations because the OUTPUT statement writes
the observation to the SAS data sets before the assignment statement is processed. If you
want the value of Day to appear in the data sets, then use the assignment statement
before you use the OUTPUT statement. The following program shows the correct
position:
/* correct position of assignment statement */
data lucasdays2 otherdays2;
set mylib.arts;
Days=Nights + 1;
if TourGuide='Lucas' then output lucasdays2;
else output otherdays2;
run;
proc print data=lucasdays2;
title "Number of Days in Lucas's Tours";
run;
proc print data=otherdays2;
title "Number of Days in Other Guides' Tours";
run;
The following output displays the results.
Figure 11.10 Intended Results: Assigning Values After Writing Observations
178 Chapter 11 Creating Subsets of Observations

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required