How to Read, Write, and Manipulate SAS® Dates
How to Read, Write, and Manipulate SAS® Dates
ABSTRACT
No matter how long you’ve been programming in SAS, using and manipulating dates still seems to require effort.
Learn all about SAS dates, the different ways they can be presented, and how to make them useful. This paper
includes excellent examples in dealing with raw input dates, functions to manage dates, and outputting SAS dates
into other formats. Included are all the date information you will need: date and time functions, Informats, formats,
and arithmetic operations.
Data _null_;
Today = date();
Testdate = ’23jan63’d;
firstdate = ’01jan1960’d;
Run;
Assigning and setting a date is done by one of the 2 methods above. Either a 2 digit year or a 4 digit year are
acceptable. The main point is the quote marks followed immediately by a d. This notation allows SAS to know the
contents inside the quotes are a date.
-1-
*Raw data as Input; *Character data as Input;
cards;
01/23/1963
;
run;
These examples work for all versions of SAS. It takes the information from text and tells SAS the information is a
date in a month-month, day-day, year-year format for a length of 10. SAS assumes the slashes (/), or other
acceptable separation character, is included in the length of 10.
SAS INFORMATS
Below are some of the more common SAS date Informats and the text values they will convert. These informats are
valid for all SAS versions.
INPUT INFORMAT
01/23/1963 ÍÎ mmddyy10.
1/23/1963 ÍÎ mmddyy10.
01/23/63 ÍÎ mmddyy8.
1/23/63 ÍÎ mmddyy8.
January 23,1963 ÍÎ worddate20.
jan 23, 1963 ÍÎ worddate12.
23jan1963 ÍÎ date9.
23jan63 ÍÎ date7.
23-jan-1963 ÍÎ date11.
01-23-63 ÍÎ mmddyy8.
19630123 ÍÎ yymmdd8.
In V9 of SAS, all the above date text values can be converted to SAS utilizing just one Informat: anydtdte15.
Page 2 of 10
SYSTEMS DATES
There are many situations where a date needs to be dynamic. SAS provides several opportunities to extract a date
from the system you are running. Below are a few of the system dates.
data _null_;
a = date();
b = today();
c = "&sysdate"d;
d = "&sysdate9"d;
run;
As you can see, each returns the same results. Although, &sysdate and &sysdate9 return the date the session
started, not necessarily the current date. The system dates are treated as macro variables and require residing
between double quotes with the d designation. Now you have a variable with a valid SAS date.
Date Functions
Because date variables are unique within SAS, they have their own set of functions. It may be you want to extract
something specific from a date while still maintaining it being a SAS date value. Such a situation would be if you
were interested in what day of the week a specific date occurred on. Below are a handful of date functions and what
results from utilizing them would produce.
data _null_;
my_birthday = '23jan63'd;
date1 = day(my_birthday);
date2 = month(my_birthday);
date3 = year(my_birthday);
date4 = qtr(my_birthday);
date5 = weekday(my_birthday);
run;
Log shows:
23 1 1963 1 4
As you can see from the example above, the day function returns the day of the month, month function returns the
month of the year and the year function returns the 4 digit year. The qtr function returns the quarter of the year
where January - March are quarter 1, etc. The weekday function returns values 1 through 7 where day 1 is Sunday
and so on representing the 7 days of the week.
Page 3 of 10
Date Functions and Date Arithmetic
Being able to determine additional information from dates also is endeavored using special SAS date functions. You
may use them to determine the time between to intervals, alter a point in time by a duration of time, or and subtract
time intervals. Below are a few of them.
data _null_;
date = date();
my_birthday = '23jan63'd;
or ‘30/360’;
run;
The intck function returns the number of “intervals” from one date to another. I this case, with the interval of Month,
th
it is the number of months from Nov 5 , 2007 to my birth.
The sum function, while very valid with other forms of numeric’s, also works with dates. As expected, it returns the
number of days between one date interval and another.
The datdif function, in this example using ‘act/act’ (actual/actual), returns the number of days between one point in
time and another point in time. The result here is the number of days from my birth to Nov 5th. 2007.
The intck function can be a very handy function while working on dates. Looking into this function further, below is
a handful of “intervals” and they’re returned results.
data _null_;
my_birthday = '23jan1963'd;
years = intck('year',my_birthday,date());
quarters = intck('qtr',my_birthday,date());
months = intck('month',my_birthday,date());
weeks = intck('week',my_birthday,date());
days = intck('day',my_birthday,date());
run;
Page 4 of 10
The results from the previous data step shows, in order, how many years, how many quarters, how many months,
how many weeks and how many days old I am. All of this with using just the one date function and altering the
“interval”.
Date Formats
Outputting dates from SAS is yet another ability of SAS to manipulate dates. Most people don’t know what today’s
date is in SAS. People need to be able to visualize what date is represented in one of the normal and acceptable
forms. Date formats are used to control the look and results of dates that are currently in SAS form. You can
presents dates in data fields, in report titles or labels. There are quite a few date formats. They may be easily
located in SAS Help and other SAS Documentation. Below are a few to give you a feel of how to use them.
data dates;
date1 = put(my_birthday,mmddyy8.);
date2 = put(my_birthday,worddate15.);
date3 = put(my_birthday,monyy7.);
date4 = put(my_birthday,julian5.);
run;
Log Shows:
The formats above clearly control the look of a SAS date. Even though the date being used above is the SAS date
1118, it is output in a form familiar to us all.
Page 5 of 10
A listing of most SAS Date Functions, Informats and Formats – All in one place!
DATE returns the current date as a SAS MDY returns a SAS date value from
date value month, day, and year values
DATEJUL converts a Julian date to a MINUTE returns the minute from a SAS
SAS date valueDATEPARTextracts the time or datetime value
date from a SAS datetime value
MONTH returns the month from a SAS
DATETIME returns the current date and date value
time of day as a SAS datetime value
QTR returns the quarter of the year
DAY returns the day of the month from from a SAS date value
a SAS date value
SECOND returns the second from a SAS
DHMS returns a SAS datetime value from time or datetime value
date, hour, minute, and seconds
TIME returns the current time of
HMS returns a SAS time value from dayTIMEPARTextracts a time value from
hour, minute, and seconds a SAS datetime value
HOUR returns the hour from a SAS time TODAY returns the current date as a
or datetime value SAS date valueWEEKDAYreturns the day
of the week from a SAS date value
INTCK returns the integer number of
time intervals in a given time span WEEKDAY returns an integer that
represents the day of the week, where
INTNX advances a date, time, or
1=Sunday, 2=Monday,…, 7=Saturday
datetime value by a given interval,
and returns a date, time, or datetime YEAR returns the year from a SAS date
valueJUL valueYRDIFreturns the difference in
years between two dates
DATE returns the Julian date from a
SAS date value YYQ returns a SAS date value from the
year and quarter
Page 6 of 10
Date Informats
17oct91 ddMONyy
DATETIMEw.d date and time:
ddMONyy:hh:mm:ss
17oct91:14:45:32
91290
Oct91
Page 7 of 10
Date Formats
17oct91 OCT91
17/10/91 4
Thursday IV
10/17/91 5
10-1991 1991
MMYYPw. month and year: mm.yy YYMMw. year and month: yyMmm
10.1991 1991M10
MMYYSw. month and year: mm/yy YYMMCw. year and month: yy:mm
10/1991 1991:10
MMYYNw. month and year: mmyy YYMMDw. year and month: yy- mm
101991 1991-10
October 1991.10
10 1991
Page 8 of 10
YYMMw. year and month: yyMmm YYQDw. year and quarter: yy-q
1991M10 1991-4
YYMMCw. year and month: yy:mm YYQPw. year and quarter: yy.q
1991:10 1991.4
YYMMDw. year and month: yy- mm YYQSw. year and quarter: yy/q
1991-10 1991/4
YYMMPw. year and month: yy.mm YYQNw. year and quarter: yyq
1991.10 19914
YYMMSw. year and month: yy/mm YQRw. Yyear and quarter in Roman
1991/10 1991
CONCLUSION
Presented here are the most common tools used while working with dates in SAS programming. Quite frequently,
there is more than one valid method to working with dates. It is best to understand dates and how to manipulate
them. They are part of nearly all programming efforts in one capacity or another.
REFERENCES
SASS 9.1.3 Help and Documentation “Your Complete Guide to Syntax, How To, Examples, Procedures, Concepts,
What’s New, and Tutorials.”
ACKNOWLEDGMENTS
A big “Thank you” to several friends that helped me prepare this paper and presentation. A special thanks to my
friends in Cameroon and Kenya for allowing me to practice this presentation in their attendance
Page 9 of 10
CONTACT INFORMATION
Your comments and questions are valued and encouraged. Contact the author at:
Jenine Milum
The Ginn Group (CDC)
Atlanta, Georgia
E-mail: [email protected]
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS
Institute Inc. in the USA and other countries. ® indicates USA registration.
Other brand and product names are trademarks of their respective companies.
Page 10 of 10