Sas 简明教程
SAS - Date & Times
在 SAS 中,日期是数值的一个特例。从 1960 年 1 月 1 日开始,每一天都会指定一个特定的数值。这一天的日期值被指定为 0,下一天的日期值被指定为 1,以此类推。此日期之前的日期用 -1、-2 等表示。通过此方法,SAS 可以表示未来的任何日期和过去的任何日期。
IN SAS dates are a special case of numeric values. Each day is assigned a specific numeric value starting from 1st January 1960. This date is assigned the date value 0 and the next date has a date value of 1 and so on. The previous days to this date are represented by -1 , -2 and so on. With this approach SAS can represent any date in future and any date in past.
当 SAS 从源中读取数据时,它会将读取的数据转换为由日期格式指定特定的日期格式。用于存储日期值的变量使用所需的适当的信息声明。使用输出数据格式显示输出日期。
When SAS reads the data from a source it converts the data read into a specific date format as specified the date format. The variable to store the date value is declared with the proper informat required. The output date is shown by using the output data formats.
SAS Date Informat
可以使用特定的日期信息(如下所示)正确读取源数据。信息结尾的数字表示使用该信息完全读取日期字符串所需的最小宽度。宽度较小会产生错误的结果。在 SAS V9 中,有一个通用日期格式 anydtdte15. ,它可以处理任何日期输入。
The source data can be read properly by using specific date informats as shown below. The digit at the end of the informat indicates the minimum width of the date string to be read completely using the informat. A smaller width will give incorrect result. with SAS V9, there is a generic date format anydtdte15. which can process any date input.
Input Date |
Date width |
Informat |
03/11/2014 |
10 |
mmddyy10. |
03/11/14 |
8 |
mmddyy8. |
December 11, 2012 |
20 |
worddate20. |
14mar2011 |
9 |
date9. |
14-mar-2011 |
11 |
date11. |
14-mar-2011 |
15 |
anydtdte15. |
Example
以下代码显示了读取不同日期格式。请注意,由于我们未对输出值应用任何格式语句,因此所有输出值都只是数字。
The below code shows the reading of different date formats. Please note the all the output values are just numbers as we have not applied any format statement to the output values.
DATA TEMP;
INPUT @1 Date1 date11. @12 Date2 anydtdte15. @23 Date3 mmddyy10. ;
DATALINES;
02-mar-2012 3/02/2012 3/02/2012
;
PROC PRINT DATA = TEMP;
RUN;
当以上代码执行时,我们会得到以下输出:
When the above code is executed, we get the following output.
SAS Date output format
读取日期后,可以将其转换为显示所需的另一种格式。这是通过使用日期类型的格式语句实现的。它们采用与信息相同格式。
The dates after being read , can be converted to another format as required by the display. This is achieved using the format statement for the date types. They take the same formats as informats.
Example
在以下示例中,日期采用一种格式读取,但以另一种格式显示。
In the below exampel the date is read in one format but displayed in another format.
DATA TEMP;
INPUT @1 DOJ1 mmddyy10. @12 DOJ2 mmddyy10.;
format DOJ1 date11. DOJ2 worddate20. ;
DATALINES;
01/12/2012 02/11/1998
;
PROC PRINT DATA = TEMP;
RUN;
当以上代码执行时,我们会得到以下输出:
When the above code is executed, we get the following output.