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】YYYY-MM表記にする方法【SUBSTR】【||】

今回はYYYYMMDD表記の年月日をYYYY-MM表記にしたいと思います。 結論:SUBSTRと||を使います。 (SUBSTR:【SAS】SUBSTR関数は文字列から指定した文字数分切り取る。 | …

【SAS】SASで良く使うテクニック。その11「Ctrl+/」~範囲指定でコメントを入れたい時~【SAS Studio】

今回はSASで良く使うテクニックということで、範囲指定でコメントを入れたい時は、「Ctrl+/」が役に立つ理由を解説していきます。 以下のようにソースコードを準備します。(ソースコードの内容は何でも良 …

【SAS】INDEXC関数は文字列から特定の文字を探して位置を返す。

今回はINDEXC関数について解説します。 data data1; Id1=”1111″; Id2=”1111″; Id3=”1111″; output; Id1=”2111″; Id2=”2111″ …

【SAS】FREQプロシージャは頻度集計とクロス集計をする

今回はFREQプロシージャについて解説します。 /* freq 頻度集計 */ data data1; 番号=”AAAAAA”; コード=”02″; 金額=1999; output; 番号=”AAAA …

【SAS】PUTはSAS日付値をフォーマットを変換して表示する【DATE】【YYMMDD】【DATETIME】【TIME】【TOD】

今回もPUTについて解説します。  前回、PUTは数値型⇒文字型への変換ができると解説しました。(参考:【SAS】PUTは数値型を文字型に変換する | ビジネスイッチ (how-to-bus …