Dates Format in Sas
Dates Format in Sas
The following table correlates tasks with various SAS language elements that are available
for working with time and date data.
Tasks with Dates and Times, Part 1
Task Type of Language Language Element Input Result
Element
Write SAS date values Date formats DATE. 18703 17MAR11
in recognizable forms
DATE9. 18703 17MAR2011
DAY. 18703 17
DDMMYY. 18703 17/03/11
DDMMYY10. 18703 17/03/2011
DDMMYYB. 18703 17 03 11
DDMMYYB10. 18703 17 03 2011
DDMMYYC. 18703 17:03:11
DDMMYYC10. 18703 17:03:2011
DDMMYYD. 18703 17-03-11
DDMMYYD10. 18703 17-03-2011
DDMMYYN. 18703 17032011
DDMMYYN6. 18703 170311
DDMMYYP. 18703 17.03.11
DDMMYYP10. 18703 17.03.2011
DDMMYYS. 18703 17/03/11
DDMMYYS10. 18703 17/03/2011
DOWNAME. 18703 Friday
JULDAY. 1 18703 77
JULIAN. 1 18703 00077
MMDDYY. 18703 03/17/00
MMDDYY10. 18703 03/17/2011
MMDDYYB. 18703 03 17 00
MMDDYYB10. 18703 03 17 2011
MMDDYYC. 18703 03:17:00
MMDDYYC10. 18703 03:17:2011
MMDDYYD. 18703 03-17-00
MMDDYYD10. 18703 03-17-2011
MMDDYYN. 18703 031700
MMDDYYN8. 18703 03172011
MMDDYYP. 18703 03.17.00
MMDDYYP10. 18703 03.17.2011
MMDDYYS. 18703 03/17/00
MMDDYYS10. 18703 03/17/2011
MMYY. 18703 03M2011
MMYYC. 18703 03:2011
MMYYD. 18703 03-2011
MMYYN. 18703 032011
MMYYP. 18703 03.2011
MMYYS. 18703 03/2011
MONNAME. 18703 March
MONTH. 18703 3
MONYY. 18703 MAR11
PDJULG. 1 18703 2011077F
PDJULI. 1 18703 0100077F
WEEKDATE. 18703 Thursday, March
17, 2011
WEEKDAY. 18703 5
WORDDATE. 18703 March 17, 2011
WORDDATX. 18703 17 March 2011
Quarter formats QTR. 18703 1
QTRR. 18703 I
Time formats TIME. 18703 5:11:43
TIMEAMPM. 18703 5:11:43 AM
TOD. 18703 05:11:43
Year formats YEAR. 18703 2011
YYMM. 18703 2011M03
YYMMC. 18703 2011:03
YYMMD. 18703 2011-03
YYMMP. 18703 2011.03
YYMMS. 18703 2011/03
YYMMN. 18703 201103
YYMMDD. 18703 11-03-17
YYMON. 18703 2011MAR
Year/Quarter YYQ. 18703 2011Q1
formats
YYQC. 18703 2011:1
YYQD. 18703 2011-1
YYQP. 18703 2011.1
YYQS. 18703 2011/1
YYQN. 18703 20111
YYQR. 18703 2011QI
YYQRC. 18703 2011:I
YYQRD. 18703 2011-I
YYQRP. 18703 2011.I
YYQRS. 18703 2011/I
YYQRN. 18703 2011I
1In SAS, a Julian date is a date in the form YYNNN or YYYYNNN, where YY is a two-digit
year, YYYY is a four-digit year, and NNN is the ordinal offset from January 1 of the year
YY or YYYY. SAS processes Julian dates only for valid SAS dates.
Tasks with Dates and Times, Part 2
Task Type of Language Input Result
Language Element
Element
Date Tasks
Read calendar dates as Date DATE. 17MAR11 18703
SAS date informats
Note:
YEARCUTOFF=1920
DATE9. 17MAR2011 18703
DDMMYY. 170300 18703
DDMMYY8. 17032011 18703
JULIAN. 1 11076 18703
JULIAN7. 1 2011077 18703
MMDDYY. 031700 18703
MMDDYY8. 03172011 18703
MONYY. MAR00 18687
YYMMDD. 000317 18703
YYMMDD8. 20110317 18703
YYQ. 11q1 18628
DATETIME 17MAR2011 1615939200
00:00:00
TIME 14:45:32 53132
Return today's date as Date DATE() or () The SAS date value
a SAS date value functions TODAY() for today.
(equivalent)
Extract calendar dates Date DAY 18703 17
from SAS functions
HOUR 18703 5
JULDATE 1 18703 11076
JULDATE7 1 18703 2011076
MINUTE 18703 11
MONTH 18703 3
QTR 18703 1
SECOND 18703 43
WEEKDAY 18703 5
YEAR 18703 2011
Write a date as a SAS date 'ddmmmyy'd '17mar00'd 18703
constant in an constant or '17mar2011'd
expression 'ddmmmyyyy'
Write today's date as a SYSDATE SYSDATE &SYSDATE The date at the time
string automatic of SAS initialization
macro in the form
variable DDMMMYY.
SYSDATE9 SYSDATE9 &SYSDATE9 The date at time of
SAS initialization, in
the form
DDMMMYYYY.
Time Tasks
Write SAS time values time formats HHMM. 18703 14:46
as time values
HOUR. 18703 5
MMSS. 18703 311
TIME. 18703 5:11:43
TIMEAMPM. 18703 5:11:43 AM
TOD. 53132 05:11:43
Read time values as Time TIME. 05:11:43 18703
SAS time values informats
Write the current time SYSTIME SYSTIME &SYSTIME The time at the
as a string automatic moment of
macro execution, in the
variable form HH:MM
Return the current time Time TIME( ) () The SAS time value
of day as a SAS time functions at moment of
value execution, in the
form NNNNN.NNN.
Return the time part of Time TIMEPART 17mar2011 5:11:43
a SAS datetime value functions 05:11:43
Datetime Tasks
Write SAS datetime Datetime DATEAMPM 1615957903 26JUL11:05:11:43
values as datetime formats AM
values
DATETIME 1615957903 17MAR11:05:11:43
Read datetime values Datetime DATETIME 17MAR11:05:11:43 1615957903
as SAS datetime informats
values
Return the current date Datetime DATETIME() () The SAS datetime
and time of day as a functions value at the moment
SAS datetime value of execution, in the
form
NNNNNNNNNN.N.
Interval Tasks
Return the number of Interval INTCK week2 1331
specified time intervals functions 01aug60
that lie between the 01jan11
two date or datetime
values
Advances a date, time, Interval INTNX day 19068
or datetime value by a functions 18703
given interval, and 365
returns a date, time, or
datetime value
1In SAS, a Julian date is a date in the form YYNNN or YYYYNNN, where YY is a two-digit
year, YYYY is a four-digit year, and NNN is the ordinal offset from January 1 of the year
YY or YYYY. SAS processes Julian dates only for valid SAS dates.
SAS also supports international formats and informats that are equivalent to some of the most
commonly used English-language date formats and informats. For details, see the SAS
formats and informats in SAS Formats and Informats: Reference.
Examples
Example 1: Displaying Date, Time, and Datetime Values as Recognizable
Dates and Times
The following example demonstrates how a value might be displayed as a date, a time, or a
datetime. Remember to select the SAS language element that converts a SAS date, time, or
datetime value to the intended date, time, or datetime format. See the previous tables for
examples.
Note:
Time formats count the number of seconds within a day, so the values are between 0
and 86400.
DATETIME formats count the number of seconds since January 1, 1960. For
datetimes that are greater than 02JAN1960:00:00:01 (integer of 86401), the datetime
value is always greater than the time value.
When in doubt, look at the contents of your data set for clues as to which type of
value you are dealing with.
This program uses the DATETIME, DATE, and TIMEAMPM formats to display the value
86399 to a date and time, a calendar date, and a time.
options nodate pageno=1 linesize=80 pagesize=18;
data test;
Time1=86399;
format Time1 datetime.;
Date1=86399;
format Date1 date9.;
Time2=86399;
format Time2 timeampm.;
run;
proc print data=test;
title 'Same Number, Different SAS Values';
footnote1 'Time1 is a SAS DATETIME value';
footnote2 'Date1 is a SAS DATE value';
footnote3 'Time2 is a SAS TIME value';
run;
footnote;
Datetime, Date, and Time Values for 86399
Same Number, Different SAS Values
1
This program reads four regional meeting dates and calculates the dates on which
announcements should be mailed.
data meeting;
1 N 24NOV2012 10OCT2012
2 S 28DEC2012 13NOV2012
3 E 03DEC2012 19OCT2012
4 W 04OCT2012 20AUG2012