This tutorial focuses on converting a number format variable to a date format variable.
 |
Convert Number Format to Date Format |
Suppose you have a numeric variable that contains dates. You are asked to convert it to SAS date format. It seems to be a easy task but it sometimes becomes a daunting task when you don't know how SAS treats dates. The input data is shown below -
 |
Raw Date Values |
Sample Data
The following program is used to create a sample data.
data temp;
input date;
cards;
20160514
19990505
20131104
20110724
;
run;
Solution
data temp2;
set temp;
newdate = input(put(date,8.),yymmdd8.);
format newdate date10.;
proc print noobs;
run;
 |
Output |
Explanation
- PUT Function is used to convert the numeric variable to character format.
- INPUT Function is used to convert the character variable to sas date format
- yymmdd8 informat refers to years followed by month and days having width of total 8
- FORMAT Function is used to display the SAS date values in a particular SAS date format. If we would not use format function, SAS would display the date in SAS datevalues format. For example, 20588 is a sas datevalue and it is equivalent to '14MAY2016'.
ANYDTDTE : Powerful Informat for all dates
ANYDTDTE
is relatively latest informat written or devloped by SAS Institute to handle dates. This is extremely useful when you hate remembering various SAS date informats and working with new dataset which you are not so familiar with the type of date format you may have in your date column. For example date can be like March 17, 2021, 17/03/2021, 03/17/2021 or 17MAR2021. If you wish there would be only one informat to handle all these dates, SAS has already fulfilled your promise.
data temp2;
set temp;
newdate = input(put(date,8.),ANYDTDTE8.);
format newdate date10.;
proc print noobs;
run;
Related Posts
About Author:

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 has worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and Human Resource.
one question, why i cant change the format yymmdd8. by the format date9. in the input function?
ReplyDeletewhat type of formats i can take or select?
the put(date,8.) variable have not a format date, why i can not take other format date?
please my friend
Deletewhy is date10. format giving the output as 14MAY2016, it is the format date9.
ReplyDeleteHow to convert character date "10/13/2019" to date9. Format
ReplyDeleteuse
Deletedt = put(input(date,mmddyy10.),date9.);
format dt date9.;
Can you please explain this using data and set statement. My one date is in $5. And I want to change it to date9. How to do that. Name of the original variable is date_1 and new variable I want to create is date_2
ReplyDeleteHi,
ReplyDeleteHere is some data, where I need to fetch yymmdd10. format data (e.g. 2020-06-08).
which includes all Jan-Dec or January to December data with (- or / ) seperates.
data xyz;
date1= "08-June-2020";output;
date1= "31-Oct-2020";output;
date1= "13/August/2020"; output;
date1= "02/Jan/2020" ;output;
run;
Thank you in advance for your help and take care.
Regards
Priya
Thank you for your help in this topic :)
ReplyDeleteIn your example i use ANYDTDTE. informat for reading data.
My way has pitfalls? Or why did you choose to convert data?
I am new and want to know all secrets :)
data temp;
input date: anydtdte.;
format date yymmdd10.;
cards;
20160514
19990505
20131104
20110724
;
run;
ANYDTDTEw. informat is more flexible (and latest one) than older informat datew. so your way is better than mine :-) We convert number format to date to make it more readable as input value is date not a numeric value.
DeleteHi if date is in 2019/un/un this format.how to get 2019 asoutput
ReplyDeleteHI, I think we can't read data in sas with missing of month and date values. Please let me know if we can read data like this "2019/un/un" in sas.
DeleteBut if we have mmddyy then we can use year() function to pull the year value.
Hello
ReplyDelete