0% found this document useful (0 votes)
18 views6 pages

SAS Date Formats and Functions Guide

SAS can read and write dates in any format and converts all dates to the number of days from January 1, 1960. SAS date formats determine how dates are displayed. Common formats include MMDDYY, DDMMYY, and DATE. Date constants for testing dates must be in single or double quotes followed by an uppercase or lowercase D. SAS includes functions like weekday(), day(), and year() to extract parts of a date as well as functions like yrdif() to calculate differences between dates. An example shows using date formats, functions, and constants.

Uploaded by

Arjun Khosla
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

SAS Date Formats and Functions Guide

SAS can read and write dates in any format and converts all dates to the number of days from January 1, 1960. SAS date formats determine how dates are displayed. Common formats include MMDDYY, DDMMYY, and DATE. Date constants for testing dates must be in single or double quotes followed by an uppercase or lowercase D. SAS includes functions like weekday(), day(), and year() to extract parts of a date as well as functions like yrdif() to calculate differences between dates. An example shows using date formats, functions, and constants.

Uploaded by

Arjun Khosla
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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