SAS Date Examples

Last updated:
Table of Contents

Ok you don't like SAS but you need to get stuff done. I do too.

Examples for doing date stuff in SAS.

Add/Subtract one month from a Date

View the documentation for the INTNX function

Use intnx(<interval>,<originaldate>,<offset>,<mode>):


    format date1 date10.;
    format date2 date10.;
    format date3 date10.;

    date1 = mdy(6,12,2018);

    /*the previous month, on the same day*/
    date2 = intnx("month",date1,-1,"sameday");

    /*the first day of next month*/
    date3 = intnx("month",date1,1,"beginning");

results-of-date-subtraction date2 is 12 May, one month before
The first day of the next month is 1 July


mdy(monthnum,day,year) is the easiest way to create date objects in SAS, both on the data step and in PROC SQL

Parse string as date

Use `input()

Format/convert date to string

Use format <your format>. (don't forget the dot at the end) for the standard DATEw. date formats.

You can use any of the formats in ADD LINK HERE

data foo;
    format date1 date10.;
    date1 = mdy(3,15,2018);

proc sql;
    create table out as
    select date1 format date5.
    from foo;

output-1 This is what date5. looks like

All SAS Date formats

datew.: ddmmyyw.: mmddyyw.: yymmddw.:

Format Name Format Spec (as in Python's strftime[1](#myfootnote1) Example for 15 March, 2018
date5. 15MAR



Dialogue & Discussion