Examples use generated data (datalines) for datasets 'animal', 'plant', 'Year1', 'Year2', 'Table1', and 'Table2'.
1 Code Block
DATA STEP Data
Explanation : This example uses the SET statement to concatenate two datasets ('animal' and 'plant') into a single output dataset ('concatenate'). Observations from 'animal' are placed before those from 'plant'. Common variables ('common') are handled, and uncommon variables are filled with missing values. The example demonstrates the creation of the 'animal' and 'plant' datasets using datalines to make them self-contained.
Copied!
data animal;
input common $ animal$;
datalines;
a Ant
b Bird
c Cat
d Dog
e Eagle
f Frog
;
run;
data plant;
input common $ plant$;
datalines;
a Apple
b Banana
c Coconut
d Dewberry
e Eggplant
f Fig
;
run;
data concatenate;
set animal plant;
run;
proc print data=concatenate;
run;
1
DATA animal;
2
INPUT common $ animal$;
3
DATALINES;
4
a Ant
5
b Bird
6
c Cat
7
d Dog
8
e Eagle
9
f Frog
10
;
11
RUN;
12
13
DATA plant;
14
INPUT common $ plant$;
15
DATALINES;
16
a Apple
17
b Banana
18
c Coconut
19
d Dewberry
20
e Eggplant
21
f Fig
22
;
23
RUN;
24
25
DATA concatenate;
26
SET animal plant;
27
RUN;
28
PROC PRINTDATA=concatenate;
29
RUN;
2 Code Block
PROC SQL Data
Explanation : This example uses PROC SQL with the OUTER UNION CORRESPONDING statement to concatenate the 'animal' and 'plant' datasets. This combines all rows from both tables and superimposes common variables. The result is stored in a new table named 'combined'. The 'animal' and 'plant' datasets are created using datalines to ensure the example's autonomy.
Copied!
data animal;
input common $ animal$;
datalines;
a Ant
b Bird
c Cat
d Dog
e Eagle
f Frog
;
run;
data plant;
input common $ plant$;
datalines;
a Apple
b Banana
c Coconut
d Dewberry
e Eggplant
f Fig
;
run;
proc sql;
create table combined as
select * from animal
outer union corresponding
select * from plant;
quit;
1
DATA animal;
2
INPUT common $ animal$;
3
DATALINES;
4
a Ant
5
b Bird
6
c Cat
7
d Dog
8
e Eagle
9
f Frog
10
;
11
RUN;
12
13
DATA plant;
14
INPUT common $ plant$;
15
DATALINES;
16
a Apple
17
b Banana
18
c Coconut
19
d Dewberry
20
e Eggplant
21
f Fig
22
;
23
RUN;
24
25
PROC SQL;
26
create TABLE combined as
27
select * from animal
28
outer union corresponding
29
select * from plant;
30
QUIT;
3 Code Block
PROC APPEND Data
Explanation : This example demonstrates the use of PROC APPEND to add observations from the 'Year2' dataset to the end of the 'Year1' dataset. Unlike the SET statement, PROC APPEND directly modifies the 'BASE=' dataset ('Year1' here) and does not create a new dataset. The 'Year1' and 'Year2' datasets are created with datalines.
Explanation : This example illustrates how the OPEN=DEFER option in the DATA statement can be used to generate a message in the SAS log when there are variables in the input datasets that are not present in the first dataset read. It first shows concatenation without OPEN=DEFER, where missing variables are simply filled with missing values. Then, it shows the same process with OPEN=DEFER, which results in the ignoring of additional variables and a warning in the log. The 'Table1' and 'Table2' datasets are created using datalines.
Copied!
data Table1;
input var1 var2 var3;
datalines;
1 10 100
2 20 200
3 30 300
4 40 400
;
run;
data Table2;
input var1 var2 var3 predict lowermean;
datalines;
5 50 500 0.5 0.1
6 60 600 0.6 0.2
7 70 700 0.7 0.3
8 80 800 0.8 0.4
;
run;
data concat;
set table1 table2;
run;
proc print data=concat;
title "Concatenate Table1 and Table2";
run;
data concat2;
set table1 table2 open=defer;
run;
proc print data=concat2;
title "Concatenate with OPEN=DEFER";
run;
1
DATA Table1;
2
INPUT var1 var2 var3;
3
DATALINES;
4
110100
5
220200
6
330300
7
440400
8
;
9
RUN;
10
11
DATA Table2;
12
INPUT var1 var2 var3 predict lowermean;
13
DATALINES;
14
5505000.50.1
15
6606000.60.2
16
7707000.70.3
17
8808000.80.4
18
;
19
RUN;
20
21
DATA concat;
22
SET table1 table2;
23
RUN;
24
PROC PRINTDATA=concat;
25
title "Concatenate Table1 and Table2";
26
RUN;
27
28
DATA concat2;
29
SET table1 table2 open=defer;
30
RUN;
31
PROC PRINTDATA=concat2;
32
title "Concatenate with OPEN=DEFER";
33
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.
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.