FORMAT SAS

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

投稿日:2021年12月1日 更新日:

今回はFORMATプロシージャについて解説します。


/* format */
data data1;
  番号="111111"; test = 10;
    output;
  番号="222222"; test = 20;
    output;
  番号="333333"; test = 30;
    output;
  番号="444444"; test = 40;
    output;
  番号="555555"; test = 50;
    output;
  番号="666666"; test = 60;
    output;
run;


proc format;
  value point 1  -   50 = 'Low'
  51 - 100 = 'High';
run;


proc print data = data1;
  format test point.;
run; 

  • formatプロシージャにより1 – 50 = ‘Low’、51 – 100 = ‘High’とする「point」というルールを設定する。
  • 変数testをpoint.のフォーマットルールで書き換える。

/* test1 */
data data1;
  番号="111111"; test1 = 10;
    output;
  番号="222222"; test1 = 20;
    output;
  番号="333333"; test1 = 30;
    output;
  番号="444444"; test1 = 40;
    output;
  番号="555555"; test1 = 50;
    output;
  番号="666666"; test1 = 60;
    output;
run;


proc format;
  value point 1  -   50   = 'Low'
              51 -   100  = 'High';
run;


proc print data = data1;
  format test point.;
run; 

  • formatプロシージャにより1 – 50 = ‘Low’、51 – 100 = ‘High’とする「point」というルールを設定する。
  • 変数testをpoint.のフォーマットルールで書き換える。
  • data1にtestという変数は無いため、WARNINGとなる。

/* 110 */
data data1;
  番号="111111"; test = 10;
    output;
  番号="222222"; test = 20;
    output;
  番号="333333"; test = 30;
    output;
  番号="444444"; test = 40;
    output;
  番号="555555"; test = 60;
    output;
  番号="666666"; test = 110;
    output;
run;


proc format;
  value point 1  -   50   = 'Low'
              51 -   100  = 'High';
run;


proc print data = data1;
  format test point.;
run; 

  • formatプロシージャにより1 – 50 = ‘Low’、51 – 100 = ‘High’とする「point」というルールを設定する。
  • 変数testをpoint.のフォーマットルールで書き換える。
  • 「point」のルールにない値はそのまま表示される。

/* High2 */
data data1;
  番号="111111"; test = 10;
    output;
  番号="222222"; test = 20;
    output;
  番号="333333"; test = 30;
    output;
  番号="444444"; test = 40;
    output;
  番号="555555"; test = 60;
    output;
  番号="666666"; test = 110;
    output;
run;


proc format;
  value point 1  -   50   = 'Low'
              51 -   100  = 'High'
              110-   200  = 'High2';
run;


proc print data = data1;
  format test point.;
run; 

  • formatプロシージャにより1 – 50 = ‘Low’、51 – 100 = ‘High’、110 – 200 = ‘High2’とする「point」というルールを設定する。
  • 変数testをpoint.のフォーマットルールで書き換える。
  • 「point」のルールが3つ以上でもいける。

/* point point2 */
data data1;
  番号="111111"; test = 10;  test1 = 10;
    output;
  番号="222222"; test = 20;  test1 = 20;
    output;
  番号="333333"; test = 30;  test1 = 30;
    output;
  番号="444444"; test = 40;  test1 = 40;
    output;
  番号="555555"; test = 50;  test1 = 50;
    output;
  番号="666666"; test = 60;  test1 = 60;
    output;
run;


proc format;
  value point 1  -   50 = 'Low'
              51 - 100  = 'High'
       point2 1  -   50 = 'Low1'
              51 - 100  = 'High1';
run;


proc print data = data1;
  format test point.;
  format test1 point2.;
run; 

  • formatプロシージャにより1 – 50 = ‘Low’、51 – 100 = ‘High’とする「point」というルールを設定する。
  • formatプロシージャにより1 – 50 = ‘Low1’、51 – 100 = ‘High1’とする「point2」というルールを設定したい。
  • 変数testをpoint.のフォーマットルールで書き換える。
  • 変数test1をpoint2.のフォーマットルールで書き換えたい。
  • 複数のフォーマットルールは1文で書けない。

/* point point2(2) */
data data1;
  番号="111111"; test = 10;  test1 = 101;
    output;
  番号="222222"; test = 20;  test1 = 200;
    output;
  番号="333333"; test = 30;  test1 = 300;
    output;
  番号="444444"; test = 40;  test1 = 400;
    output;
  番号="555555"; test = 50;  test1 = 500;
    output;
  番号="666666"; test = 60;  test1 = 600;
    output;
run;


proc format;
  value point 1  -   50 = 'Low'
              51 - 100  = 'High'
       point2 101  -150 = 'Low1'
              151 - 200 = 'High1';
run;


proc print data = data1;
  format test point.;
  format test1 point2.;
