Sas-numeric-formats

提供:Dev Guides
移動先:案内検索

SAS-数値形式

SASは、さまざまな数値データ形式を処理できます。 変数名の最後にこれらの形式を使用して、特定の数値形式をデータに適用します。 SASは2種類の数値形式を使用します。 1つは informat と呼ばれる数値データの特定の形式を読み取るためのもので、もう1つは output format と呼ばれる特定の形式で数値データを表示するためのものです。

構文

数値入力形式の構文は-

Varname Formatnamew.d

以下は、使用されるパラメータの説明です-

  • Varname は変数の名前です。
  • Formatname は、変数に適用される数値形式の名前です。
  • w は、変数に格納できるデータ列の最大数(小数点以下の桁数と小数点自体を含む)です。
  • d は、小数点以下の桁数です。

数値形式の読み取り

以下は、SASにデータを読み込むために使用される形式のリストです。

入力数値形式

Format Use
n. Maximum "n" number of columns with no decimal point.
n.p Maximum "n" number of columns with "p" decimal points.
COMMAn.p Maximum "n" number of columns with "p" decimal places which removes any comma or dollar signs.
COMMAn.p Maximum "n" number of columns with "p" decimal places which removes any comma or dollar signs.

数値形式の表示

データの読み取り中にフォーマットを適用するのと同様に、SASプログラムの出力でデータを表示するために使用されるフォーマットのリストを以下に示します。

出力数値フォーマット

Format Use
n. Write maximum "n" number of digits with no decimal point.
n.p Write maximum "n.p" number of columns with "p" decimal points.
DOLLARn.p Write maximum "n" number of columns with p decimal places, leading dollar sign and a comma at the thousandth place.

注意してください-

  • 小数点以下の桁数がフォーマット指定子よりも少ない場合、ゼロが最後に追加されます
  • 小数点以下の桁数がフォーマット指定子よりも大きい場合、最後の桁は「四捨五入」されます。

以下の例は、上記のシナリオを示しています。

DATA MYDATA1;
input x 6.;/*maxiiuum width of the data*/
format x 6.3;
datalines;
8722
93.2
.1122
15.116
PROC PRINT DATA = MYDATA1;
RUN;

DATA MYDATA2;
input x 6.;/*maximum width of the data*/
format x 5.2;
datalines;
8722
93.2
.1122
15.116
PROC PRINT DATA = MYDATA2;
RUN;
DATA MYDATA3;
input x 6.;/*maximum width of the data*/
format x DOLLAR10.2;
datalines;
8722
93.2
.1122
15.116
PROC PRINT DATA = MYDATA3;
RUN;

上記のコードを実行すると、次の結果が生成されます-

# MYDATA1.
Obs     x
1   8722.0 # Display 6 columns with zero appended after decimal.
2   93.200 # Display 6 columns with zero appended after decimal.
3   0.112  # No integers before decimal, so display 3 available digits after decimal.
4   15.116 # Display 6 columns with 3 available digits after decimal.

# MYDATA2
Obs     x
1   8722  # Display 5 columns. Only 4 are available.
2   93.20 # Display 5 columns with zero appended after decimal.
3   0.11  # Display 5 columns with 2 places after decimal.
4   15.12 # Display 5 columns with 2 places after decimal.

# MYDATA3
Obs     x
1   $8,722.00 # Display 10 columns with leading $ sign, comma at thousandth place and zeros appended after decimal.
2   $93.20    # Only 2 integers available before decimal and one available after the decimal.
3   $0.11     # No integers available before decimal and two available after the decimal.
4   $15.12    # Only 2 integers available before decimal and two available after the decimal.