SAS: combine data sets by rows using SET statements
1
data three;
set one
two;
run;
Note:
l both datasets share common variables. Such
combination would be like append a datasets to another one.
one
num char
1 A
2 B
4 D
5 E
|
two
num char
2 X
3 Y
5 V
|
three
Obs num char
1 1 A
2 2 B
3 4 D
4 5 E
5 2 X
6 3 Y
7 5 v
|
l Note the data type should be the same for
num variable.
one
num char1
1 A
2 B
4 D
5 E
|
two
num char2
2 X
3 Y
5 V
|
three
Obs num char1 char2
1 1 A
.
2 2 B
.
3 4 D
.
4 5 E
.
5 2 .
X
6 3 .
Y
7 5 .
v
|
1. using BY statements
data three;
set one
two;
by num;
run;
combined two
datasets by rows and then sort row by
the variables num.
one
num char1
1 A
2 B
4 D
5 E
|
two
num char2
2 X
3 Y
5 V
|
three
Obs num char1 char2
1 1 A
.
2 2 B
.
3 2
.
X
4 3
.
Y
5 4 D
.
6 5 E
.
7 5
.
v
|
3
data three;
set one;
set two;
run;
one
num char
1 A
2 B
4 D
5 E
|
two
num char
2 X
3 Y
5 V
|
three
Obs num char
1 2 X
2 3 Y
3 5 v
|
one
num char
1 A
2 B
|
two
num char
2 X
3 Y
5 V
|
three
Obs num char
1 2 X
2 3 Y
|
one
num char
1 A
2 B
4 D
5 E
|
two
num char2
2 X
3 Y
5 V
|
three
Obs num char char2
1 2 A X
2 3 B Y
3 5 D v
|
one
num char
1 A
2 B
4 D
5 E
|
two
num2 char2
2 X
3 Y
5 V
|
three
Obs num char
num2 char2
1 1 A
2 X
2 2 B
3 Y
3 4 D
5 v
|
4.
data three;
set two;
if X=.
THEN set one;
run;
Obs ID X Y
1 1 12
11
2 2 15
.
|
Obs ID X Z
1 2 .
4
2 3 17
6
3 5 18
.
|
Obs ID X Z Y
1 1 12
4 11
2 3 17
6 11
3 5 18
. 11
|
Obs ID X Y
1 1 12
11
2 2 15
2
|
Obs ID X Z
1 1 .
0
2 2 .
4
3 3 17
6
4 5 18
.
|
Obs ID X Z Y
1 1 12
0 11
2 2 15
4 2
3 3 17
6 2
4 5 18
. 2
|
data three;
set two;
if X= NE
.;
run;
Obs ID X Y
1 1 12
11
2 2 15
.
|
Obs ID X Z
1 2 .
4
2 3 17
6
3 5 18
.
|
Obs ID X Z
1 3 17
6
2 5 18
.
|
data three;
set two;
if X EQ .
THEN delete;
run;
Obs ID X Y
1 1 12
11
2 2 15
.
|
Obs ID X Z
1 2 .
4
2 3 17
6
3 5 18
.
|
Obs ID X Z
1 3 17
6
2 5 18
.
|
5.
data five;
set one two three;
run;
one
Obs X
1 1
2 2
|
two
Obs X
1 3
2 4
3 5
|
three
Obs X
1
9
2
10
|
Five
Obs X
1 1
2 2
3 3
4 4
5 5
6 9
7 10
|
6.
one
Obs X
1 1
2 2
|
two
Obs X
1 3
2 4
3 5
|
Clause
|
|
if
_n_=1 then set one;
|
obs x
1
1
2
1
|
if
_n_=1 then set two;
|
obs x
1
3
2
3
3
3
|
if
_n_=1 then set one;
set
two;
|
obs x
1
3
2
4
3
5
|
if
_n_=1 then set two;
set
one;
|
obs
x
1
1
2
2
|
No comments:
Post a Comment