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!
/* Écrit l'observation courante dans un ensemble de données SAS spécifié dans l'instruction DATA */
output;
/* Écrit l'observation courante dans un ensemble de données SAS lorsqu'une condition spécifiée est vraie */
if deptcode gt 2000 then output;
/* Écrit une observation dans l'ensemble de données MARKUP lorsque la valeur de PHONE est manquante */
if phone=. then output markup;
1
/* Écrit l'observation courante dans un ensemble de données SAS spécifié dans l'instruction DATA */
2
OUTPUT;
3
4
/* Écrit l'observation courante dans un ensemble de données SAS lorsqu'une condition spécifiée est vraie */
5
IF deptcode gt 2000THENOUTPUT;
6
7
/* Écrit une observation dans l'ensemble de données MARKUP lorsque la valeur de PHONE est manquante */
8
IF phone=. THENOUTPUT 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!
data sulfa;
input patient $ time1 time2 time3;
datalines;
pat1 10 20 30
pat2 15 25 35
;
run;
data response(drop=time1-time3);
set sulfa;
time=time1;
output;
time=time2;
output;
time=time3;
output;
run;
1
DATA sulfa;
2
INPUT patient $ time1 time2 time3;
3
DATALINES;
4
pat1 102030
5
pat2 152535
6
;
7
RUN;
8
9
DATA response(drop=time1-time3);
10
SET sulfa;
11
time=time1;
12
OUTPUT;
13
time=time2;
14
OUTPUT;
15
time=time3;
16
OUTPUT;
17
RUN;
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!
data _null_;
file 'input_data.txt';
put 'London 01JAN2025 O3 25';
put 'Paris 01JAN2025 NOx 15';
put 'Berlin 01JAN2025 O3 30';
put 'Rome 01JAN2025 SO2 10';
run;
data ozone oxides;
infile 'input_data.txt';
input city $ 1-15 date date9.
chemical $ 26-27 ppm 29-30;
if chemical='O3' then output ozone;
else output oxides;
run;
1
DATA _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';
7
RUN;
8
9
DATA 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'THENOUTPUT ozone;
14
ELSEOUTPUT oxides;
15
RUN;
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!
data gadgets;
input defects;
datalines;
1
2
3
4
5
6
7
8
9
10
11
12
;
run;
data discards;
set gadgets;
drop defects;
reps+1;
if reps=1 then total=0;
total+defects;
if reps=10 then do;
output;
stop;
end;
run;
1
DATA gadgets;
2
INPUT defects;
3
DATALINES;
4
1
5
2
6
3
7
4
8
5
9
6
10
7
11
8
12
9
13
10
14
11
15
12
16
;
17
RUN;
18
19
DATA discards;
20
SET gadgets;
21
drop defects;
22
reps+1;
23
IF reps=1THEN total=0;
24
total+defects;
25
IF reps=10THENDO;
26
OUTPUT;
27
stop;
28
END;
29
RUN;
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.
« 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. »
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.