run; 


  • formatプロシージャにより1 – 50 = ‘Low’、51 – 100 = ‘High’とする「point」というルールを設定する。
  • formatプロシージャにより101 – 150 = ‘Low1’、151 – 200 = ‘High1’とする「point2」というルールを設定したい。
  • 変数testをpoint.のフォーマットルールで書き換える。
  • 変数test1をpoint2.のフォーマットルールで書き換えたい。
  • 複数のフォーマットルールは1文で書けない。(全て範囲が異なっていても)

/* point; point2(3); */
data data1;
  番号="111111"; test = 10;  test1 = 101;
    output;
  番号="222222"; test = 20;  test1 = 200;
    output;
  番号="333333"; test = 30;  test1 = 300;
    output;
  番号="444444"; test = 40;  test1 = 400;
    output;
  番号="555555"; test = 50;  test1 = 500;
    output;
  番号="666666"; test = 60;  test1 = 600;
    output;
run;


proc format;
  value point 1  -   50 = 'Low'
              51 - 100  = 'High';
  value point2 101  -150 = 'Low1'
              151 - 200 = 'High1';
run;


proc print data = data1;
  format test point.;
  format test1 point2.;
run; 

  • formatプロシージャにより1 – 50 = ‘Low’、51 – 100 = ‘High’とする「point」というルールを設定する。
  • formatプロシージャにより101 – 150 = ‘Low1’、151 – 200 = ‘High1’とする「point2」というルールを設定したい。
  • 変数testをpoint.のフォーマットルールで書き換える。
  • 変数test1をpoint2.のフォーマットルールで書き換えたい。
  • フォーマットルール名は最後に数字が使えない。

/* point pointt */
data data1;
  番号="111111"; test = 10;  test1 = 101;
    output;
  番号="222222"; test = 20;  test1 = 200;
    output;
  番号="333333"; test = 30;  test1 = 300;
    output;
  番号="444444"; test = 40;  test1 = 400;
    output;
  番号="555555"; test = 50;  test1 = 500;
    output;
  番号="666666"; test = 60;  test1 = 600;
    output;
run;


proc format;
  value point 1  -   50 = 'Low'
              51 - 100  = 'High';
  value pointt 101  -150 = 'Low1'
              151 - 200 = 'High1';
run;


proc print data = data1;
  format test point.;
  format test1 pointt.;
run; 

  • formatプロシージャにより1 – 50 = ‘Low’、51 – 100 = ‘High’とする「point」というルールを設定する。
  • formatプロシージャにより101 – 150 = ‘Low1’、151 – 200 = ‘High1’とする「pointt」というルールを設定する。
  • 変数testをpoint.のフォーマットルールで書き換える。
  • 変数test1をpointt.のフォーマットルールで書き換える。
  • フォーマットルールを別の文に分け、最後が数字でなければ、正しく実行できる。

/* point pointt(2) */
data data1;
  番号="111111"; test = 10;  test1 = 10;
    output;
  番号="222222"; test = 20;  test1 = 20;
    output;
  番号="333333"; test = 30;  test1 = 30;
    output;
  番号="444444"; test = 40;  test1 = 40;
    output;
  番号="555555"; test = 50;  test1 = 50;
    output;
  番号="666666"; test = 60;  test1 = 60;
    output;
run;


proc format;
  value point 1  -   50 = 'Low'
              51 - 100  = 'High';
  value pointt 1  -   50= 'Low1'
              51 - 100  = 'High1';
run;


proc print data = data1;
  format test point.;
  format test1 pointt.;
run; 

  • formatプロシージャにより1 – 50 = ‘Low’、51 – 100 = ‘High’とする「point」というルールを設定する。
  • formatプロシージャにより1 – 50 = ‘Low1’、51 – 100 = ‘High1’とする「pointt」というルールを設定する。
  • 変数testをpoint.のフォーマットルールで書き換える。
  • 変数test1をpointt.のフォーマットルールで書き換える。
  • フォーマットルールを別の文に分け、最後が数字でなければ、正しく実行できる。
  • 範囲が被っていても正しく実行できる。

-FORMAT, SAS

執筆者:


comment

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

関連記事

【SAS】PUTステートメントは再帰的に型変換ができない。

今回はPUTステートメントの再帰的な型変換について解説します。 (なお、通常のPUT型変換の使い方は以下の記事で解説しています。⇒【SAS】PUTは数値型を文字型に変換する | ビジネスイッチ (ho …

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

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

【SAS】”nは本来使用できない文字を使用可能にする。

今回は”nについて解説します。(参考:Solved: what the ”  ‘n ” means? – SAS Support Commu …

【SAS】LIBNAMEステートメントはExcelファイルをライブラリとして割り当てることができる。【SAS Base Programming対策5】

今回はLIBNAMEステートメントを用いてExcelファイルをライブラリ参照する方法について触れていきます。 SAS Base Programmingの試験を2回受験しましたが、2回ともExcelファ …

【SAS】DATETIME関数はSAS日時値を取得する。

今回はDATETIME関数について解説します。 /* datetime_1 */ data data1; aaaa = datetime(); run; datetime()はSAS時間値を取得する。 …