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】INPUTは文字型を数値型に変換する

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

【SAS】LABELオプションは変数名ではなく、ラベル名を出力する。【PRINTプロシージャ】【SAS Base Programming対策2】

今回はLABELオプションについて解説します。 SAS Base Programmingの四択問題でも出されそうな感じです。 /* label */ data data1; attrib Id1 le …

【SAS】DROPで良く問われる文法【SAS Base Programming対策8】

今回もDROPについて解説します。 (対比:KEEP 【SAS】KEEPはカラムの選択に使える。 | ビジネスイッチ (how-to-business.com)) 過去記事ではDROPの使い方について …

【SAS】列名と列ラベルの表示を切り替える方法【LABEL】

今回はデータセットにおける列名と列ラベルの表示を切り替える方法を解説します。 /* label */ data data1; attrib Id1 length = 8 label = “Id1111 …

【SAS】INTNX関数は指定期間の条件を満たす日付けを返す。その2

今回はINTNX関数について追加で解説します。 過去記事では INTNX関数 の使い方(引数3つ)について解説しました。⇒【SAS】INTNX関数は指定期間の条件を満たす日付けを返す。 | ビジネスイ …