This tutorial explains how to use date formats and informats in SAS, along with examples.
- 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 -
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 -
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;
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
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.
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.;
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;
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).
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 |
can we use informat and format in excel import ?
ReplyDeleteif it is possible can u send me code.
How i can read 10-oct-14 format ?
ReplyDeleteI have added 'how to read this format' in the post. Hope it helps!
Deletedate9.;
DeleteDate9.
DeleteHow to use datetime() with format? A small program example will be of great help.
ReplyDeletedata _null_;
Deletetoday=datetime();
format today datetime.;
put today;
run;
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
ReplyDeleteHi ,
Deleteyou can do this by using format varname monname.;
it will print month name.
Use "monname"
DeleteExample :
data xyz;
dt=today();
format dt monname.;
run;
Why used @6 and @15 in the below statment.
ReplyDeleteINPUT @6 date1 ddmmyy8. @15 date2 ddmmyy10.;
even i hv the same question
Delete@X -- It defines the starting position of your data set. In above example @6 signifies that column valure starts from 6th.
DeleteHello Deepanshu
ReplyDeleteas we have different dates in one format using anydtdte.
can we convert dates in one variable in different formats?
what is the use of @ in this chapter. i believe this @ symbol is used for the formatting.
ReplyDelete@--it is read it as single trail(@)in sas. it means starting position of the variable.
DeleteHow to read below type of format:
ReplyDelete2012-03-02
kindly suggest.
use yyddmm10. format this will help u!!
Deleteuse yymmdd10. format is helpful;
DeleteAbove date i have to read through CSV file.
ReplyDeletewhich format should be used to read date in mm-dd-yyyy format?
ReplyDeleteWe can use mmddyy10.; format
Deletehi i have doubt on the following programme how can sas read the data and how i have to use informat and format
ReplyDeletedata=rec;
a=12feb1980;
b="12feb1980";
c="12feb1980"d;
run;
proc print data=rec;
run;
Hi,
DeleteI 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;
Data Mrg;
ReplyDeleteInfile '\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..
Try Date11.
ReplyDeleteI want to read dates in different formats in the same dataset how i can solve this
ReplyDeleteHi,
Deletewe 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;
unable to understand @6 and @15 so plz make a note on that particular topic...By the way Listen-data is awesome.
ReplyDeletei have a sas dataset with character column as 2024-01-29 05:04:43. how to convert into date, if anyone knows pls let me know.I just need date only
ReplyDeleteCheck out this link - https://www.listendata.com/2024/03/how-to-extract-date-from-datetime-in-sas.html
Delete