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>):

data;

    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");
run;

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

mdy

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);
run;

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

output-1 This is what date5. looks like


All SAS Date formats

datew.: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000195834.htm ddmmyyw.: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000197953.htm mmddyyw.: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000199367.htm yymmddw.: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000197961.htm

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

1: http://strftime.org/


References

Dialogue & Discussion