This tutorial explains how to convert a numeric variable to a date variable in SAS, along with examples.
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 -
Sample Data
The following program is used to create a sample data.
data temp; input date; cards; 20160514 19990505 20131104 20110724 ; run;
Solution
To convert a numeric variable into a date variable in SAS, you can use the following syntax.
data temp2; set temp; newdate = input(put(date,8.),yymmdd8.); format newdate date10.; proc print noobs; run;
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 : One Informat for All
ANYDTDTE
is the relatively latest informat developed by SAS Institute, which alone can handle various date, time or datetime forms. For example date can be like March 17, 2024, 17/03/2024, 03/17/2024, 17MAR2024 17:30:02, 17:30
data temp2; set temp; newdate = input(put(date,8.),ANYDTDTE8.); format newdate date10.; proc print noobs; run;
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.