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 18.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 18.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 409
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 I/O Files 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.
410 Chapter 18 DATA Step Processing
Table 18.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 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. For specific information about
SAS functions, see the SAS I/O Files and External Files categories in “SAS Functions
and CALL Routines by Category” 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 18.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 411
SAS Language Element Function
HEADER= option in the FILE statement alters the flow of control whenever a PUT
statement causes a new page of output to begin;
statements following the label specified in the
HEADER= option are executed until a RETURN
statement is encountered, at which time control
returns to the point from which the HEADER=
option was activated.
GO TO statement alters the flow of execution by branching to the
label that is specified in the GO TO statement. SAS
executes subsequent statements then returns control
to the beginning of the DATA step.
EOF= option in an INFILE statement alters the flow of execution when the end of the
input file is reached; statements following the label
that is specified in the EOF= option are executed at
that time.
_N_ automatic variable in an IF-THEN
construct
causes parts of the DATA step to execute only for
particular iterations.
SELECT statement conditionally executes one of a group of SAS
statements.
OUTPUT statement in an IF-THEN
construct
outputs an observation before the end of the DATA
step, based on a condition; prevents automatic
output at the bottom of the DATA step.
DELETE statement in an IF-THEN
construct
deletes an observation based on a condition and
causes a return to the top of the DATA step.
ABORT statement in an IF-THEN
construct
stops execution of the DATA step and instruct SAS
to resume execution with the next DATA or PROC
step. It can also stop executing a SAS program
altogether, depending on the options specified in
the ABORT statement and on the method of
operation.
WHERE statement or WHERE= data set
option
causes SAS to read certain observations based on
one or more specified criteria.
Step Boundary—How to Know When Statements Take Effect
Understanding step boundaries is an important concept in SAS programming because
step boundaries determine when SAS statements take effect. SAS executes program
statements only when SAS crosses a default or a step boundary. Consider the following
DATA steps:
data _null_; 1
set allscores(drop=score5-score7);
title 'Student Test Scores'; 2
412 Chapter 18 DATA Step Processing
data employees; 3
set employee_list;
run;
1
The DATA statement begins a DATA step and is a step boundary.
2
The TITLE statement is in effect for both DATA steps because it appears before the
boundary of the first DATA step. (The TITLE statement is a global statement.)
3
The DATA statement is the default boundary for the first DATA step.
The TITLE statement in this example is in effect for the first DATA step as well as for
the second because the TITLE statement appears before the boundary of the first DATA
step. This example uses the default step boundary data employees;.
The following example shows an OPTIONS statement inserted after a RUN statement.
data scores; 1
set allscores(drop=score5-score7);
run; 2
options firstobs=5 obs=55; 3
data test;
set alltests;
run;
1
The DATA statement is a step boundary.
2
The RUN statement is the boundary for the first DATA step.
3
The OPTIONS statement affects the second DATA step only.
The OPTIONS statement specifies that the first observation that is read from the input
data set should be the 5th, and the last observation that is read should be the 55th.
Inserting a RUN statement immediately before the OPTIONS statement causes the first
DATA step to reach its boundary (run;) before SAS encounters the OPTIONS
statement. The OPTIONS statement settings, therefore, are put into effect for the second
DATA step only.
Following the statements in a DATA step with a RUN statement is the simplest way to
make the step begin to execute, but a RUN statement is not always necessary. SAS
recognizes several step boundaries for a SAS step:
another DATA statement
a PROC statement
a RUN statement
Note: For SAS programs executed in interactive mode, a RUN statement is required
to signal the step boundary for the last step that you submit.
the semicolon (with a DATALINES or CARDS statement) or four semicolons (with a
DATALINES4 or CARDS4 statement) after data lines
an ENDSAS statement
in noninteractive or batch mode, the end of a program file containing SAS
programming statements
a QUIT statement (for some procedures)
When you submit a DATA step during interactive processing, it does not begin running
until SAS encounters a step boundary. This fact enables you to submit statements as you
About DATA Step Execution 413
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.232