スクリプトガイド > プログラム例の紹介 > 文字の日付を数値の日付に変換
公開日: 11/25/2021

文字の日付を数値の日付に変換

データテーブルの列において、自分は「数値」としてデータを扱いたいのに、列プロパティで見ると、データタイプが「文字」に設定されてしまっている場合があります。データを日付時間値として処理するには、列を数値列に変換し、値の表示形式を指定します。

「Convert Dates.jsl」は、まず、データテーブルを作成します。次に、データ入力形式を指定し、データタイプを「文字」から「数値」に、尺度を連続尺度に変更します。最後に、その列に、m/d/y形式を適用します(図17.1)。

// Create a data table with character dates.

dt = New Table( "Example",
	Add Rows( 3 ),
	New Column( "Dates",
		Character,
		Nominal,
		Set Values( {"25/01/2010", "30/09/2009", "15/12/2013"} )
	)
);
 

// Display a modal dialog for the user to confirm the format conversion.

nw = New Window( "Date Conversion",
	<<Modal,
	tb = Text Box(
		"Notice the d/m/y format of the character dates.
Click OK to convert the column to a numeric column and apply the m/d/y format."
	)
);
 

/* Apply the Numeric data type.

Specify the Informat (input format) value "d/m/y".

Specify the Format (display format) value "m/d/y".

Apply the Continuous modeling type */

col = Column( dt, "Dates" );
col << Data Type( "Numeric", Informat( "d/m/y" ), Format( "m/d/y" ) );
col << Modeling Type( "Continuous" );

図17.1 文字の日付の変換(変換前と変換後) 

Converting Character Dates (Before and After)

列のデータタイプを文字から数値に変更した場合は、データの入力形式の定義が重要になります。この例では、Informat( "d/m/y" )が入力形式を定義します。Format( "m/d/y" )は、表示形式を定義します。Informat()を省略した場合、Format()値は入力形式と表示形式の両方として適用されます。この例の場合は、結果としてデータが欠測値となります。

Convert Dates.jslに変更を加え、確認してみましょう。

1. サンプルスクリプトのフォルダにある「Convert Dates.jsl」を開きます。

2. スクリプトウィンドウを右クリックし、[行番号を表示する]を選択します。

3. 9行目で「25/01/2010」を「01/25/2010」に変更します。

4. 27行目で「Informat( "d/m/y" ),」を(カンマも含め)削除します。

5. スクリプトを実行します。

Format( "m/d/y" )が列に適用されます。列には「01/25/2010」だけが表示されます。「30/09/2009」と「15/12/2013」は有効なm/d/y値ではないため、欠測値となります。

より詳細な情報が必要な場合や、質問があるときは、JMPユーザーコミュニティで答えを見つけましょう (community.jmp.com).