最近看Cody的SAS Function by Example 2 nd edition。其中1.19是为了读入混合的日期格式。
* Program 1.19: Reading dates in a mixture of formats;
***Primary function: INDEXC;
***Other function: INPUT;
***Program to read mixed dates;
data mixed_dates;
input @1 Dummy $15.;
if indexc(dummy,'/-:') then Date = input(Dummy,mmddyy10.);
else Date = input(Dummy,date9.);
format Date worddate.;
datalines;
10/21/1946
06JUN2002
5-10-1950
7:9:57
;
title "Listing of Data Set MIXED_DATES";
proc print data=mixed_dates noobs;
run;
其中后面也说可以用 ANYDTDTE 格式读入,但是不是期望的结果。后来查到crackman 的帖子:http://crackman.net/?p=1221, 用 options datestyle= mdy 可以将第3个观测(5-10-1950)按照预期的读入。
程序如下:options datestyle= mdy;
data mixed_dates2;
input @1 Date anydtdte10.;
format Date worddate.;
datalines;
10/21/1946
06JUN2002
5-10-1950
7:9:57
;
title "Listing of Data Set MIXED_DATES";
proc print data=mixed_dates2 ;
run;
但是第4个观测(7:9:57)用 anydtdte读入后是1960年1月1日。如何将其读成日期呢?
谢谢!
Obs Date
1 October 21, 1946
2 June 6, 2002
3 October 5, 1950
4 January 1, 1960