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】ARRAYステートメントは配列を設定することができる。

今回はarrayステートメントについて解説します。 /* array */ data data1; array hairetsu(3) Id1 Id2 Id3; hairetsu(1) = 1111; …

【SAS】PUTは数値型を文字型に変換する

今回はPUTについて解説します。 /*数値8桁*/ data work.a; a = put(20061228, 8.); run; 数値型⇒文字型への変換はputを使う。数値型20061228⇒文字 …

【SAS】%MACROはマクロを定義することができる。その5 =を使うと数を渡す引数,数を受け取る引数を指定できる。【%MEND】

今回はマクロ引数に「=」を用いる方法について解説していきたいと思います。(事前に読んでおきたい記事:【SAS】%MACROはマクロを定義することができる。その3 マクロには引数を設定できる。【%MEN …

【SAS】オブザベーションを跨いだ計算【RETAIN】【SAS Base Programming対策4】

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

【SAS】0による除算【SAS Base Programming対策3】

今回は0による除算(割り算)を解説します。 数学では「0で割れない」とか、極限の単元だと「∞に発散する」とか言われますが、SASで0による除算を行うとどうなるのか触れていきます。 知ってるか知らないか …