SAS Code for Converting Number Format into Date Format

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

SAS Code for Converting Number Format into Date Format

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
Solution
‎12-05-2016 03:27 PM
New Contributor
Posts: 2

Re: SAS Code for Converting Number Format into Date Format

[ Edited ]

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;

 

 

View solution in original post


All Replies
Super User
Posts: 5,255

Re: SAS Code for Converting Number Format into Date Format

Use yymmdd8. informat

Data never sleeps
Solution
‎12-05-2016 03:27 PM
New Contributor
Posts: 2

Re: SAS Code for Converting Number Format into Date Format

[ Edited ]

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;

 

 

Super Contributor
Posts: 644

Re: SAS Code for Converting Number Format into Date Format

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

Super User
Super User
Posts: 6,498

Re: SAS Code for Converting Number Format into Date Format

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;

Trusted Advisor
Posts: 1,247

Re: SAS Code for Converting Number Format into Date Format

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.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 99521 views
  • 8 likes
  • 5 in conversation