Published on :
Action CREATION_INTERNE

OUTPUT Statement

This code is also available in: Deutsch Español Français
Awaiting validation
The OUTPUT statement tells SAS© to write the current observation to a SAS© dataset immediately, rather than at the end of the DATA step. If no dataset name is specified in the OUTPUT statement, the observation is written to the dataset(s) listed in the DATA statement.
By default, each DATA step contains an implicit OUTPUT statement at the end of each iteration that tells SAS© to write observations to the dataset(s) being created. Including an explicit OUTPUT statement in a DATA step overrides automatic output, and SAS© adds an observation to a dataset only when an explicit OUTPUT statement is executed. However, once you use an OUTPUT statement to write an observation to a dataset, there is no longer an implicit OUTPUT statement at the end of the DATA step. In this situation, a DATA step writes an observation to a dataset only when an explicit OUTPUT statement is executed. You can use the OUTPUT statement alone or as part of an IF-THEN, SELECT, or DO loop processing.
When using the MODIFY statement with the OUTPUT statement, the REMOVE and REPLACE statements cancel the implicit write action at the end of each DATA step iteration. If both OUTPUT and REPLACE or REMOVE statements execute for a given observation, perform the output action last to maintain the correct observation pointer position.
Data Analysis

Type : CREATION_INTERNE


Examples use generated data (datalines) or SASHELP.

1 Code Block
DATA STEP
Explanation :
These code snippets demonstrate simple uses of the OUTPUT statement. The first line writes the current observation. The second writes the observation if 'deptcode' is greater than 2000. The third writes the observation to the 'markup' dataset if the 'phone' variable is missing.
Copied!
1/* Écrit l'observation courante dans un ensemble de données SAS spécifié dans l'instruction DATA */
2OUTPUT;
3 
4/* Écrit l'observation courante dans un ensemble de données SAS lorsqu'une condition spécifiée est vraie */
5IF deptcode gt 2000 THEN OUTPUT;
6 
7/* Écrit une observation dans l'ensemble de données MARKUP lorsque la valeur de PHONE est manquante */
8IF phone=. THEN OUTPUT markup;
2 Code Block
DATA STEP Data
Explanation :
This example creates three observations in the 'response' dataset for each observation in the 'sulfa' dataset. The 'sulfa' dataset is first created with inline data (datalines). Then, the 'response' DATA step reads each observation from 'sulfa' and uses the OUTPUT statement three times to create three new observations, each with a different 'time' value from the 'time1', 'time2', and 'time3' variables.
Copied!
1DATA sulfa;
2 INPUT patient $ time1 time2 time3;
3 DATALINES;
4pat1 10 20 30
5pat2 15 25 35
6;
7RUN;
8 
9DATA response(drop=time1-time3);
10 SET sulfa;
11 time=time1;
12 OUTPUT;
13 time=time2;
14 OUTPUT;
15 time=time3;
16 OUTPUT;
17RUN;
3 Code Block
DATA STEP Data
Explanation :
This example shows how to create multiple SAS datasets ('ozone' and 'oxides') from a single input file. A temporary input file 'input_data.txt' is first created. The DATA step then reads data from this file. If the chemical substance is 'O3', the observation is written to the 'ozone' dataset; otherwise, it is written to the 'oxides' dataset.
Copied!
1DATA _null_;
2 file 'input_data.txt';
3 put 'London 01JAN2025 O3 25';
4 put 'Paris 01JAN2025 NOx 15';
5 put 'Berlin 01JAN2025 O3 30';
6 put 'Rome 01JAN2025 SO2 10';
7RUN;
8 
9DATA ozone oxides;
10 INFILE 'input_data.txt';
11 INPUT city $ 1-15 date date9.
12 chemical $ 26-27 ppm 29-30;
13 IF chemical='O3' THEN OUTPUT ozone;
14 ELSE OUTPUT oxides;
15RUN;
4 Code Block
DATA STEP Data
Explanation :
This example combines multiple input observations into a single one. The 'gadgets' dataset is created with defect values. The 'discards' DATA step reads these observations, accumulates the 'defects' variable into 'total', and writes a single observation to 'discards' after processing 10 observations, then stops processing.
Copied!
1DATA gadgets;
2 INPUT defects;
3 DATALINES;
41
52
63
74
85
96
107
118
129
1310
1411
1512
16;
17RUN;
18 
19DATA discards;
20 SET gadgets;
21 drop defects;
22 reps+1;
23 IF reps=1 THEN total=0;
24 total+defects;
25 IF reps=10 THEN DO;
26 OUTPUT;
27 stop;
28 END;
29RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright © SAS Institute Inc. All Rights Reserved


Banner
Expert Advice
Expert
Michael
Responsable de l'infrastructure Viya.
« The OUTPUT statement is one of the most powerful tools in a SAS developer's arsenal for transitioning from simple data cleaning to complex data restructuring. By default, SAS operates on an "implicit output" logic—writing one observation at the very end of each DATA step iteration. However, using an explicit OUTPUT statement grants you manual control over exactly when and where data is committed to disk. »