SAS Date Formats and Informats

Deepanshu Bhalla 29 Comments

This tutorial explains how to use date formats and informats in SAS, along with examples.

What are Formats and Informats?
  • Formats is used to tell SAS how to display or write values of a variable whereas Informats is used to tell SAS how to read a variable. Informats is basically used when you read or import data from either an external file (Text/Excel/CSV) or read in sample data which was created using CARDS/DATALINES statement. It is also used when you create a new variable in a dataset.
  • Formats can be used in both Data Steps and PROC Steps whereas Informats can be used only in Data Steps.

Let's understand by examples -

Example 1 - Read Dates in SAS

In the program below, we have used INFORMATS ddmmyy8. and ddymmyy10. to read dates in SAS. It creates a dataset called sampledata which is stored in WORK library.

DATA sampledata;
     INPUT @6 date1 ddmmyy8. @15 date2 ddmmyy10.;
    CARDS;
     30-12-16 30-12-2016
  ;
RUN;

The INFORMATS ddmmyy8. is used to read 30-12-16 date and ddmmyy10. to read 30-12-2016 date. In this case, 8 and 10 refers to width of the date. The created dataset looks like below -

SAS Date Informat

It returns 20818 as it is in SAS date value form. It is not meaningful if you look at the value. You cannot tell which date it is. To display in real date form, use FORMAT statement.

DATA sampledata;
     INPUT @6 date1 ddmmyy8. @15 date2 ddmmyy10.;
     FORMAT date1 ddmmyy8. date2 ddmmyy10.;
  cards;
     30-12-16 30-12-2016
  ;
RUN;
SAS Date Format
How to read DD-MMM-YY format

You can use date11. format for both DD-MMM-YY and DD-MMM-YYYY format.

DATA temp;
     INPUT @6 dt date11.;
     FORMAT dt date11.;
     CARDS;
     10-oct-14
     ;
PROC PRINT NOOBS;
RUN;
Result : 10-OCT-2014
Example 2 - Display Today's Date

The today() function can be used to generate current date.

data _null_;
    dt=today();
    format dt yymmdd10.;
    put dt ;
run;

Result : It returns 2016-12-30 as 30DEC2016 is the today's date. It's in YYYY-MM-DD format because we've used yymmdd10. format. The 10 refers to the width of the date as 2016-12-30 contains 10 elements. The PUT statement is used to show value in log window.

How to Display Date in Word Format
1. Short Word Date Format

The format date9. returns 30DEC2016.

format dt date9.;
2. Complete Word Date Format

The format WORDDATE. returns DECEMBER 30, 2016. No need to specify width in this format. It automatically adjusts the width depending on the month.

format dt WORDDATE.;
3. Including WEEK

The format WEEKDATE. returns Friday, December 30, 2016

format dt WEEKDATE.;
Display DAY / MONTH / YEAR

In this section, we will see how we can display only day, month, year and weekday.

data _null_;
dt=today();
put "Day :"  dt  DAY.;
put "Month :" dt MONTH.;
put "YEAR:" dt YEAR.;
put "WEEKDAY:" dt DOWNAME.;
run;
SAS Date Formats

We can also use FORMAT in the PUT statement without specifying FORMAT statement explicitly. The DAY. format returned 30, MONTH. format returned 12 and YEAR. format returned 2016. In addition, we have used DOWNAME. format to extract weekday (Friday).

Other Popular Date Formats

Some of the commonly used date formats are listed below -

Formats Result
DDMMYYP10. 30.12.2016
DDMMYYS10. 30/12/2016
MMDDYYP10. 12.30.2016
MMDDYYS10. 12/30/2016
WORDDATX19. 30 DECEMBER 2016
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.

29 Responses to "SAS Date Formats and Informats"
  1. can we use informat and format in excel import ?
    if it is possible can u send me code.

    ReplyDelete
  2. How i can read 10-oct-14 format ?

    ReplyDelete
  3. How to use datetime() with format? A small program example will be of great help.

    ReplyDelete
    Replies
    1. data _null_;
      today=datetime();
      format today datetime.;
      put today;
      run;

      Delete
  4. deepanshu I have a querry. when we extract the month from date like 24-08-2017 it guves results as Month =8. What to do if i want to find out the name of that month like output is Month = August

    ReplyDelete
    Replies
    1. Hi ,

      you can do this by using format varname monname.;

      it will print month name.

      Delete
    2. Use "monname"

      Example :
      data xyz;
      dt=today();
      format dt monname.;
      run;

      Delete
  5. Why used @6 and @15 in the below statment.

    INPUT @6 date1 ddmmyy8. @15 date2 ddmmyy10.;

    ReplyDelete
    Replies
    1. @X -- It defines the starting position of your data set. In above example @6 signifies that column valure starts from 6th.

      Delete
  6. Hello Deepanshu
    as we have different dates in one format using anydtdte.
    can we convert dates in one variable in different formats?

    ReplyDelete
  7. what is the use of @ in this chapter. i believe this @ symbol is used for the formatting.

    ReplyDelete
    Replies
    1. @--it is read it as single trail(@)in sas. it means starting position of the variable.

      Delete
  8. How to read below type of format:
    2012-03-02
    kindly suggest.

    ReplyDelete
  9. Above date i have to read through CSV file.

    ReplyDelete
  10. which format should be used to read date in mm-dd-yyyy format?

    ReplyDelete
  11. hi i have doubt on the following programme how can sas read the data and how i have to use informat and format
    data=rec;
    a=12feb1980;
    b="12feb1980";
    c="12feb1980"d;
    run;
    proc print data=rec;
    run;

    ReplyDelete
    Replies
    1. Hi,

      I think your data step is incorrect. Below is what I thought it should be with some formats applied

      data rec;
      a="12feb1980"d;
      b="12feb1980"d;
      c="12feb1980"d;
      format a b c date9.;
      run;

      proc print data=rec;
      run;

      proc print data=rec;
      format a b c WORDDATX19.;
      run;

      proc print data=rec;
      format a b c DDMMYYS10.;
      run;

      Delete
  12. Data Mrg;
    Infile '\home\data\ds\Mrg.txt';
    Input member Id name$ dob;
    Datalines;
    Run;

    I want date format dob example.16-04-1999,infile store input value 16041999 how to solve..

    ReplyDelete
  13. I want to read dates in different formats in the same dataset how i can solve this

    ReplyDelete
    Replies
    1. Hi,

      we can read dates in different formats but while reading the data from external files the informat should match with data. If you are writing data in data step then you can mention the different formats as per your need. Please check the below sample code.

      DATA sampledata;
      INPUT @6 date1 ddmmyy8. @15 date2 ddmmyy10.;
      FORMAT date1 ddmmyy8. date2 ddmmyy10.;
      cards;
      30-12-16 30-12-2016
      ;
      RUN;

      Delete
  14. unable to understand @6 and @15 so plz make a note on that particular topic...By the way Listen-data is awesome.

    ReplyDelete
Next → ← Prev