Working With SAS System Date and Time Functions: Andrew H. Karp
Working With SAS System Date and Time Functions: Andrew H. Karp
Thanks!
Thanks for attending WUSS 12 and this
presentation
Please hold questions until the end of
the presentation
Paper in the Conference Proceedings
Copy of these slides as PDF at
www.SierraInformation.com
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
Working with
SAS System Date and Time Functions
Many applications require that operations be
performed on data collected in the time
domain, such as:
determining the frequency with which a
phenomenon of interest occurs in time
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
Working with
SAS System Date and Time Functions
operating conditionally on observations in a SAS
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
Working with
SAS System Date and Time Functions
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
Informats
convert raw data into SAS date, time or
datetime variables
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
Key Concept
A SAS date, time or date time variable is a
special case of the numeric variable
Date variable:
number of days from January 1, 1960
Time variable:
number of seconds from midnight
A time variable is independent of a date variable
Datetime variable:
number of seconds from midnight 1/1/1960
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
Negative Integers
Positive Integers
July 4, 1976 =
July 4, 2003 =
Oct 14, 2004 =
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
6029
15890
16358
10
Raw Data
10/14/2004
DATA MYDATA;
INFILE more SAS statements ;
INPUT @1 DATE MMDDYY10. ;
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
11
DAY
14
YEAR
2004
MDY
Function
DATE = MDY(MONTH,DAY,YEAR);
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
12
13
SAS Programming
Statements:
Results:
A = MONTH(TODAY);
B = DAY(TODAY);
C = YEAR(TODAY);
D = QTR(TODAY);
E = WEEKDAY(TODAY);
10
14
2004
4
5
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
14
1=Sunday
2=Monday
3=Tuesday
4=Wednesday
5=Thursday
6=Friday
7=Saturday
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
15
16
SAS Programming
Statements
Results
A = HOUR(TIME);
B = MINUTE(TIME);
C = SECOND(TIME);
13
30
00
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
17
18
1413379800
SAS Date value
DATEPART
Function
TIMEPART
Function
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
19
1413379800
SAS Programming Statement:
Result:
DATE = DATEPART(TODAY);
16358
TIME = TIMEPART(TODAY);
48600
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
20
16358
46800
1413379800
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
21
22
Constant
Result:
17467
1509165000
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
23
24
Formatted_Value
No Format
16358
MMDDYY8. Format
10/14/04
MMDDYY10. Format
10/14/2004
DDMMYY10. Format
14/10/2004
DATE. Format
14OCT04
DATE7. Format
14OCT04
YYQ. Format
2004Q4
YEAR. Format
2004
WORDDATE. Format
WEEKDATE. Format
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
25
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
26
INTCK Function
Determines the number of interval boundaries
which have been crossed between two SAS date,
time or date time variables
INTCK(interval , from , to)
interval = character constant or variable name enclosed in
single quotes representing the time period of interest
from = SAS date, time or datetime variable identifying the start
of a time interval
to = SAS date, time or datetime variable identifying the end of
a time interval
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
27
Arithmetic Operation
vs. INTCK Function
Example: a child is born (and therefore admitted to
the hospital) on December 28, 2003 and
discharged on January 2, 2004. The child is
therefore five days old at discharge.
AGE = 02JAN2004D - 28DEC2003D;
yields 5, which is the desired result
BUT, using the INTCK function
AGE =INTCK(YEAR,28DEC2003D,02JAN2004D);
yields 1 as the result. Why?
How old is this child?
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
28
January 2, 2004
January 1, 2004
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
29
WEEKDAY
TENDAY
SEMIMONTH
SEMIYEAR
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
30
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
31
32
MAILDATE = INTNX(month,BDATE,3)
the result is a SAS date variable representing the first day of
the month which is three months past the childs birthday
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
33
END
MIDDLE
BEGINNING
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
34
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
35
TRANSACTION
MONTH:10/31/2002
SAS is a registered trademark of SAS
Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
36
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
37
Data _null_;
transaction_date = '21oct1999'd;
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
38
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
39
40
41
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
42
Two Problems
How can we
Round the values interpolated by PROC
EXPAND?
Solution: TRANSFORMOUT Option
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
43
Solutions
proc expand data=sasclass.carmiss
observed = total out=weekly
from = month
to = week.2;
id date;
convert cars = weekly/transformout=(ceil);
run;
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
44
Solutions
PROC EXPAND
Interpolation from Monthly to Weekly
Default Missing Value Replacement
Weekly Series Starts on Monday
CEIL Transformation Operator Used to Round Results
Obs
276
277
278
279
280
281
282
283
284
285
286
287
288
Mon, 7
Mon, 14
Mon, 21
Mon, 28
Mon, 4
Mon, 11
Mon, 18
Mon, 25
Mon, 2
Mon, 9
Mon, 16
Mon, 23
Mon, 30
Oct
Oct
Oct
Oct
Nov
Nov
Nov
Nov
Dec
Dec
Dec
Dec
Dec
DATE
2002
2002
2002
2002
2002
2002
2002
2002
2002
2002
2002
2002
2002
weekly
8721
8582
8623
8821
9134
9517
9927
10320
10651
10878
10957
10843
10509
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
45
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
46
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
47
Obs DATE
1998
1
cal_year
48,676
1999
53,400
2000
58,600
2001
56,919
2002
59,097
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
48
id date;
convert cars = fiscal_year/transformout=(ceil);
run;
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
49
DATE
Fiscal Year
Starting in June
JUN1997
55,833
JUN1998
35,619
JUN1999
39,144
JUN2000
38,333
JUN2001
42,941
JUN2002
38,515
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
50
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
51
52
Version 8 Enhancements
DATDIF and YRDIF Functions
Returns the number of days (DATDIF) or
years (YRDIF) between two dates.
Similar to the INTCK Function
But, behavior is different
More options
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
53
Version 8 Enhancements
DATDIF(start_date,end_date,basis)
YRDIF(start_date,end_date,basis)
Start_date: period start date
End_date:
period end date (not included in the
calculations)
BASIS
Result
Act/Act
Actual
Same as Act/Act
30/360
Act/360 (YRDIF
Only)
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
54
Version 8 Enhancements
data years;
start = '18dec1956'd;
stop
= '31oct2001'd;
intyears = intck('year',start,stop);
approxyears = (stop-start)/365.25;
difyears_act = yrdif(start,stop,'actual');
difyears_360 = yrdif(start,stop,'act/360');
difyears_365 = yrdif(start,stop,'act/365');
* put it in the sas log;
put start = stop= ;
put intyears= approxyears= ;
put difyears_act = difyears_360 = difyears_365 = ;
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
run;
55
Version 8 Enhancements
The YEARDIF Function
start=-1109
stop=15628
intyears=46
approxyears=45.823408624
difyears_act=45.824552736
difyears_360=46.491666667
difyears_365=45.854794521
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
56
Version 8 Enhancements
The separator character can now be
specified with the
DDMMYY
MMDDYY
YYMMDD Formats
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
57
Version 8 Enhancements
data x;
nov5 = '05nov2001'd;
put " " nov5 mmddyy10.;
put "B " nov5 mmddyyb10.;
put "C " nov5 mmddyyc10.;
put "P " nov5 mmddyyp10.;
run;
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
58
Version 8 Enhancements
969
nov5 = '05nov2001'd;
970
971
972
973
974
run;
11/05/2001
B 11 05 2001
C 11:05:2001
P 11.05.2001
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
59
Version 8 Enhancements
New Date Formats in Release 8.2
DTWKDATX
Displays the Weekday, Month, Day and Year of a
Datetime Variable
DTDATE
Displays the Day, Month and Year of a Datetime
Variable
60
Version 8 Enhancements
data _null_;
datetime1 = '15sep2003:08:00:00'dt;
datetime2 = datetime1;
format
datetime1 dtwkdatx.
datetime2 dtdate.;
put datetime1= datetime2=;
run;
datetime1=Friday, 21 November 2003
datetime2=21NOV03
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
61
Version 8 Enhancements
The %SYSFUNC (system function)
capability was added in V6 and can be
used to execute SAS Functions within
the SAS Macro Facility
Example: Format todays date in the TITLE
Use the %SYSFUNC facility and the
DATE() SAS Programming Language
Function
Apply the WORDDATE. Format
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
62
Version 8 Enhancements
options nocenter ls=80;
proc print data=mwsug.cardtrans(obs=10);
title1 'MWSUG 2003 Conference: Minneapolis';
run;
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
63
Version 8 Enhancements
MWSUG 2003 Conference: Minneapolis
Report Prepared on September 15, 2003
Report Run Time:
6:50:39
trans_ card_
Obs type type
1
2
3
4
5
6
7
8
9
10
2
1
1
1
1
1
1
1
1
1
12
12
12
12
12
12
12
12
12
12
cardnumber
charge_
amount
transaction_
datetime
9630-0420-1039-0391
9630-0420-1039-0391
9630-0420-1039-0391
9630-0420-1039-0391
9630-0420-1039-0391
9630-0420-1039-0391
9630-0420-1039-0391
9630-0420-1039-0391
9630-0420-1039-0391
9630-0420-1039-0391
452.34
955.20
413.37
795.78
898.87
361.14
93.42
386.35
409.61
190.32
08APR99:23:59:59
24MAY99:23:59:59
03AUG99:23:59:59
12AUG99:23:59:59
07SEP99:23:59:59
11SEP99:23:59:59
14OCT99:23:59:59
28DEC99:23:59:59
05JAN00:23:59:59
11JAN00:23:59:59
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
year LOB01
1999
1999
1999
1999
1999
1999
1999
1999
2000
2000
O
O
O
O
O
O
O
O
O
O
64
Default is NODTRESET
SASLOG displays date/time of session initialzation
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
65
ANYDTDTw
ANYDTMEw
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
66
67
date
16084
16084
16084
16084
16084
16084
16071
date2
January 14,
January 14,
January 14,
January 14,
January 14,
January 14,
January 1,
2004
2004
2004
2004
2004
2004
2004
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
68
Optional Descriptors
Change the basis for determining the first week of
the year
See the SAS 9.1 Documentation for the WEEK
Function
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
69
SAS is a registered trademark of SAS Institute in the USA and other countries indicates USA registration. This
document copyright 2004 by Sierra Information Services. All rights reserved.
70