DISTINCT SAS SQL

【SAS】DISTINCTは重複をユニークにする。【SQL】

投稿日:

今回はSQL文のDISTINCTについて解説します。


/* Id1 */
data data1;
  Id1=1111; Id2=1111; Id3=1111; Id4=1111;
    output;
  Id1=1111; Id2=1111; Id3=1111; Id4=2222;
    output;
  Id1=2222; Id2=2222; Id3=2222; Id4=3333;
    output;
  Id1=2222; Id2=2222; Id3=2222; Id4=4444;
    output;
  Id1=3333; Id2=3333; Id3=3333; Id4=5555;
    output;
  Id1=3333; Id2=3333; Id3=3333; Id4=6666;
    output;
run;

proc sql;
   create table data2 AS
   select  distinct Id1
   from data1 t1;
quit ;

  • select句にdistinctを用いると、重複をユニーク化する。
  • 今回はId1の重複をユニーク化。

/* Id1,Id4 */
data data1;
  Id1=1111; Id2=1111; Id3=1111; Id4=1111;
    output;
  Id1=1111; Id2=1111; Id3=1111; Id4=2222;
    output;
  Id1=2222; Id2=2222; Id3=2222; Id4=3333;
    output;
  Id1=2222; Id2=2222; Id3=2222; Id4=4444;
    output;
  Id1=3333; Id2=3333; Id3=3333; Id4=5555;
    output;
  Id1=3333; Id2=3333; Id3=3333; Id4=6666;
    output;
run;

proc sql;
   create table data2 AS
   select  distinct Id1,Id4
   from data1 t1;
quit ;

  • select句にdistinctを用いると、重複をユニーク化する。
  • 今回はId1-Id4の重複をユニーク化。

/* *1 */
data data1;
  Id1=1111; Id2=1111; Id3=1111;
    output;
  Id1=1111; Id2=1111; Id3=1111;
    output;
  Id1=2222; Id2=2222; Id3=2222;
    output;
  Id1=2222; Id2=2222; Id3=2222;
    output;
  Id1=3333; Id2=3333; Id3=3333;
    output;
  Id1=3333; Id2=3333; Id3=3333;
    output;
run;

proc sql;
   create table data2 AS
   select  distinct *
   from data1 t1;
quit ;

  • select句にdistinctを用いると、重複をユニーク化する。
  • 今回は全項目での重複をユニーク化。

/* *2 */
data data1;
  Id1=1111; Id2=1111; Id3=1111; Id4=1111;
    output;
  Id1=1111; Id2=1111; Id3=1111; Id4=2222;
    output;
  Id1=2222; Id2=2222; Id3=2222; Id4=3333;
    output;
  Id1=2222; Id2=2222; Id3=2222; Id4=4444;
    output;
  Id1=3333; Id2=3333; Id3=3333; Id4=5555;
    output;
  Id1=3333; Id2=3333; Id3=3333; Id4=6666;
    output;
run;

proc sql;
   create table data2 AS
   select  distinct *
   from data1 t1;
quit ;

  • select句にdistinctを用いると、重複をユニーク化する。
  • 今回は全項目での重複をユニーク化。

-DISTINCT, SAS, SQL

執筆者:


comment

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

関連記事

【SAS】%SYSFUNC関数はデータステップの外でも関数を使えるようにする。その2(%LETステートメント中の挙動)

今回は%SYSFUNC関数について、%LETステートメント中の挙動に着目して解説します。 /* substr */ %let a = substr(123456789, 2, 4); data dat …

【SAS】KEEPはカラムの選択に使える。

今回はKEEPについて解説していきたいと思います。 DROPと対になる感じで、 DROPが指定したカラムを捨てるのに対し、KEEPは指定したカラムのみ残す動きを取ります。 (参考「DROP」:【SAS …

no image

【SQL】【Snowflake】ストアドプロシージャをスケジュール実行する方法【タスク】【CREATE OR REPLACE TASK】

今回は、登録したストアドプロシージャをスケジュール登録し、自動走行する方法について解説します。 テーブルの作成(更新):【SQL】【Snowflake】テーブルの更新【CREATE OR REPLAC …

【SAS】DROPオプションの位置による処理の違い

今回はDROPオプションの付き方によって、結果が異なるケースを解説します。 (対比:KEEP 【SAS】KEEPはカラムの選択に使える。 | ビジネスイッチ (how-to-business.com) …

【SAS】データセットのエンコードを答えさせる問題【CONTENTS】【SAS Base Programming対策7】

今回はデータセットのエンコードを答えさせる問題について解説します。SAS Base Programming試験を受けたときに出題されました。 data data1; Id1=1111; Id2=111 …