input TeamName $ ParticipantName $ Event1 Event2 Event3;
TeamTotal + (Event1 + Event2 + Event3);
datalines;
Knights Sue 6 8 8
Cardinals Jane 9 7 8
Knights John 7 7 7
Cardinals Lisa 8 9 9
Cardinals Fran 7 6 6
Knights Walter 9 8 10
;
proc print data=total_points;
title 'Total Team Scores';
run;
Output 20.1 Output from the Walk-through DATA Step
About DATA Step Execution
The Default Sequence of Execution in the DATA Step
The following table outlines the default sequence of execution for statements in a DATA
step. The DATA statement begins the step and identifies usually one or more SAS data
sets that the step will create. (You can use the keyword _NULL_ as the data set name if
you do not want to create an output data set.) Optional programming statements process
your data. SAS then performs the default actions at the end of processing an observation.
Table 20.1 Default Execution for Statements in a DATA Step
Structure of a DATA Step Action
DATA statement begins the step
counts iterations
Data-reading statements:
*
About DATA Step Execution 419
Structure of a DATA Step Action
INPUT describes the arrangement of values in the
input data record from a raw data source
SET reads an observation from one or more SAS
data sets
MERGE joins observations from two or more SAS data
sets into a single observation
MODIFY replaces, deletes, or appends observations in
an existing SAS data set in place
UPDATE updates a master file by applying transactions
Optional SAS programming statements, for
example:
further processes the data for the current
observation
FirstQuarter=Jan+Feb+Mar;
if RetailPrice < 500;
computes the value for FirstQuarter for the
current observation
subsets by value of variable RetailPrice for the
current observation
Default actions at the end of processing an
observation
At end of DATA step:
Automatic write, automatic
return
At top of DATA step:
Automatic reset
writes an observation to a SAS data set
returns to the DATA statement
resets values to missing in program data
vector
*
The table shows the default processing of the DATA step. You can alter the sequence of statements in the
DATA step. You can code optional programming statements, such as creating or reinitializing a constant,
before you code a data-reading statement.
Note: You can also use functions to read and process data. For information about how
statements and functions process data differently, see “Using Functions to
Manipulate Files” in SAS Functions and CALL Routines: Reference. For specific
information about SAS functions, see the SAS File I/O and External Files categories
in “SAS Functions and CALL Routines by Category” in SAS Functions and CALL
Routines: Reference.
Changing the Default Sequence of Execution
Using Statements to Change the Default Sequence of Execution
You can change the default sequence of execution to control how your program
executes. SAS language statements offer you a lot of flexibility to do this in a DATA
step. The following list shows the most common ways to control the flow of execution in
a DATA step program.
420 Chapter 20 DATA Step Processing
Table 20.2 Common Methods That Alter the Sequence of Execution
Task Possible Methods
Read a record merge, modify, join data sets
read multiple records to create a single observation
randomly select records for processing
read from multiple external files
read selected fields from a record by using statement or data
set options
Process data use conditional logic
retain variable values
Write an observation write to a SAS data set or to an external file
control when output is written to a data set
write to multiple files
For more information, see the individual statements in SAS DATA Step Statements:
Reference.
Using Functions to Change the Default Sequence of Execution
You can also use functions to read and process data. For information about how
statements and functions process data differently, see “Using Functions to Manipulate
Files” in SAS Functions and CALL Routines: Reference..
Altering the Flow for a Given Observation
You can use statements, statement options, and data set options to alter how SAS
processes specific observations. The following table lists SAS language elements and
their effects on processing.
Table 20.3 Language Elements That Alter Programming Flow
SAS Language Element Function
subsetting IF statement stops the current iteration when a condition is false,
does not write the current observation to the data
set, and returns control to the top of the DATA step.
IF-THEN/ELSE statement executes a SAS statement for observations that
meet the current condition and continues with the
next statement.
DO loops cause parts of the DATA step to be executed
multiple times.
LINK and RETURN statements alter the flow of control, execute statements
following the label specified, and return control of
the program to the next statement following the
LINK statement.
About DATA Step Execution 421

Get SAS 9.4 Language Reference, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.