SAS Date Formats and Informats

This tutorial describes the usage of SAS Date formats and informats. It includes practical real-world data problems related to SAS formats.

What are Formats and Informats?

Informats is used to tell SAS how to read a variable whereas Formats is used to tell SAS how to display or write values of 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 Informat 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.;
     30-12-16 30-12-2016
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 -
Read SAS Date
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.;
     30-12-16 30-12-2016
Output : SAS Format

Example 2 - Display Today's Date

The today() function can be used to generate current date.
data _null_;
    format dt yymmdd10.;
    put dt ;
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.

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. gives Friday, December 30, 2016
format dt WEEKDATE.;

Display DAY / MONTH / YEAR

In this section, we will see how we can write only day, month, year and weekday.
data _null_;
put "Day :"  dt  DAY.;
put "Month :" dt MONTH.;
put "YEAR:" dt YEAR.;
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 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


Hope you have a better understanding of the difference between SAS Date Formats and Informats and how they are used after completing this tutorial.

SAS Tutorials : 100 Free SAS Tutorials

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

0 Response to "SAS Date Formats and Informats"

Post a Comment

Next → ← Prev