SAS Date Examples

Last updated:
Table of Contents

WIP Alert This is a work in progress. Current information is correct but more content may be added in the future.

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

Twitter Linkedin YC Hacker News Reddit

mdy

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

Convert char to date

You can use any of the formats in All SAS Date Examples

Use input(<character value>,<date format>)

data table2;
    input datestr=$20.; 
DATALINES;
datestr=foobar
datestr=31MAY18
;
run;

proc sql;
    select input(datestr,date7.) from table2;
quit;

original-data-table The original table (char type)
         
converted to date Date types are represented as integers by SAS

Note that the first element became MISSING
because it's not a valid date string

Format/convert date to string

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

Again, You can use any of the formats in All SAS Date Examples

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

Convert one date format to another

TODO input with current format, and then put with new format

All SAS Date formats

WIP Alert This is a work in progress. Current information is correct but more content may be added in the future.

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