ITPub博客

首页 > IT职业 > IT职场 > SAS Proc SQL Join

SAS Proc SQL Join

IT职场 作者:ybbyic 时间:2021-09-09 10:20:33 0 删除 编辑

SAS day 24: Proc SQL Join

Review:

Last time we went to over SAS Merge, it is a SAS Merge statement used for 1 - 1 mapping or One - Many mapping,

What should we do for many to many mapping?

图片描述

Problem:

Suppose we want to generate a dataset which has the combined info from both dataset A and B.
*Note: Data A and B both have more than 1 record for each patient. *

Sample Dummy Dataset:
Dataset A
图片描述

Dataset B图片描述

Solutions:

  1. One-sided Join( Left join or Right join) Suppose we want to join dataset A to all the records in dataset B
    Keywords: right join / Left join

图片描述

SAS Code:

proc sql noprint nowarn;
create table example as 
select distinct b.*,  a.pt, a.transyn
from a right join  b 
on a.pt=b.pt
;
quit;

2. Intersection (Inner Join)
Suppose we want to produce all the records that contained in both Dataset A and Dataset B
Keywords: inner join

图片描述

SAS Code

proc sql noprint nowarn;
create table example as 
select distinct b.*,  a.pt, a.transyn
from a inner join  b 
on a.pt=b.pt;
quit;
3. Union (full Join)

Suppose we want to generate a dataset that contains either dataset A or dataset B
Keywords: full join

图片描述

proc sql noprint nowarn;
create table example as 
select distinct b.*,  a.pt, a.transyn
from a full join  b 
on a.pt=b.pt
;
quit;

4. Join with conditionsSuppose we want to select all the records with Transyn="Yes"
Keywords: where

图片描述

SAS Code:

proc sql noprint nowarn;
create table exam_inner as 
select distinct b.*,  a.pt, a.transyn
from a inner join  b 
on a.pt=b.pt
where transyn="Yes"
;
quit;

Summary:
A lot of times we need to combine the info from two datasets or more, in order to amalgamate the info efficiently,
we use SAS Merge for 1 - 1 or 1- many mapping with at least one common key variables,
and use Proc SQL to generate the datasets with many to many mappings.

Happy studying!!! ????

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1769/viewspace-2820581/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    88
  • 访问量
    1009