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】DATAステートメントは複数データセットを出力できる。

今回はDATAステートメントで複数データセットを出力する方法について解説します。 /* data2 data3_1 */ data data1 ; id1=”1111″; id2=”1111″; id …

【SAS】SUBSTR関数を使って右から文字列を切り取る方法について解説【LENGTHN】

今回はSUBSTR関数を使って右から文字列を切り取る方法について解説します。 (参考:【SAS】SUBSTR関数は文字列から指定した文字数分切り取る。 | ビジネスイッチ (how-to-busine …

【SAS】IFステートメントは処理を分岐できる。その3~サブセットIF~

今回はサブセットIFについて解説していきたいと思います。 (参考:【SAS】IFステートメントは処理を分岐できる。その1 | ビジネスイッチ (how-to-business.com)) (参考:【S …

【SAS】TIME関数はSAS時間値を取得する。

今回はTIME関数について解説します。 /* time_1 */ data data1; Id1 = time(); run; time()により、SAS時間値を取得する。 /* time_2 */ …

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

今回はSUM関数を用いた足し算で、ofを使った計算について解説します。  (前回はSUM関数を用いた足し算について解説しました。⇒【SAS】足し算。その2(SUM関数を用いた計算) | ビジ …