MET MA 603: SAS Programming and Applications
MET MA 603:
SAS Programming and Applications
The Output Statement
1
1
The Implicit Output Statement
By default, SAS executes each statement in a Data step in order, and then it writes the values of the current observation to the output dataset before executing the statements for the next observation.
SAS writes the observations to the output dataset because there is an implicit output statement built within the Data step. The implicit output statement is executed only when the Data step doesn’t contain an explicit output statement.
2
2
The Explicit Output Statement
The Output statement controls when SAS writes the current values of an observation to an output dataset.
data numbers;
set mydata.integers;
output work.numbers;
run;
In the example, the output statement instructs SAS to write the current value of the observation to the dataset work.numbers. Note that this is exactly the way the implicit output statement functions – here, the implicit statement is simply made explicit.
Note that if the name of the output dataset is not included in the output statement that the result will be the same.
3
3
The Conditional Output Statement
The explicit output statement is used in situations where we need to control when observations are written to the output dataset (otherwise we can simply rely on the implicit output statement). This control is usually accomplished using logical operators. A Conditional Output statement writes observations to an output dataset based on evaluating a logical statement.
data odd_numbers;
set mydata.integers;
if mod(Number, 2) = 1 then output work.odd_numbers;
run;
A special case of the conditional output statement is the Subset statement, which is really just a short-hand version of the conditional output statement.
4
4
Output to Multiple Datasets
More than one dataset name can appear in the Data statement, thus instructing SAS to create multiple output datasets.
data odd_numbers even_numbers;
set mydata.integers;
if mod(Number, 2) = 1 then output work.odd_numbers;
else output work.even_numbers;
run;
Generally, when outputting to multiple datasets, a conditional output statement is used, such that observations are written to one or another of the output datasets based on a condition. Otherwise, all observations will be written to all output datasets, creating duplicate datasets.
5
5
The Output Statement within a Do Loop
Combining the Output statement with the Do Loop allows for a single observation to generate multiple observations in the output dataset.
data odd_numbers;
do Number = 1 to 100;
output;
end;
run;
6
6
Practice
Use a single Data step to create 4 output datasets. The first dataset should contain the odd integers from 1 to 100, the second dataset should contain the even integers from 1 to 100. Similarly, the third and fourth datasets should contain the integers even divisible by 3 and 4, respectively.
Solve the problem above using a) the integers.sas7bdat dataset as an input, and b) without using an input dataset.
7
7
Readings
Textbook section 6.10, 6.11
8
8
/docProps/thumbnail.jpeg