You are on page 1of 6

Working with SAS

Dates
SAS Dates
 SAS can read and write dates in any format.
 SAS converts all dates to number of days from 1 Jan
1960.
SAS Date Formats - Examples
Format How Date is Displayed
Mmddyy10. 10/21/1950
Mmddyyd10. 10-21-1950
Mmddyyb10 10 21 1950
Mmddyyc10. 10:21:1950
Ddmmyy10. 21/10/1950
DATE9. 21Oct1950
WEEKDATE. Saturday, October 21, 1950

• Formats ending in 10d, 10b, 10c replace default / with dashes,


blanks or colons respectively.
• We can add d’s, b’s, c’s to the ddmmyy formats as well.
Date Constant-For Testing a Date
Data _null_;
Title “Checking for out of range dates”;
Input @1 Date mmddyy10.; Date value should
File print;
be given in single or
If Date lt ‘01Jan2012’d and not missing(Date) or
Date gt ‘31Dec2014’d
double quotes
then put “Date “ Date “is out of range”; followed by
Format Date mmddyy10.; uppercase or
Datalines; lowercase d. At
10/13/2014
compile time , SAS
5/1/2011
1/1/2015
converts the date
6/5/2014
constant to SAS
1/1/2000 dates.
;
Date Functions
 Weekday (Date) – Extracts day of the week (a number from 1 to 7
with 1 for Sunday)
 Day (Date) – Extracts day of the month (a number from 1 to 31)
 Year (Date) – Extracts year from SAS Date.
 Yrdif(FirstDate,Lastdate) – Computes difference between SAS Dates.
Example:
 Age=yrdif(DOB,’01Jan2015’d)
 For computing age the function will be
Age=int(yrdif(DOB,’01Jan2015’d))
 Today(): SAS function that returns today’s date.
Example
Data Extract;
Informat Date mmddyy10.;
Input date @@;
DOW=weekday(date);
Day_of_month=day(date);
Format date mmddyyd10.;
Datalines;
10/13/2014 5/1/2011 1/1/2015 6/5/2014 1/1/2000 11/5/2005 5/2/2015 10/10/2013 12/4/2012
6/7/2016
;
Title “Listing of all Observations”;
Proc print data=Extract; run;
Title “Listing of first 8 Observations”;
Proc print data=Extract (obs=8); run;

You might also like