0% found this document useful (0 votes)
152 views

Different Types of Merging Using Data Step or Proc SQL in SAS

SQL SAS

Uploaded by

hima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views

Different Types of Merging Using Data Step or Proc SQL in SAS

SQL SAS

Uploaded by

hima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Different types of merging using Data Step or Proc SQL in

SAS
mehtahemal.com /sas/different-types-of-merging-using-data-step-or-proc-sql-in-sas/450/

By Hemal
Mehta

This post shows different type of merging using Data step and Proc SQL in SAS. Use the following two datasets (A &
B) and give it a try!!

Dataset A = Red color;


Dataset B = Green color;
Common observations in A and B = Yellow color (Red + Green = Yellow!)

DATA A DATA B

Data A; Data B;
input ID Gender$; input ID Sex$;
Datalines; Datalines;
1M 6F
2M 7F
3M 8M
4M 9M
5M 10 M
6F 11 F
7F 12 F
8M 13 F
9M 14 F
10 M 15 F
; ;
run; run;

Graphical
What do you want? representation Data Step Proc SQL

All observations data M; Proc sql;


merge A (in = x) B (in = y); create table M2 as
by id; select coalesce(a.id, b.id) as id,
if x = 1 or y = 1; gender, sex
run; from A full join B
on a.id = b.id;
quit;

All matching obs and non- data N; Proc sql;


matching from A merge A (in = x) B (in = y); create table N2 as
by id; select coalesce(a.id, b.id) as id,
if x = 1; gender, sex
run; from A left join B
on a.id = b.id;
quit;

1/3
Graphical
What do you want? representation Data Step Proc SQL

All matching obs and non- data O; Proc sql;


matching from B merge A (in = x) B (in = y); create table O2 as
by id; select coalesce(a.id, b.id) as id,
if y = 1; gender, sex
run; from A right join B
on a.id = b.id;
quit;

Only matching observations data P; Proc sql;


merge A (in = x) B (in = y); create table P2 as
by id; select coalesce(a.id, b.id) as id,
if x = 1 and y = 1; gender, sex
run; from A inner join B
on a.id = b.id;
quit;

Only nonmatching from A data Q; Proc sql;


merge A (in = x) B (in = y); create table Q2 as
by id; select coalesce(a.id, b.id) as id,
if x = 1 and y = 0; gender, sex
run; from A full join B
on a.id = b.id
where b.id is null;
quit;

More efficient code:


Proc Sql;
create table Q2 as
select coalesce(a.id, b.id) as id,
gender, sex
from A left join B
on a.id = b.id
where b.id is null;
quit;

2/3
Graphical
What do you want? representation Data Step Proc SQL

Only nonmatching from B data R; Proc sql;


merge A (in = x) B (in = y); create table R2 as
by id; select coalesce(a.id, b.id) as id,
if x = 0 and y = 1; gender, sex
run; from A full join B
on a.id = b.id
where a.id is null;
quit;

More efficient code:


Proc Sql;
create table R2 as
select coalesce(a.id, b.id) as id,
gender, sex
from A right join B
on a.id = b.id
where a.id is null;
quit;

Only nonmatching from A and data S; Proc sql;


B merge A (in = x) B (in = y); create table S2 as
by id; select coalesce(a.id, b.id) as id,
if (x = 1 and y = 0) or (x=0 gender, sex
and y = 1); from A full join B
run; on a.id = b.id
where a.id is null or b.id is null;
quit;

Showing 1 to 7 of 7 entries
© 2017 Default copyright text

3/3

You might also like