SAS : Date Functions

Deepanshu Bhalla 16 Comments

This tutorial explains the various date functions in SAS. It also includes ways to handle date values with SAS.

Syntax of Date Functions in SAS

  1. date(): Returns today's date as a SAS date value.
  2. today(): Returns today's date as a SAS date value.
  3. day(date): Returns the day of month from the variable 'date'.
  4. month(date): Extracts the month component from the variable 'date'.
  5. year(date): Extracts the year component from the variable 'date'.
  6. qtr(date): Extracts the quarter component from the variable 'date'.
  7. weekday(date): Returns the day of week from the variable 'date'.
  8. intnx('year', date, 60): Calculates the date of retirement by adding 60 years to the date.
  9. intck('day', date, date()): Calculates the number of days between date and the current date.
  10. datdif(date, date(), 'ACT/ACT'): Calculates the difference between date and the current date using the ACT/ACT method.
  11. datepart(datetime): Returns the date from datetime.
  12. timepart(datetime): Returns the time from datetime.
System Dates (Current Date)
data _null_;
a = date();
b = today();
c = "&sysdate"d;
put 'Without Format: ' a = b = c =;
run;
See Log
Without Format: a=20480 b=20480 c=19749
How to Format Date Values
data _null_;
a = date();
b = today();
c = "&sysdate"d;
format a b c ddmmyy10.;
put 'With Format: ' a = b = c = ;
run;
See Log
With Format: a=27/01/2016 b=27/01/2016 c=26/01/2014
SAS Date Formats
data dates;
date1 = put(date(),mmddyy8.);
date2 = put(date(),WORDDATE.);
run;
SAS Date Formats
SAS Date Formats
Date Functions
SAS Date Functions
SAS Date Functions
data _null_;
birthdt = '22oct91'd;
date1 = day(birthdt);
date2 = month(birthdt);
date3 = year(birthdt);
date4 = qtr(birthdt);
date5 = weekday(birthdt);
put date1 date2 date3 date4 date5;
run;
Date Calculations

Task - Number of intervals (Years /Months / Days) from birth date to today's date.

Functions - DATDIF and INTCK
data _null_;
birthdt = '22oct91'd;
date5 = datdif(birthdt, date(), 'ACT/ACT');
date55 = intck('day',birthdt, date());
date6 = intck('year',birthdt, date());
date7 = intck('qtr',birthdt, date());
date8 = intck('week',birthdt, date());
put date5 date55 date6 date7 date8;
run;

Note : 'ACT' refers to the actual number of days between dates. Each month is considered to have the actual number of calendar days in that month, and each year is considered to have the actual number of calendar days in that year.

Task -When customer will retire (60 years old)

The INTNX function advances a date value by a given interval and returns a date value.

data _null_;
birthdt = '22oct91'd;
retirement = intnx('year',birthdt, 60);
put retirement date9.;
run;
Additional Parameter in INTNX Option
date10 = intnx('month',date(), 5, 'sameday');
date11 = intnx('month',date(), 5, 'end');
Important Points

1. January 1, 1960 is the zero point for SAS. Any day before 1/1/1960 is a negative number, and any day after that is a positive number.

Old dates can be subject to producing incorrect dates in SAS. You may not get a missing value, butmake sure that you check your century. The YEARCUTOFF option gives you the capability to define a 100-year range for two-digit year values. The default value for the YEARCUTOFF option is 1920, giving you a range of 1920- 2019.

toptions yearcutoff = 1890;
data _null_;
birthdt = '22oct91'd;
currdt = '22oct51'd;
diff = intck('year',birthdt, currdt);
put diff;
run;

In the code above, yearcutoff = 1890 takes century from 1890 to 1989.

2. Subsetting Date Formatted Data
data temp2;
set temp;
where date >= '1jan1991'd;
run;
DATEPART and TIMEPART Functions

DATEPART returns the date from a datetime value. Whereas, TIMEPART returns the time from a datetime value.

data _null_;
x='21oct16:8:45'dt;
xx=datepart(x);
xxx=timepart(x);
put xx worddate.;
put xxx time.;
run;
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 16 Responses to "SAS : Date Functions"
  1. You made it sooooooooo..... easy..thank u

    ReplyDelete
  2. How to print multiple date formats in proc print option

    ReplyDelete
  3. Many, many thanks!!!

    ReplyDelete
  4. is there anyother format option instead of timedate ? bcz this format is not working on portable software.

    ReplyDelete
  5. Thanks for such a nice explanation!

    ReplyDelete
  6. How can I convert the value x=01012017 [Numeric value] into sas date value,like (01Jan2017)

    ReplyDelete
  7. data mydatte;
    x=today();
    format x date9.;

    run;

    ReplyDelete
  8. Sir, what is difference between date() and today() function.?

    ReplyDelete
    Replies
    1. both are same.
      Today() is another name for the date()

      Delete
  9. Please check your sysdate format...it seems wrong and below is the correct values from log.

    data _null_;
    25 a = date();
    26 b = today();
    27 c = "&sysdate"d;
    28 format a b c ddmmyy10.;
    29 put 'With Format: ' a = b = c = ;
    30 run;

    With Format: a=18/12/2020 b=18/12/2020 c=18/12/2020

    ReplyDelete
Next → ← Prev