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】PUTLOGステートメントは文字列をログに出力する。

今回はPUTLOGについて解説します。 /* putlog_1 */ data _null_; putlog ‘2022/03/26’; run; putlogにより、ログに文字列を出力できる。%pu …

【SAS】WHEREステートメントはPRINTプロシージャ内で使用ができる。【IFステートメントは使えない】【SAS Base Programming対策6】

/* where */ data data1; Id1=1111; Id2=1111; Id3=1111; output; Id1=2222; Id2=2222; Id3=2222; output; …

【SAS】足し算。その2(SUM関数を用いた計算)

今回はSUM関数を用いた足し算について解説します。  (前回は+演算子を用いた足し算について解説しました。⇒【SAS】足し算。その1(+演算子を用いた計算)) /* sum */ data …

no image

【SQL】【Snowflake】データベースを新規作成する方法【CREATE DATABASE】

今回は、Snowflake上でデータベースを新規作成する方法について、解説します。 SQL文からデータベースを作成する方法 — 新規作成用 create DATABASE DB1; データベースDB …

【SAS】FORMATプロシージャはフォーマットルールを定義し、値を書き換える。その2

今回もFORMATプロシージャについて解説します。 前回は値の範囲によって、定義されたフォーマットルールに従い、値を書き換えるという内容でした。 【SAS】FORMATプロシージャはフォーマットルール …