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

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

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

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

【SAS】SORTプロシージャはBYで指定した変数で並べ替える その1 昇順

今回はSORTプロシージャについて解説します。 /*sort1*/ data data1; id1=”1111″; id2=”1111″; output; id1=”1111″; id2=”2222″ …

【SAS】CONTENTSプロシージャはデータセットに関する情報を表示する。

今回はCONTENTSプロシージャについて解説します。  /* data1 */ data data1; Id1=1111; Id2=1111; Id3=1111; output; Id1= …