- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Does anyone know how to convert number format into date format? For example, the following shows what my data looks like:
20060306 |
20040727 |
20040407 |
20010423 |
All of data is shown as the number format. I want to convert them into date format. For example, I want to change 20060306 into 03/06/2006. I used
the code: date aaa; set aaa; format varialbename mmyydd8.;run;
This code does not work out. Could anyone tell me how to do it? Thanks.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Editor's Note: hongxianzhang's code below shows how to use the INPUT function along with the YYMMDDw.d informat, which was suggested by LinusH. The INPUT function along with the YYMMDDw.d informat allow you to convert a numeric string in the form yymmdd into a SAS date.
DATA SET2;
SET SET1;
DATE2 = INPUT(PUT(DATE1,8.),YYMMDD8.);
FORMAT DATE2 YYMMDD8.;
RUN;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Use yymmdd8. informat
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Editor's Note: hongxianzhang's code below shows how to use the INPUT function along with the YYMMDDw.d informat, which was suggested by LinusH. The INPUT function along with the YYMMDDw.d informat allow you to convert a numeric string in the form yymmdd into a SAS date.
DATA SET2;
SET SET1;
DATE2 = INPUT(PUT(DATE1,8.),YYMMDD8.);
FORMAT DATE2 YYMMDD8.;
RUN;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The INPUT function uses the INFORMAT, in this case YYMMDD, which has the same name as the format in the FORMAT statement in the following line. Informats are used to read text data into (usually) numeric variables. You used the informat correctly.
Richard
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
That is exactly how to use INFORMAT on an existing variable.
Looks like the variable is already numeric so you could store the date value into the original variable.
If you want it to display the same then use YYMMDDN8, otherwise it will display two digit years and hyphens. Or you could use YYMMDD10. to leave room for the hyphens.
DATA SET2;
SET SET1;
DATE1 = INPUT(PUT(DATE1,8.),YYMMDD8.);
FORMAT DATE1 YYMMDD10.;
RUN;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I recently wrote a blog post relating to the put and input function that you may find useful - Rhymes, mnemonics and tips in learning SAS - The SAS Training Post
As your original variable is numeric you use the put function to convert it to a character type and then use the input function using the YYMMDD8. (as that is the way your data looks) to create the numeric SAS date.
The format statement simply displays the SAS date number in the format you want, YYMMDD10.