0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SAS/RでSQL エピソードⅡ : Left Join, Inner Join, Full Joinを使ったデータの結合

Last updated at Posted at 2020-07-16

SQLを用いたデータのハンドリング

SASやRでSQLを使うシリーズ2

※以下はgroup byの紹介
https://qiita.com/saspy/items/2829fdf6ab21ce9d9ce0

今回はleft joinなどデータの結合を紹介

SAS SQLプロシジャ

proc Sql;
  create table work.DATA
    select 残す変数 A.VAR1 , A.VAR2 , B.*など
    from DATA1 as A
    left join DATA2 as B
    結合条件 on A.VAR1 = B.XXX and A.VAR2 >= 100;
quit;

left joinで左結合(DATA1がベースとなり、DATA2にしかない行は残らない)
left joinをright joinにすると右結合(DATA2がベースとなる)
inner joinだと両方ともにある行を残す(数学でいう積集合)
full joinだとどっちかにある行を残す(数学でいう和集合)
差集合にしたいなら、where=うまく使うとか、except使うなど

dataステップでのやり方は、以下を参照
https://qiita.com/saspy/items/7f61d4e733a952cd83e7

R sqldf

DATA <- fn$sqldf(
 " select A.VAR1 , A.VAR2 , B.*
   from DATA1 as A
   left join DATA2 as B
   on A.VAR1 = B.XXX and A.VAR2 >= 100
 " )

full join使ったら、
エラー: RIGHT and FULL OUTER JOINs are not currently supported
と出たのでleft joinとinner joinしかダメかも
まぁそしたらdplyr使えばいい

結合しながら処理ならsqldfの方がいいかもしれないが、dplyrの方がきれいかもね

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?